From 562d7138c05401c3146b8265d9c07620c91fba6b Mon Sep 17 00:00:00 2001 From: Felipe M Date: Tue, 11 Aug 2020 13:29:24 +0200 Subject: [PATCH] Added author information to message --- butterrobot/app.py | 17 +++++++++++++---- butterrobot/objects.py | 2 ++ butterrobot/platforms/slack.py | 2 ++ butterrobot/platforms/telegram.py | 2 ++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/butterrobot/app.py b/butterrobot/app.py index 2f04ea9..8487949 100644 --- a/butterrobot/app.py +++ b/butterrobot/app.py @@ -17,7 +17,9 @@ logger = structlog.get_logger(__name__) app = Quart(__name__) available_platforms = {} plugins = get_available_plugins() -enabled_plugins = [plugin for plugin_name, plugin in plugins.items() if plugin_name in ENABLED_PLUGINS] +enabled_plugins = [ + plugin for plugin_name, plugin in plugins.items() if plugin_name in ENABLED_PLUGINS +] @app.before_serving @@ -39,11 +41,18 @@ async def incoming_platform_message_view(platform, path=None): return {"error": "Unknown platform"}, 400 try: - message = await available_platforms[platform].parse_incoming_message(request=request) + message = await available_platforms[platform].parse_incoming_message( + request=request + ) except Platform.PlatformAuthResponse as response: return response.data, response.status_code except Exception as error: - logger.error(f"Error parsing message", platform=platform, error=error, traceback=traceback.format_exc()) + logger.error( + f"Error parsing message", + platform=platform, + error=error, + traceback=traceback.format_exc(), + ) return {"error": str(error)}, 400 if not message: @@ -53,7 +62,7 @@ async def incoming_platform_message_view(platform, path=None): if result := await plugin.on_message(message): if isinstance(result, Message): result = [result] - + for out_message in result: await available_platforms[platform].methods.send_message(out_message) diff --git a/butterrobot/objects.py b/butterrobot/objects.py index 0c734ee..8ef22ca 100644 --- a/butterrobot/objects.py +++ b/butterrobot/objects.py @@ -7,6 +7,8 @@ from typing import Text, Optional class Message: text: Text chat: Text + author: Text + is_bot: bool = False date: Optional[datetime] = None id: Optional[Text] = None reply_to: Optional[Text] = None diff --git a/butterrobot/platforms/slack.py b/butterrobot/platforms/slack.py index 71d9d7c..f6d1865 100644 --- a/butterrobot/platforms/slack.py +++ b/butterrobot/platforms/slack.py @@ -63,6 +63,8 @@ class SlackPlatform(Platform): return Message( id=data["event"].get("thread_ts", data["event"]["ts"]), + author=data["event"]["user"], + is_bot="bot_id" in data["event"], date=datetime.fromtimestamp(int(float(data["event"]["event_ts"]))), text=data["event"]["text"], chat=data["event"]["channel"], diff --git a/butterrobot/platforms/telegram.py b/butterrobot/platforms/telegram.py index 7506861..602fd33 100644 --- a/butterrobot/platforms/telegram.py +++ b/butterrobot/platforms/telegram.py @@ -61,6 +61,8 @@ class TelegramPlatform(Platform): id=request_data["message"]["message_id"], date=datetime.fromtimestamp(request_data["message"]["date"]), text=str(request_data["message"]["text"]), + is_bot=request_data["message"]["from"]["is_bot"], + author=request_data["message"]["from"]["id"], chat=str(request_data["message"]["chat"]["id"]), raw=request_data, )