Skip to content

Commit 9439e76

Browse files
authored
Merge pull request #7 from ribbal/file_upload
Enhance file upload process
2 parents 38de319 + d546647 commit 9439e76

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,5 @@ data:
115115

116116
```
117117

118-
# Important
119-
### If the path to the file does not exist no message will be sent.
118+
#### Important
119+
If the path to the file does not exist, the message will still be sent; but will log a warning.

custom_components/greenapi/notify.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_service(hass, config, discovery_info=None):
3232

3333
class GreenAPINotificationService(BaseNotificationService):
3434

35-
def __init__(self, title, token,instance_id, target):
35+
def __init__(self, title, token, instance_id, target):
3636
"""Initialize the service."""
3737
self._title = title
3838
self._token = token
@@ -53,15 +53,20 @@ def send_message(self, message="", **kwargs):
5353
target = kwargs.get(ATTR_TARGET)[0] if kwargs.get(ATTR_TARGET) is not None else self._target #Allow setting the target from either the service-call or the service config. Service call target can override the default config.
5454
_LOGGER.info(f"Sending message to {target}")
5555
if data is not None:
56-
file_path = data["file"]
57-
if os.path.exists(file_path):
58-
upload_file_response = self._greenAPI.sending.uploadFile(file_path)
59-
if upload_file_response.code == 200:
56+
file_path = data.get("file")
57+
if file_path is not None:
58+
if os.path.exists(file_path):
59+
upload_file_response = self._greenAPI.sending.uploadFile(file_path)
60+
if upload_file_response.code != 200:
61+
raise Exception(upload_file_response.code, "Failed to upload file: " + file_path)
6062
url_file = upload_file_response.data["urlFile"]
6163
url = urlparse(url_file)
6264
file_name = basename(url.path)
6365
send_file_by_url_response = self._greenAPI.sending.sendFileByUrl(target, url_file, file_name, caption=message)
64-
else:
65-
self._greenAPI.sending.sendMessage(target, message, linkPreview=False)
66+
return
67+
else:
68+
_LOGGER.warn("Sending message to %s: excluding the file '%s' that was not found", kwargs.get(ATTR_TARGET)[0], file_path)
69+
self._greenAPI.sending.sendMessage(target, message, linkPreview=False)
6670
except Exception as e:
67-
_LOGGER.error("Sending message to %s: has failed with the following error %s", kwargs.get(ATTR_TARGET)[0] ,str(e))
71+
_LOGGER.error("Sending message to %s: has failed with the following error %s", kwargs.get(ATTR_TARGET)[0], str(e))
72+

0 commit comments

Comments
 (0)