Files
beavergram/backend/src/userbot/handlers/raw/read_contents.py
T

53 lines
1.8 KiB
Python

from datetime import UTC, datetime
from pyrogram import raw, utils
from userbot import PyroClient
from userbot.modules.capture.chat_meta import meta_from_chat_id
from userbot.modules.raw_json import raw_json
from userbot.modules.read_receipts import repository as receipts
from userbot.modules.stt import repository
from userbot.modules.stt.gate import safe_transcribe
HANDLES = (
raw.types.UpdateReadMessagesContents,
raw.types.UpdateChannelReadMessagesContents,
)
async def handle(
client: PyroClient, update: raw.base.Update, _users: dict, _chats: dict
) -> None:
ctx = client.capture
if ctx is None:
return
if isinstance(update, raw.types.UpdateChannelReadMessagesContents):
chat_id = utils.get_peer_id(raw.types.PeerChannel(channel_id=update.channel_id))
candidates = await repository.voice_reads(
ctx.pool, ctx.account_id, update.messages, chat_id=chat_id
)
elif isinstance(update, raw.types.UpdateReadMessagesContents):
candidates = await repository.voice_reads(
ctx.pool, ctx.account_id, update.messages
)
else:
return
self_id = client.me.id if client.me else None
for cand_chat_id, message_id, sender_id, untranscribed in candidates:
if sender_id is not None and sender_id == self_id:
await receipts.insert_listened(
ctx.pool,
ctx.account_id,
cand_chat_id,
cand_chat_id,
datetime.now(UTC),
message_id,
raw_json(update),
)
continue
if not untranscribed:
continue
meta = meta_from_chat_id(cand_chat_id, ctx.contacts.ids)
if ctx.resolve(meta).stt:
await safe_transcribe(client, ctx, cand_chat_id, message_id)