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.
This commit is contained in:
Steve Howell 2016-10-10 09:42:14 -07:00 committed by Tim Abbott
parent 2ed9f15ff7
commit 0dab69b76f
3 changed files with 17 additions and 3 deletions

View file

@ -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',

View file

@ -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

View file

@ -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)