feat: implement adapter/telegram/ with aiogram 3.x
Virtual DM chats, FSM (idle/waiting_response/settings states), SQLite local DB for tg_users+chats, converter, keyboards, and handlers for /start, /new, /chats, /settings, confirm callbacks.
This commit is contained in:
parent
a3449fc864
commit
9c555261b3
15 changed files with 791 additions and 0 deletions
50
adapter/telegram/converter.py
Normal file
50
adapter/telegram/converter.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
# adapter/telegram/converter.py
|
||||
from __future__ import annotations
|
||||
|
||||
from aiogram.types import Message
|
||||
|
||||
from core.protocol import Attachment, IncomingMessage, OutgoingEvent, OutgoingMessage, OutgoingUI
|
||||
|
||||
|
||||
def from_message(message: Message, chat_id: str) -> IncomingMessage:
|
||||
return IncomingMessage(
|
||||
user_id=str(message.from_user.id),
|
||||
chat_id=chat_id,
|
||||
text=message.text or message.caption or "",
|
||||
attachments=_extract_attachments(message),
|
||||
platform="telegram",
|
||||
)
|
||||
|
||||
|
||||
def _extract_attachments(message: Message) -> list[Attachment]:
|
||||
attachments: list[Attachment] = []
|
||||
if message.photo:
|
||||
file = message.photo[-1]
|
||||
attachments.append(Attachment(
|
||||
type="image",
|
||||
url=f"tg://file/{file.file_id}",
|
||||
mime_type="image/jpeg",
|
||||
))
|
||||
if message.document:
|
||||
attachments.append(Attachment(
|
||||
type="document",
|
||||
url=f"tg://file/{message.document.file_id}",
|
||||
mime_type=message.document.mime_type or "application/octet-stream",
|
||||
filename=message.document.file_name,
|
||||
))
|
||||
if message.voice:
|
||||
attachments.append(Attachment(
|
||||
type="audio",
|
||||
url=f"tg://file/{message.voice.file_id}",
|
||||
mime_type="audio/ogg",
|
||||
))
|
||||
return attachments
|
||||
|
||||
|
||||
def format_outgoing(chat_name: str, event: OutgoingEvent) -> str:
|
||||
prefix = f"[{chat_name}] "
|
||||
if isinstance(event, OutgoingMessage):
|
||||
return prefix + event.text
|
||||
if isinstance(event, OutgoingUI):
|
||||
return prefix + event.text
|
||||
return prefix + str(event)
|
||||
Loading…
Add table
Add a link
Reference in a new issue