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:
parent
e3ad073fad
commit
48d8298828
21
README.md
21
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
|
||||
|
@ -352,7 +347,7 @@ class MatrixModule(BotModule):
|
|||
|
||||
```
|
||||
|
||||
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.
|
||||
|
|
22
bot.py
22
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
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue