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
49
adapter/telegram/handlers/confirm.py
Normal file
49
adapter/telegram/handlers/confirm.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# adapter/telegram/handlers/confirm.py
|
||||
from __future__ import annotations
|
||||
|
||||
from aiogram import F, Router
|
||||
from aiogram.fsm.context import FSMContext
|
||||
from aiogram.types import CallbackQuery
|
||||
|
||||
from core.handler import EventDispatcher
|
||||
from core.protocol import IncomingCallback
|
||||
|
||||
router = Router(name="confirm")
|
||||
|
||||
|
||||
@router.callback_query(F.data.startswith("confirm:"))
|
||||
async def handle_confirm(
|
||||
callback: CallbackQuery,
|
||||
state: FSMContext,
|
||||
dispatcher: EventDispatcher,
|
||||
) -> None:
|
||||
parts = callback.data.split(":", 2)
|
||||
_, decision, action_id = parts # "yes" or "no"
|
||||
|
||||
data = await state.get_data()
|
||||
chat_id = data.get("active_chat_id", "")
|
||||
chat_name = data.get("active_chat_name", "Чат")
|
||||
|
||||
from adapter.telegram import db as tgdb
|
||||
tg_id = callback.from_user.id
|
||||
tg_user = tgdb.get_or_create_tg_user(tg_id, str(tg_id), callback.from_user.full_name)
|
||||
platform_user_id = tg_user.get("platform_user_id", str(tg_id))
|
||||
|
||||
incoming = IncomingCallback(
|
||||
user_id=platform_user_id,
|
||||
platform="telegram",
|
||||
chat_id=chat_id,
|
||||
action="confirm" if decision == "yes" else "cancel",
|
||||
payload={"action_id": action_id},
|
||||
)
|
||||
events = await dispatcher.dispatch(incoming)
|
||||
|
||||
await callback.message.edit_reply_markup(reply_markup=None)
|
||||
|
||||
for event in events:
|
||||
from core.protocol import OutgoingMessage, OutgoingUI
|
||||
from adapter.telegram.converter import format_outgoing
|
||||
if isinstance(event, (OutgoingMessage, OutgoingUI)):
|
||||
await callback.message.answer(format_outgoing(chat_name, event))
|
||||
|
||||
await callback.answer()
|
||||
Loading…
Add table
Add a link
Reference in a new issue