From 0b35ed0a93b11cec959a598bde20110171b45470 Mon Sep 17 00:00:00 2001 From: PIG208 <359101898@qq.com> Date: Thu, 13 May 2021 19:36:45 +0800 Subject: [PATCH] terminal: Extract message server from the handler. This makes the user and the bot to share the message server when sending messages. As a result, the message id can be shared. And history messages sent by the user will be stored as well. --- zulip_bots/zulip_bots/simple_lib.py | 4 ++-- zulip_bots/zulip_bots/terminal.py | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/zulip_bots/zulip_bots/simple_lib.py b/zulip_bots/zulip_bots/simple_lib.py index 76046ff..c466f2f 100644 --- a/zulip_bots/zulip_bots/simple_lib.py +++ b/zulip_bots/zulip_bots/simple_lib.py @@ -40,10 +40,10 @@ class SimpleMessageServer: return dict(result='success', msg='', uri='https://server/user_uploads/{}'.format(uuid4())) class TerminalBotHandler: - def __init__(self, bot_config_file): + def __init__(self, bot_config_file, message_server): self.bot_config_file = bot_config_file self._storage = SimpleStorage() - self.message_server = SimpleMessageServer() + self.message_server = message_server @property def storage(self): diff --git a/zulip_bots/zulip_bots/terminal.py b/zulip_bots/zulip_bots/terminal.py index 79ba796..6c3ed94 100644 --- a/zulip_bots/zulip_bots/terminal.py +++ b/zulip_bots/zulip_bots/terminal.py @@ -4,7 +4,7 @@ import sys import argparse from zulip_bots.finder import import_module_from_source, resolve_bot_path -from zulip_bots.simple_lib import TerminalBotHandler +from zulip_bots.simple_lib import SimpleMessageServer, TerminalBotHandler current_dir = os.path.dirname(os.path.abspath(__file__)) @@ -49,7 +49,8 @@ def main(): print("This module does not appear to have a bot handler_class specified.") sys.exit(1) - bot_handler = TerminalBotHandler(args.bot_config_file) + message_server = SimpleMessageServer() + bot_handler = TerminalBotHandler(args.bot_config_file, message_server) if hasattr(message_handler, 'initialize') and callable(message_handler.initialize): message_handler.initialize(bot_handler) @@ -59,11 +60,12 @@ def main(): while True: content = input('Enter your message: ') - message = dict( + message = message_server.send(dict( content=content, sender_email=sender_email, display_recipient=sender_email, - ) + )) + message_handler.handle_message( message=message, bot_handler=bot_handler,