Added author information to message

This commit is contained in:
Felipe M 2020-08-11 13:29:24 +02:00
parent e1c158bd6c
commit 562d7138c0
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
4 changed files with 19 additions and 4 deletions

View File

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

View File

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

View File

@ -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"],

View File

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