From 0dab69b76f13931c28da3123ee66be8fb80d1196 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 10 Oct 2016 09:42:14 -0700 Subject: [PATCH] Add StateHandler to contrib_bots/run.py. This allows bots to be stateful. It doesn't handle persistence after the bot shuts down, but it does store state between invocations of handle_message. --- contrib_bots/lib/followup.py | 2 +- contrib_bots/lib/help.py | 2 +- contrib_bots/run.py | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/contrib_bots/lib/followup.py b/contrib_bots/lib/followup.py index 081eebd..9042ab4 100644 --- a/contrib_bots/lib/followup.py +++ b/contrib_bots/lib/followup.py @@ -37,7 +37,7 @@ class FollowupHandler(object): return is_follow_up - def handle_message(self, message, client): + def handle_message(self, message, client, state_handler): original_content = message['content'] original_sender = message['sender_email'] new_content = original_content.replace('@followup', diff --git a/contrib_bots/lib/help.py b/contrib_bots/lib/help.py index a8bdabd..ad5982c 100644 --- a/contrib_bots/lib/help.py +++ b/contrib_bots/lib/help.py @@ -23,7 +23,7 @@ class HelpHandler(object): return True - def handle_message(self, message, client): + def handle_message(self, message, client, state_handler): help_content = ''' Info on Zulip can be found here: https://github.com/zulip/zulip diff --git a/contrib_bots/run.py b/contrib_bots/run.py index 7ab1508..a8a649b 100644 --- a/contrib_bots/run.py +++ b/contrib_bots/run.py @@ -42,6 +42,18 @@ def run_message_handler_for_bot(lib_module, quiet, config_file): message_handler = lib_module.handler_class() + class StateHandler(object): + def __init__(self): + self.state = None + + def set_state(self, state): + self.state = state + + def get_state(self): + return self.state + + state_handler = StateHandler() + if not quiet: print(message_handler.usage()) @@ -50,7 +62,9 @@ def run_message_handler_for_bot(lib_module, quiet, config_file): if message_handler.triage_message(message=message): message_handler.handle_message( message=message, - client=restricted_client) + client=restricted_client, + state_handler=state_handler + ) logging.info('starting message handling...') client.call_on_each_message(handle_message)