Add support to ignore m.text messages and fix url module to use this. Fix url not saving message type.

This commit is contained in:
Ville Ranki 2020-03-29 20:34:57 +03:00
parent e3ad073fad
commit 48d8298828
3 changed files with 40 additions and 15 deletions

View File

@ -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`. 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 ## Module API
Just write a python file with desired command name and place it in modules. See current modules for 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 ```python
class MatrixModule(BotModule): class MatrixModule(BotModule):
async def matrix_message(self, bot, room, event): async def matrix_message(self, bot, room, event):
args = event.body.split() args = event.body.split()
args.pop(0) args.pop(0)
# Echo what they said back # Echo what they said back
self.logger.debug(f"room: {room.name} sender: {event.sender} wants an echo") self.logger.debug(f"room: {room.name} sender: {event.sender} wants an echo")
await bot.send_text(room, ' '.join(args)) await bot.send_text(room, ' '.join(args))
@ -350,9 +345,9 @@ class MatrixModule(BotModule):
def help(self): def help(self):
return 'Echoes back what user has said' return 'Echoes back what user has said'
``` ```
Functions: ### Functions
* matrix_start - Called once on startup * matrix_start - Called once on startup
* async matrix_message - Called when a message is sent to room starting with !module_name * 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. 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. Use `self.logger` in your module to print information to the console.
Module settings are stored in Matrix account data. 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. If you write a new module, please make a PR if it's something useful for others.

22
bot.py
View File

@ -70,20 +70,25 @@ class Bot:
self.logger.debug("Logger initialized") 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 = { msg = {
"body": body, "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) 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 = { msg = {
"msgtype": msgtype, "msgtype": msgtype,
"format": "org.matrix.custom.html", "format": "org.matrix.custom.html",
"formatted_body": html, "formatted_body": html,
"body": plaintext "body": plaintext
} }
if bot_ignore:
msg["org.vranki.hemppa.ignore"] = "true"
await self.client.room_send(room.room_id, 'm.room.message', msg) await self.client.room_send(room.room_id, 'm.room.message', msg)
async def send_image(self, room, url, body): async def send_image(self, room, url, body):
@ -133,6 +138,10 @@ class Bot:
def is_owner(self, event): def is_owner(self, event):
return event.sender in self.owners 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): def save_settings(self):
module_settings = dict() module_settings = dict()
for modulename, moduleobject in self.modules.items(): for modulename, moduleobject in self.modules.items():
@ -157,8 +166,13 @@ class Bot:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
async def message_cb(self, room, event): 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 body = event.body
# Figure out the command
if not self.starts_with_command(body): if not self.starts_with_command(body):
return return

View File

@ -46,6 +46,9 @@ class MatrixModule(BotModule):
""" """
Handle client callbacks for all room text events Handle client callbacks for all room text events
""" """
if self.bot.should_ignore_event(event):
return
# no content at all? # no content at all?
if len(event.body) < 1: if len(event.body) < 1:
return return
@ -88,7 +91,7 @@ class MatrixModule(BotModule):
msg = f"Description: {description}" msg = f"Description: {description}"
if msg is not None: 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) @lru_cache(maxsize=128)
def get_content_from_url(self, url): def get_content_from_url(self, url):
@ -182,6 +185,7 @@ class MatrixModule(BotModule):
def get_settings(self): def get_settings(self):
data = super().get_settings() data = super().get_settings()
data['status'] = self.status data['status'] = self.status
data['type'] = self.type
return data return data
def set_settings(self, data): def set_settings(self, data):