In-Chat Message Retention
Relevant source files
The following files were used as context for generating this wiki page:
In-Chat Message Retention refers to the mechanism that stores recent Telegram messages within the bot's memory to facilitate text summarization. This system allows the bot to provide summaries of recent chat history when a user invokes the /summary command without providing explicit text.
The retention system utilizes a volatile, in-memory data structure that tracks messages on a per-chat basis. It operates within pre-defined limits to manage memory consumption while ensuring enough context is available for the summarization logic.
Sources: README.md:9-11, bot.py:32-34
Data Structure and Logic
The bot stores messages using a defaultdict where each key is a unique chat_id. The values are deque objects, which act as double-ended queues with a fixed maximum length. This structure ensures that as new messages arrive, the oldest messages are automatically evicted once the limit is reached.
Storage Components
| Component | Type | Description |
|---|---|---|
chat_messages | dict[int, deque[str]] | Maps chat IDs to a queue of message strings. |
MAX_STORED_MESSAGES | int | Defines the capacity of the deque for each chat. |
process_message | function | Handles incoming updates and appends text to the storage. |
Sources: bot.py:27, bot.py:32-33, bot.py:110-111
Message Processing Flow
The following diagram illustrates how the bot evaluates incoming messages and determines whether to store them or trigger a summary based on the current retention state.
The logic differentiates between commands and standard message content to maintain a clean history for summarization.
Sources: bot.py:92-111
Configuration and Constraints
The retention behavior is controlled via environment variables. These settings define the depth of the bot's memory and the resulting summary length.
| Environment Variable | Default | Description |
|---|---|---|
MAX_STORED_MESSAGES | 100 | The maximum number of recent messages kept per chat. |
SUMMARY_SENTENCES | 5 | The number of sentences extracted from stored messages. |
Sources: README.md:46-47, bot.py:27-28
Implementation Detail
The retention logic is implemented within the process_message function. When a standard message is received (not starting with a command), the bot appends the text to the specific chat_id queue.
# From bot.py:110-111
chat_messages[chat_id].append(text)
When /summary is called without arguments, the bot joins the stored strings into a single block of text for processing:
# From bot.py:106-107
else:
source_text = " ".join(chat_messages[chat_id])
Sources: bot.py:106-111
Execution Lifecycle
The message retention system exists only during the bot's runtime. Because the data is stored in a Python deque within the script's memory, restarting the process clears all retained messages.
This sequence demonstrates the volatile nature of the current message retention implementation.
Sources: bot.py:32-33, bot.py:113-124, README.md:72-74
Summary
In-Chat Message Retention provides the necessary context for the bot's local summarizer by maintaining a rolling window of the last 100 messages per chat. This system is memory-efficient due to the use of fixed-length deques but does not persist data across bot restarts or deployments.
Sources: bot.py:27-33, README.md:9-11