-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmcommand_handler.py
More file actions
32 lines (26 loc) · 1.06 KB
/
mcommand_handler.py
File metadata and controls
32 lines (26 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
Defines a matrix bot handler for commands
"""
import re
from matrix_bot_api.mhandler import MHandler
class MCommandHandler(MHandler):
# command - String of command to handle
# handle_callback - Function to call if message contains command
# cmd_char - Character that denotes a command. '!' by default
def __init__(self, command, handle_callback, cmd_char='!'):
MHandler.__init__(self, self.test_command, handle_callback)
self.command = command
self.cmd_char = cmd_char
# Function called by Matrix bot api to determine whether or not to handle this message
def test_command(self, room, event):
# Test the message to see if it has our command
if event['type'] == "m.room.message":
if re.match(self.cmd_char + self.command, event['content']['body']):
return True
return False
# Generic command testing function for all MHandler objects
def triggers_on(self, trigger):
if trigger == self.command:
return True
else:
return False