diff --git a/README.md b/README.md index 3a93589..774a553 100644 --- a/README.md +++ b/README.md @@ -115,5 +115,5 @@ data: ``` -# Important -### If the path to the file does not exist no message will be sent. \ No newline at end of file +#### Important +If the path to the file does not exist, the message will still be sent; but will log a warning. \ No newline at end of file diff --git a/custom_components/greenapi/notify.py b/custom_components/greenapi/notify.py index e062d09..f775608 100644 --- a/custom_components/greenapi/notify.py +++ b/custom_components/greenapi/notify.py @@ -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 @@ -53,15 +53,19 @@ 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) + 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) except Exception as e: - _LOGGER.error("Sending message to %s: has failed with the following error %s", kwargs.get(ATTR_TARGET)[0] ,str(e)) \ No newline at end of file + _LOGGER.error("Sending message to %s: has failed with the following error %s", kwargs.get(ATTR_TARGET)[0], str(e)) \ No newline at end of file