Skip to content

Commit

Permalink
Adding support for sending media
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mer committed Apr 4, 2024
1 parent d250255 commit f91027a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion custom_components/greenapi/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/t0mer/green-api-custom-notifier",
"requirements": ["whatsapp-api-client-python"],
"version": "0.1.0"
"version": "0.2.0"
}
33 changes: 24 additions & 9 deletions custom_components/greenapi/notify.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import logging
import json
import os
from os.path import basename
from urllib.parse import urlparse
import voluptuous as vol
from whatsapp_api_client_python import API
import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import (
ATTR_TARGET, ATTR_TITLE, PLATFORM_SCHEMA, BaseNotificationService)
ATTR_TARGET, ATTR_TITLE, ATTR_DATA, PLATFORM_SCHEMA, BaseNotificationService)

ATTR_INSTANCE = "instance_id"
ATTR_TOKEN = "token"
Expand All @@ -12,35 +16,46 @@
_LOGGER = logging.getLogger(__name__)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(ATTR_TARGET): cv.string,
# vol.Required(ATTR_TARGET): cv.string,
vol.Required(ATTR_INSTANCE): cv.string,
vol.Required(ATTR_TOKEN): cv.string,
vol.Optional(ATTR_TITLE): cv.string,
})

def get_service(hass, config, discovery_info=None):
"""Get the custom notifier service."""
target = config.get(ATTR_TARGET)
title = config.get(ATTR_TITLE)
token = config.get(ATTR_TOKEN)
instance_id = config.get(ATTR_INSTANCE)
return GreenAPINotificationService(target, title, token, instance_id)
return GreenAPINotificationService(title, token, instance_id)

class GreenAPINotificationService(BaseNotificationService):

def __init__(self, target, title, token,instance_id):
def __init__(self, title, token,instance_id):
"""Initialize the service."""
self._targets = target.split(',')
self._title = title
self._token = token
self._instance_id = instance_id
self._greenAPI = API.GreenAPI(self._instance_id, self._token)

def send_message(self, message="", **kwargs):

"""Send a message to the target."""

try:
for target in self._targets:
_LOGGER.info("Sending message to %s: %s", target, message)
data = kwargs.get(ATTR_DATA)
target = kwargs.get(ATTR_TARGET)[0]
_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:
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)
except Exception as e:
_LOGGER.error("Sending message to %s: %s has failed with the following error %s", self._target, message, 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 f91027a

Please sign in to comment.