From 48d8298828d01d9388792acc2f020210cfa8f4f5 Mon Sep 17 00:00:00 2001 From: Ville Ranki Date: Sun, 29 Mar 2020 20:34:57 +0300 Subject: [PATCH] Add support to ignore m.text messages and fix url module to use this. Fix url not saving message type. --- README.md | 27 +++++++++++++++++---------- bot.py | 22 ++++++++++++++++++---- modules/url.py | 6 +++++- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c30f493..8d5fd4c 100644 --- a/README.md +++ b/README.md @@ -324,11 +324,6 @@ __*ATTENTION:*__ Don't include bot itself in `BOT_OWNERS` if cron or any other m To enable debugging for the root logger set `DEBUG=True`. -## Logging - -Uses [python logging facility](https://docs.python.org/3/library/logging.html) to print information to the console. Customize it to your needs editing `config/logging.yml`. -See [logging.config documentation](https://docs.python.org/3/library/logging.config.html) for further information. - ## Module API Just write a python file with desired command name and place it in modules. See current modules for @@ -338,11 +333,11 @@ examples. No need to register it anywhere else. ```python class MatrixModule(BotModule): - + async def matrix_message(self, bot, room, event): args = event.body.split() args.pop(0) - + # Echo what they said back self.logger.debug(f"room: {room.name} sender: {event.sender} wants an echo") await bot.send_text(room, ' '.join(args)) @@ -350,9 +345,9 @@ class MatrixModule(BotModule): def help(self): return 'Echoes back what user has said' -``` +``` -Functions: +### Functions * matrix_start - Called once on startup * async matrix_message - Called when a message is sent to room starting with !module_name @@ -364,10 +359,22 @@ Functions: You only need to implement the ones you need. See existing bots for examples. -Logging: +### Logging + +Uses [python logging facility](https://docs.python.org/3/library/logging.html) to print information to the console. Customize it to your needs editing `config/logging.yml`. +See [logging.config documentation](https://docs.python.org/3/library/logging.config.html) for further information. Use `self.logger` in your module to print information to the console. Module settings are stored in Matrix account data. +### Ignoring text messages + +If you want to send a m.text message that bot should always ignore, set "org.vranki.hemppa.ignore" property in the event. Bot will ignore events with this set. +Set the bot_ignore parameter to True in sender functions to acheive this. + +If you write a module that installs a custom message handler, use bot.should_ignore_event(event) to check if event should be ignored. + +## Contributing + If you write a new module, please make a PR if it's something useful for others. diff --git a/bot.py b/bot.py index a719b3a..d2f1f2d 100755 --- a/bot.py +++ b/bot.py @@ -70,20 +70,25 @@ class Bot: self.logger.debug("Logger initialized") - async def send_text(self, room, body, msgtype="m.notice"): + async def send_text(self, room, body, msgtype="m.notice", bot_ignore=False): msg = { "body": body, - "msgtype": msgtype + "msgtype": msgtype, } + if bot_ignore: + msg["org.vranki.hemppa.ignore"] = "true" + await self.client.room_send(room.room_id, 'm.room.message', msg) - async def send_html(self, room, html, plaintext, msgtype="m.notice"): + async def send_html(self, room, html, plaintext, msgtype="m.notice", bot_ignore=False): msg = { "msgtype": msgtype, "format": "org.matrix.custom.html", "formatted_body": html, "body": plaintext } + if bot_ignore: + msg["org.vranki.hemppa.ignore"] = "true" await self.client.room_send(room.room_id, 'm.room.message', msg) async def send_image(self, room, url, body): @@ -133,6 +138,10 @@ class Bot: def is_owner(self, event): return event.sender in self.owners + # Checks if this event should be ignored by bot, including custom property + def should_ignore_event(self, event): + return "org.vranki.hemppa.ignore" in event.source['content'] + def save_settings(self): module_settings = dict() for modulename, moduleobject in self.modules.items(): @@ -157,8 +166,13 @@ class Bot: traceback.print_exc(file=sys.stderr) async def message_cb(self, room, event): - # Figure out the command + # Ignore if asked to ignore + if self.should_ignore_event(event): + print('Ignoring this!') + return + body = event.body + # Figure out the command if not self.starts_with_command(body): return diff --git a/modules/url.py b/modules/url.py index f094a04..ab946d2 100644 --- a/modules/url.py +++ b/modules/url.py @@ -46,6 +46,9 @@ class MatrixModule(BotModule): """ Handle client callbacks for all room text events """ + if self.bot.should_ignore_event(event): + return + # no content at all? if len(event.body) < 1: return @@ -88,7 +91,7 @@ class MatrixModule(BotModule): msg = f"Description: {description}" if msg is not None: - await self.bot.send_text(room, msg, self.type) + await self.bot.send_text(room, msg, msgtype=self.type, bot_ignore=True) @lru_cache(maxsize=128) def get_content_from_url(self, url): @@ -182,6 +185,7 @@ class MatrixModule(BotModule): def get_settings(self): data = super().get_settings() data['status'] = self.status + data['type'] = self.type return data def set_settings(self, data):