@@ -32,7 +32,7 @@ def get_service(hass, config, discovery_info=None):
32
32
33
33
class GreenAPINotificationService (BaseNotificationService ):
34
34
35
- def __init__ (self , title , token ,instance_id , target ):
35
+ def __init__ (self , title , token , instance_id , target ):
36
36
"""Initialize the service."""
37
37
self ._title = title
38
38
self ._token = token
@@ -53,15 +53,20 @@ def send_message(self, message="", **kwargs):
53
53
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.
54
54
_LOGGER .info (f"Sending message to { target } " )
55
55
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 )
60
62
url_file = upload_file_response .data ["urlFile" ]
61
63
url = urlparse (url_file )
62
64
file_name = basename (url .path )
63
65
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 )
66
70
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