Skip to content

Commit

Permalink
Merge pull request #7 from ribbal/file_upload
Browse files Browse the repository at this point in the history
Enhance file upload process
  • Loading branch information
t0mer authored Aug 13, 2024
2 parents 38de319 + d546647 commit 9439e76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ data:

```

# Important
### If the path to the file does not exist no message will be sent.
#### Important
If the path to the file does not exist, the message will still be sent; but will log a warning.
21 changes: 13 additions & 8 deletions custom_components/greenapi/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_service(hass, config, discovery_info=None):

class GreenAPINotificationService(BaseNotificationService):

def __init__(self, title, token,instance_id, target):
def __init__(self, title, token, instance_id, target):
"""Initialize the service."""
self._title = title
self._token = token
Expand All @@ -53,15 +53,20 @@ def send_message(self, message="", **kwargs):
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.
_LOGGER.info(f"Sending message to {target}")
if data is not None:
file_path = data["file"]
if os.path.exists(file_path):
upload_file_response = self._greenAPI.sending.uploadFile(file_path)
if upload_file_response.code == 200:
file_path = data.get("file")
if file_path is not None:
if os.path.exists(file_path):
upload_file_response = self._greenAPI.sending.uploadFile(file_path)
if upload_file_response.code != 200:
raise Exception(upload_file_response.code, "Failed to upload file: " + file_path)
url_file = upload_file_response.data["urlFile"]
url = urlparse(url_file)
file_name = basename(url.path)
send_file_by_url_response = self._greenAPI.sending.sendFileByUrl(target, url_file, file_name, caption=message)
else:
self._greenAPI.sending.sendMessage(target, message, linkPreview=False)
return
else:
_LOGGER.warn("Sending message to %s: excluding the file '%s' that was not found", kwargs.get(ATTR_TARGET)[0], file_path)
self._greenAPI.sending.sendMessage(target, message, linkPreview=False)
except Exception as e:
_LOGGER.error("Sending message to %s: has failed with the following error %s", kwargs.get(ATTR_TARGET)[0] ,str(e))
_LOGGER.error("Sending message to %s: has failed with the following error %s", kwargs.get(ATTR_TARGET)[0], str(e))

0 comments on commit 9439e76

Please sign in to comment.