From 942d105de20cec11b050e354342193ae72d447e9 Mon Sep 17 00:00:00 2001 From: Sophie Tauchert Date: Sun, 8 Jan 2023 17:51:23 +0100 Subject: [PATCH] Add support for tags with optional emoji support --- maubot.yaml | 3 +++ ntfy/bot.py | 34 ++++++++++++++++++++++++---------- ntfy/emoji.py | 26 ++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 ntfy/emoji.py diff --git a/maubot.yaml b/maubot.yaml index 598892a..ffce0e8 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -12,3 +12,6 @@ database_type: asyncpg config: true extra_files: - base-config.yaml + +soft_dependencies: + - "emoji>=2.0" diff --git a/ntfy/bot.py b/ntfy/bot.py index 2a79c3b..7ec0cea 100644 --- a/ntfy/bot.py +++ b/ntfy/bot.py @@ -14,6 +14,7 @@ from mautrix.util.formatter import parse_html from .config import Config from .db import DB, Topic, upgrade_table +from .emoji import parse_tags, WHITE_CHECK_MARK class NtfyBot(Plugin): @@ -87,7 +88,7 @@ class NtfyBot(Plugin): else: await self.db.add_subscription(db_topic.id, evt.room_id) await evt.reply("Subscribed this room to %s/%s", server, topic) - await evt.react("✅") + await evt.react(WHITE_CHECK_MARK) if not existing_subscriptions: await self.subscribe_to_topic(db_topic) @@ -108,7 +109,7 @@ class NtfyBot(Plugin): return await self.db.remove_subscription(db_topic.id, evt.room_id) await evt.reply("Unsubscribed this room from %s/%s", server, topic) - await evt.react("✅") + await evt.react(WHITE_CHECK_MARK) async def subscribe_to_topics(self) -> None: topics = await self.db.get_topics() @@ -170,8 +171,7 @@ class NtfyBot(Plugin): self.log.exception( "Failed to send matrix message!", exc_info=exc) - @classmethod - def build_message_content(cls, server: str, message) -> str: + def build_message_content(self, server: str, message) -> str: topic = message["topic"] body = message["message"] title = message.get("title", None) @@ -179,21 +179,35 @@ class NtfyBot(Plugin): click = message.get("click", None) attachment = message.get("attachment", None) + if tags: + (emoji, non_emoji) = parse_tags(self.log, tags) + emoji = "".join(emoji) + " " + tags = ", ".join(non_emoji) + else: + emoji = tags = "" + html_content = "Ntfy message in topic %s/%s
" % ( html.escape(server), html.escape(topic)) # build title if title and click: - html_content += "

%s

" % ( - html.escape(click), html.escape(title)) + html_content += "

%s%s

" % ( + emoji, html.escape(click), html.escape(title)) + emoji = "" elif title: - html_content += "

%s

" % html.escape(title) + html_content += "

%s%s

" % (emoji, html.escape(title)) + emoji = "" # build body if click and not title: - html_content += "%s" % (html.escape( - click), html.escape(body).replace("\n", "
")) + html_content += "%s%s" % ( + emoji, html.escape(click), html.escape(body).replace("\n", "
")) else: - html_content += html.escape(body).replace("\n", "
") + html_content += emoji + html.escape(body).replace("\n", "
") + + # add non-emoji tags + if tags: + html_content += "
Tags: %s" % html.escape( + tags) # build attachment if attachment: diff --git a/ntfy/emoji.py b/ntfy/emoji.py new file mode 100644 index 0000000..176036d --- /dev/null +++ b/ntfy/emoji.py @@ -0,0 +1,26 @@ +from typing import List, Tuple + +from mautrix.util.logging import TraceLogger + +try: + import emoji + WHITE_CHECK_MARK = emoji.emojize(":white_check_mark:") +except ImportError: + emoji = None + WHITE_CHECK_MARK = "✅" + + +def parse_tags(log: TraceLogger, tags: List[str]) -> Tuple[List[str], List[str]]: + if emoji is None: + log.warn("Please install the `emoji` package for emoji support") + return ([], tags) + emojis = [] + non_emoji_tags = [] + + for tag in tags: + emojized = emoji.emojize(f":{tag}:") + if emoji.is_emoji(emojized): + emojis.append(emojized) + else: + non_emoji_tags.append(tag) + return (emojis, non_emoji_tags)