-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmregex_handler.py
More file actions
31 lines (24 loc) · 934 Bytes
/
mregex_handler.py
File metadata and controls
31 lines (24 loc) · 934 Bytes
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
"""
Defines a matrix bot handler that uses regex to determine if message should be handled
"""
import re
from matrix_bot_api.mhandler import MHandler
class MRegexHandler(MHandler):
# regex_str - Regular expression to test message against
#
# handle_callback - Function to call if messages matches regex
def __init__(self, regex_str, handle_callback):
MHandler.__init__(self, self.test_regex, handle_callback)
self.regex_str = regex_str
def test_regex(self, room, event):
# Test the message and see if it matches the regex
if event['type'] == "m.room.message":
if re.search(self.regex_str, event['content']['body']):
# The message matches the regex, return true
return True
return False
def triggers_on(self, trigger):
if trigger == self.regex_str:
return True
else:
return False