From 36560c9c3b414d24ecaff901dc766ade28fad228 Mon Sep 17 00:00:00 2001 From: Stavros Korokithakis Date: Tue, 11 Dec 2018 19:00:07 +0200 Subject: Add `order` argument and match continuation --- signald/main.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'signald/main.py') diff --git a/signald/main.py b/signald/main.py index da90d40..30231c7 100644 --- a/signald/main.py +++ b/signald/main.py @@ -141,7 +141,7 @@ class Signal: payload = {"type": "send", "username": self.username, "recipientNumber": recipient, "messageBody": text} self._send_command(payload, block) - def chat_handler(self, regex): + def chat_handler(self, regex, order=100): """ A decorator that registers a chat handler function with a regex. """ @@ -149,7 +149,9 @@ class Signal: regex = re.compile(regex, re.I) def decorator(func): - self._chat_handlers.append((regex, func)) + self._chat_handlers.append((order, regex, func)) + # Use only the first value to sort so that declaration order doesn't change. + self._chat_handlers.sort(key=lambda x: x[0]) return func return decorator @@ -162,7 +164,7 @@ class Signal: if not message.text: continue - for regex, func in self._chat_handlers: + for _, regex, func in self._chat_handlers: match = re.search(regex, message.text) if not match: continue @@ -171,4 +173,14 @@ class Signal: reply = func(message, match) except: # noqa - We don't care why this failed. continue + + if isinstance(reply, tuple): + stop, reply = reply + else: + stop = True + self.send_message(recipient=message.source, text=reply) + + if stop: + # We don't want to continue matching things. + break -- cgit v1.2.3-54-g00ecf