AI Context from Telegram Messages
This reference is about extracting useful Telegram context for AI features.
It is not a general persistence recipe.
Goal
When you pass Telegram data into an LLM, do not dump the raw Message object.
Instead, extract only the fields that change interpretation.
Good AI context is:
- compact
- explicit
- structured
- stable across message types
The most useful signals
1. Visible user text
Usually the first field to extract:
message.textmessage.caption- normalized HTML or markdown text if your bot preserves formatting
Use this as the main body of the AI input.
2. Quote or reply context
For modern Telegram clients, visible quoted text can arrive through message.quote.text.
This matters when the user is replying to something and the quote changes meaning.
Useful fields:
- quote text
- whether the message is a reply
- reply target message id when available
- thread or topic id when relevant
3. Poll data
Polls are useful AI context and should not be treated as empty messages.
Useful fields:
- poll question
- option texts
- poll type
- whether multiple answers are allowed
- whether the poll is closed
- explanation text when present
For AI formatting, keep only semantic fields, for example:
Poll question: Which feature should we ship first?
Options: Notifications; Calendar sync; CSV export
Poll type: regular
Multiple answers allowed: false
Closed: false4. Sticker signals
Stickers often have little text, but they still carry meaning.
Useful fields:
- sticker emoji
- sticker set name if relevant to product logic
- whether it is animated or video
For AI, pass sticker data only if it changes interpretation. In many bots, emoji alone is enough.
5. Attachment summary
Attachments are often important even when you do not pass the binary itself.
Useful fields by type:
- photo:
has_photo, caption, optional image OCR or vision output if your stack supports it - video:
has_video, caption, optional transcription or vision summary - voice/audio:
has_voiceorhas_audio, optional transcription - document: file name, mime type, caption, optional extracted text
- animation/video note: media type plus caption or summary
For AI, prefer a summary field over raw Telegram metadata:
Attachment: voice message
Transcription: I need help connecting the bot to my channel.6. Forward origin
Forward origin can change how AI should interpret a message.
Useful fields:
- forward origin type
- forwarded user id or chat id when available
- forwarded display name when available
Use scripts/forward_origin_context.py as a minimal typed example.
7. Entities and links
Entities are often more useful than raw text parsing.
Useful signals:
- URLs
- text links
- mentions
- hashtags
- bot commands
- custom emoji ids when your bot resolves them to text-like meanings
For AI, it is usually enough to pass a short entity summary:
Entities: url=https://example.com, mention=@support, hashtag=#launch8. Sender and chat context
Sometimes meaning depends on who sent the message.
Useful fields:
- whether sender is
from_userorsender_chat - sender display name
- whether the message is a channel comment
- chat type: private, group, supergroup, channel
- whether the chat is a forum topic
Do not pass internal ids to the model unless they affect reasoning.
What to pass for common AI tasks
Moderation
Useful context:
- main text or caption
- quote text
- link and entity summary
- forward origin
- attachment type and transcription if any
Reply intent classification
Useful context:
- main text
- quote text
- reply flag
- topic or thread flag
Assistant response generation
Useful context:
- main text
- quote text
- poll summary if the message is a poll
- attachment summary or transcription
- sender or chat role only if it changes tone or permissions
Support bots
Useful context:
- main text
- transcription for voice notes
- document extracted text when available
- quote text showing what the user is responding to
Recommended formatting for AI
Two good universal formats are:
Option A: compact labeled text
Message text: How do I connect this?
Quote text: Click Bot Settings and then Add to Channel.
Chat type: supergroup
Is reply: true
Attachment: none
Entities: none
Forward origin: noneThis is simple and works well for prompts.
Option B: small structured object
{
"message_text": "How do I connect this?",
"quote_text": "Click Bot Settings and then Add to Channel.",
"chat_type": "supergroup",
"is_reply": true,
"attachment": null,
"entities": []
}This is better when a downstream service already expects structured inputs.
What not to pass
Avoid sending large low-signal blobs such as:
- raw serialized
Messageobjects - every Telegram field by default
- binary metadata that does not affect interpretation
- internal ids that the model cannot use meaningfully
Minimal principle
Only pass the Telegram context that changes model interpretation:
- visible message text
- quote text
- poll semantics
- attachment summary or transcription
- forward origin metadata
- reply or thread relation when it changes meaning