LLM-powered Telegram bot (OpenAI + Anthropic)
Project description
TeLLMgramBot
The basic goal of this project is to create a bridge between a Telegram Bot and a Large Language Model (LLM), supporting both OpenAI's GPT models and Anthropic's Claude models.
- To use this library, you must have a Telegram account with a user name, not just a phone number. If you don't have one, create one online.
- If added to a Telegram group, the bot must be administrator in order to respond to a user calling out its name, initials, or nickname.
Telegram Bot + LLM Encapsulation
- The Telegram interface handles special commands, especially on some basic "chatty" prompts and responses that don't require LLM, like "Hello".
- The more dynamic conversation gets handed off to the LLM to manage prompts and responses, and Telegram acts as the interaction broker.
- Pass the URL in [square brackets] and mention how the bot should interpret it.
- Example: "What do you think of this article? [https://some_site/article]"
- This uses a separate model (configurable via
url_model) to support more URL content with its higher token limit.
- Tokens are used to measure the length of all conversation messages between the Telegram bot assistant and the user. This is useful to:
- Ensure the length does not go over the model limit. If it does, prune oldest messages to fit within the limit.
- Remember 50% of the past conversations when starting up TeLLMgramBot again.
- Users can also clear their conversation history for privacy.
Why Telegram?
Using Telegram as the interface not only solves "exposing" the interface, but gives you boatloads of interactivity over a standard Command Line interface, or trying to create a website with input boxes and submit buttons to try to handle everything:
- Telegram already lets you paste in verbose, multiline messages.
- Telegram already lets you paste in pictures, videos, links, etc.
- Telegram already lets you react with emojis, stickers, etc.
Supported LLM Providers
TeLLMgramBot selects the LLM provider automatically based on the model name:
| Model prefix | Provider | Example models |
|---|---|---|
gpt- |
OpenAI | gpt-4o, gpt-4o-mini, gpt-5-mini |
claude- |
Anthropic | claude-sonnet-4-6, claude-haiku-4-5 |
Simply set chat_model (and optionally url_model) in your config.yaml to any supported model and supply the corresponding API key — no other changes needed.
Directories
When initializing TeLLMgramBot, the following directories get created:
configs- Contains bot configuration files.config.yaml(can be a different name)- This file sets main bot parameters like naming and the LLM models to use.
chat_model— the model used for normal conversation (e.g.gpt-5-miniorclaude-sonnet-4-6).url_model— the model used to read and summarize URL content, can differ fromchat_model.- An empty
token_limitwill use the maximum tokens supported by thechat_model.
tokenGPT.yaml- Contains token size parameters for all supported models.
- On first run, GPT and Claude model families are pre-populated. Additional models can be added manually.
prompts- Contains prompt files for how the bot interacts with any user.test_personality.prmpt(can be a different name)- A sample prompt file as a basis to test this library.
- The user can create more prompt files as needed for different personalities.
url_analysis.prmpt- Prompt template used to analyze URL content passed in brackets
[].
- Prompt template used to analyze URL content passed in brackets
errorlogs- Contains a
tellmgrambot_error.logfile to investigate if there are problems during the interaction. - User will also get notified to contact the owner.
- Contains a
sessionlogs- Every conversation is stored between the Telegram bot assistant and each user.
- If a user types
/forget, any session log files between the bot and the user will all be removed.
Environment Variables
TeLLMgramBot also creates or utilizes the following environment variables that can be pre-loaded especially in other operating systems like Home Assistant, having different configurations for persistent storage:
TELLMGRAMBOT_CONFIGS_PATHTELLMGRAMBOT_PROMPTS_PATHTELLMGRAMBOT_ERRORLOGS_PATHTELLMGRAMBOT_SESSIONLOGS_PATH
If neither of these are defined, the initialization would use the top-level execution run directory.
API Keys
To operate TeLLMgramBot, the following API keys are required:
- OpenAI — required when using a
gpt-*model. - Anthropic — required when using a
claude-*model. - Telegram — always required; offers a Bot API through BotFather.
- VirusTotal — always required; performs safety checks on URLs.
There are two ways to populate each API key: environment variables or .key files.
Environment Variables
TeLLMgramBot uses the following environment variables for API keys:
TELLMGRAMBOT_OPENAI_API_KEY(OpenAI models)TELLMGRAMBOT_ANTHROPIC_API_KEY(Anthropic models)TELLMGRAMBOT_TELEGRAM_API_KEYTELLMGRAMBOT_VIRUSTOTAL_API_KEY
During spin-up time, a user can call out os.environ[env_var] to set those variables, like the following example:
my_keys = Some_Vault_Fetch_Function()
os.environ['TELLMGRAMBOT_OPENAI_API_KEY'] = my_keys['OpenAIKey']
os.environ['TELLMGRAMBOT_ANTHROPIC_API_KEY'] = my_keys['AnthropicKey']
os.environ['TELLMGRAMBOT_TELEGRAM_API_KEY'] = my_keys['BotFatherToken']
os.environ['TELLMGRAMBOT_VIRUSTOTAL_API_KEY'] = my_keys['VirusTotalToken']
This means the user can implement whatever key vault they want to fetch the keys at runtime, without needing files stored in the directory.
API Key Files
The other route is to create files by the base path during execution or a specified environment variable TELLMGRAMBOT_KEYS_PATH. By default, files are created for the user to input each API key:
openai.keyanthropic.key(planned — env var only for now; see Phase 3)telegram.keyvirustotal.key
Each file with the associated API key will update its respective environment variable if not defined.
Bot Setup
This library includes an example script test_local.py, which uses files from the folders configs and prompts for TeLLMgramBot to process.
- Ensure the previous sections are followed with the proper API keys and your Telegram bot set.
- Install this library via PIP (
pip install TeLLMgramBot) and then import into your project. - Instantiate the bot by passing in various configuration pieces needed below.
Note the Telegram bot's full name and username auto-populate before startup.
telegram_bot = TeLLMgramBot.TelegramBot( bot_owner = <Bot owner's Telegram username>, bot_nickname = <Bot nickname like 'Botty'>, bot_initials = <Bot initials like 'FB'>, chat_model = <Conversation model like 'gpt-4o-mini' or 'claude-sonnet-4-6'>, url_model = <URL analysis model like 'gpt-4o' or 'claude-haiku-4-5'>, token_limit = <Maximum token count set, by default chat_model max>, persona_temp = <LLM factual to creative value [0-2], by default 1.0>, persona_prompt = <System prompt summarizing bot personality> ) - Turn on TeLLMgramBot by calling:
Once you seetelegram_bot.start_polling()TeLLMgramBot polling..., the bot is online in Telegram. - Converse! Type
/helpfor all available commands.
Resources
- GitHub repository python-telegram-bot has guides to create a Telegram bot.
- For more information on OpenAI models and token limits:
- For more information on Anthropic Claude models:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tellmgrambot-2.4.3.tar.gz.
File metadata
- Download URL: tellmgrambot-2.4.3.tar.gz
- Upload date:
- Size: 27.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76820873edf1a613c1c40367a9ec32e13cc71f6fa78765d2aaafa1254971bd4d
|
|
| MD5 |
b428d86318750bb7b3d6824f491b2f64
|
|
| BLAKE2b-256 |
81b532d40f39bb65efc18c1a935d577b91655173f2df106bc7ac196acfebdee9
|
File details
Details for the file tellmgrambot-2.4.3-py3-none-any.whl.
File metadata
- Download URL: tellmgrambot-2.4.3-py3-none-any.whl
- Upload date:
- Size: 30.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c922563f5fde93a3de817c9dab376044161f81e7a0f192ec914d421518200bdb
|
|
| MD5 |
eb0a419b9482c95b570180e55191930b
|
|
| BLAKE2b-256 |
3e5f891a2a7ce19f61bc288758400f0be8acfbe47ae5472546af065f5b2fc336
|