Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ Multiple media files are now supported. Use the multiline feature as shown below
```yaml
service: notify.wapi_whatsapp_notifire
data:
message: The garage door has been open for 10 minutes.
message: The garage door has been open for 10 minutes. #messages can't be empty but if you want to send images only just put a space " " as message
title: Your Garage Door Friend
target: xxxxxxxxxx@c.us #Can be contact or group chat id
data:
ascaption: true #optional, attaches the title and message as caption to the first image othwise text is independent
media_url: |
https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example
https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example
Expand Down
4 changes: 2 additions & 2 deletions custom_components/wapi/manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"domain": "wapi",
"name": "wapi notifier based on https://github.com/chrishubert/whatsapp-api and can send notifications to whatsapp groups and contacts",
"name": "wapi notifier based on https://github.com/chrishubert/whatsapp-api",
"codeowners": ["@t0mer"],
"documentation": "https://github.com/t0mer/wapi-custom-notifier",
"iot_class": "local_polling",
"issue_tracker": "https://github.com/t0mer/wapi-custom-notifier",
"requirements": ["requests"],
"version": "0.2.0"
"version": "0.2.5"
}
68 changes: 41 additions & 27 deletions custom_components/wapi/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
extra=vol.ALLOW_EXTRA,
)


def get_service(hass, config, discovery_info=None):
"""Get the custom notifier service."""
url = config.get(CONF_URL)
session = config.get(CONFIG_SESSION)
token = config.get(CONFIG_TOKEN)
return MatterNotificationService(url, session, token)


class MatterNotificationService(BaseNotificationService):
def __init__(self, url, session, token=None):
self._url = url
Expand All @@ -55,29 +53,45 @@ def __send(self, data):
_LOGGER.error("Error sending notification using wapi: %s", ex)

def send_message(self, message="", **kwargs):
title = kwargs.get(ATTR_TITLE)
title = kwargs.get(ATTR_TITLE) or ""
chatId = kwargs.get(ATTR_TARGET)
data = kwargs.get(ATTR_DATA)

msg_data = {
"content": "*" + title + "* \n" + message,
"chatId": chatId,
"contentType": "string",
}
self.__send(msg_data)

if data is not None and data["media_url"] is not None:
media_urls = data["media_url"].splitlines()
for url in media_urls:
media_data = {
"content": url,
"chatId": chatId,
"contentType": "MessageMediaFromURL"
}
self.__send(media_data)






data = kwargs.get(ATTR_DATA) or {}
media_urls = data.get("media_url", "").splitlines() if data.get("media_url") else []
message = "" if message == " " else message
ascaption = data.get("ascaption", False)

def format_text(title, message):
return ("*" + title + "*" + ("\n" if message else "") if title else "") + (message or "")

if ascaption and len(media_urls) > 1:
_LOGGER.warning("Multiple media URLs provided, but 'ascaption' is true. Only the first URL will have a caption.")

if not media_urls:
self.__send({
"content": format_text(title, message),
"chatId": chatId,
"contentType": "string",
})
return

if ascaption:
self.__send({
"chatId": chatId,
"contentType": "MessageMediaFromURL",
"content": media_urls[0],
"options": {"caption": format_caption(title, message)}
})
media_urls = media_urls[1:]
elif title or message:
self.__send({
"content": format_text(title, message),
"chatId": chatId,
"contentType": "string",
})

for url in media_urls:
self.__send({
"chatId": chatId,
"contentType": "MessageMediaFromURL",
"content": url,
})