49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
from typing import NamedTuple, Optional
|
|
import json
|
|
|
|
|
|
class SwitchboardMessage(NamedTuple):
|
|
command: str
|
|
content: str
|
|
source_message_id: str
|
|
sender_nick: str
|
|
room_id: str
|
|
|
|
|
|
class BotResponse(NamedTuple):
|
|
content: str
|
|
source_message_id: str | None
|
|
room_id: str
|
|
as_reply: bool # requires source_message_id
|
|
as_markdown: bool
|
|
|
|
|
|
def unknown_cmd_help_reply(swm: SwitchboardMessage):
|
|
'''Returns the standard help text
|
|
because the user requested an unknown command'''
|
|
import commands
|
|
WRONG_CMD_TEXT = f'Hai sbagliato commando, lezzo! Prova uno di questi: {commands.commands}'
|
|
return BotResponse(content=WRONG_CMD_TEXT,
|
|
source_message_id=swm.source_message_id,
|
|
room_id=swm.room_id,
|
|
as_reply=True,
|
|
as_markdown=False)
|
|
|
|
|
|
def json_to_bot_response(json_str: str) -> Optional[BotResponse]:
|
|
import log
|
|
try:
|
|
data = json.loads(json_str)
|
|
|
|
content = data['content']
|
|
room_id = data['room_id']
|
|
|
|
as_reply = data.get('as_reply', False)
|
|
as_markdown = data.get('as_markdown', False)
|
|
source_message_id = data.get('source_message_id', None)
|
|
|
|
return BotResponse(content, source_message_id, room_id, as_reply, as_markdown)
|
|
|
|
except Exception as e:
|
|
log.error(f"Error parsing JSON or missing required fields: {str(e)}")
|
|
return None
|