Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from bundle
Use when building or fixing aiogram 3 bots that need aiogram_i18n with Fluent/FTL, topic-aware reply handling, Telegram quote and forward-origin context, and pr
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/ai-context-from-telegram-message.md
1# AI Context from Telegram Messages23This reference is about extracting useful Telegram context for AI features.45It is not a general persistence recipe.67## Goal89When you pass Telegram data into an LLM, do not dump the raw `Message` object.1011Instead, extract only the fields that change interpretation.1213Good AI context is:14- compact15- explicit16- structured17- stable across message types1819## The most useful signals2021### 1. Visible user text2223Usually the first field to extract:24- `message.text`25- `message.caption`26- normalized HTML or markdown text if your bot preserves formatting2728Use this as the main body of the AI input.2930### 2. Quote or reply context3132For modern Telegram clients, visible quoted text can arrive through `message.quote.text`.3334This matters when the user is replying to something and the quote changes meaning.3536Useful fields:37- quote text38- whether the message is a reply39- reply target message id when available40- thread or topic id when relevant4142### 3. Poll data4344Polls are useful AI context and should not be treated as empty messages.4546Useful fields:47- poll question48- option texts49- poll type50- whether multiple answers are allowed51- whether the poll is closed52- explanation text when present5354For AI formatting, keep only semantic fields, for example:55```text56Poll question: Which feature should we ship first?57Options: Notifications; Calendar sync; CSV export58Poll type: regular59Multiple answers allowed: false60Closed: false61```6263### 4. Sticker signals6465Stickers often have little text, but they still carry meaning.6667Useful fields:68- sticker emoji69- sticker set name if relevant to product logic70- whether it is animated or video7172For AI, pass sticker data only if it changes interpretation. In many bots, emoji alone is enough.7374### 5. Attachment summary7576Attachments are often important even when you do not pass the binary itself.7778Useful fields by type:79- photo: `has_photo`, caption, optional image OCR or vision output if your stack supports it80- video: `has_video`, caption, optional transcription or vision summary81- voice/audio: `has_voice` or `has_audio`, optional transcription82- document: file name, mime type, caption, optional extracted text83- animation/video note: media type plus caption or summary8485For AI, prefer a summary field over raw Telegram metadata:86```text87Attachment: voice message88Transcription: I need help connecting the bot to my channel.89```9091### 6. Forward origin9293Forward origin can change how AI should interpret a message.9495Useful fields:96- forward origin type97- forwarded user id or chat id when available98- forwarded display name when available99100Use `scripts/forward_origin_context.py` as a minimal typed example.101102### 7. Entities and links103104Entities are often more useful than raw text parsing.105106Useful signals:107- URLs108- text links109- mentions110- hashtags111- bot commands112- custom emoji ids when your bot resolves them to text-like meanings113114For AI, it is usually enough to pass a short entity summary:115```text116Entities: url=https://example.com, mention=@support, hashtag=#launch117```118119### 8. Sender and chat context120121Sometimes meaning depends on who sent the message.122123Useful fields:124- whether sender is `from_user` or `sender_chat`125- sender display name126- whether the message is a channel comment127- chat type: private, group, supergroup, channel128- whether the chat is a forum topic129130Do not pass internal ids to the model unless they affect reasoning.131132## What to pass for common AI tasks133134### Moderation135136Useful context:137- main text or caption138- quote text139- link and entity summary140- forward origin141- attachment type and transcription if any142143### Reply intent classification144145Useful context:146- main text147- quote text148- reply flag149- topic or thread flag150151### Assistant response generation152153Useful context:154- main text155- quote text156- poll summary if the message is a poll157- attachment summary or transcription158- sender or chat role only if it changes tone or permissions159160### Support bots161162Useful context:163- main text164- transcription for voice notes165- document extracted text when available166- quote text showing what the user is responding to167168## Recommended formatting for AI169170Two good universal formats are:171172### Option A: compact labeled text173174```text175Message text: How do I connect this?176Quote text: Click Bot Settings and then Add to Channel.177Chat type: supergroup178Is reply: true179Attachment: none180Entities: none181Forward origin: none182```183184This is simple and works well for prompts.185186### Option B: small structured object187188```json189{190"message_text": "How do I connect this?",191"quote_text": "Click Bot Settings and then Add to Channel.",192"chat_type": "supergroup",193"is_reply": true,194"attachment": null,195"entities": []196}197```198199This is better when a downstream service already expects structured inputs.200201## What not to pass202203Avoid sending large low-signal blobs such as:204- raw serialized `Message` objects205- every Telegram field by default206- binary metadata that does not affect interpretation207- internal ids that the model cannot use meaningfully208209## Minimal principle210211Only pass the Telegram context that changes model interpretation:212- visible message text213- quote text214- poll semantics215- attachment summary or transcription216- forward origin metadata217- reply or thread relation when it changes meaning218