OpenAI GPT, driven by Telegram
Project description
TeLLMgramBot
The basic goal of this project is to create a bridge between a Telegram Bot and a Large Langage Model (LLM), like OpenAI's GPT 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 Telgram group, the bot must be adminstrator 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 another GPT model, preferably GPT-5 or GPT-4o, 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 boadloads 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.
Directories
When initializing TeLLMgramBot, the following directories get created:
configs- Contains bot configuration files.commands.txt- Users can type
/helpinteracting with the to see this file's text get displayed in Telegram.
- Users can type
config.yaml(can be a different name)- This file sets main OpenAI parameters like naming and GPT models to process.
- The parameter
url_modelis to read URL content, different thanchat_modelthat the bot normally uses to interact with the user. - An empty
token_limitwould do the maximum amount of tokens supported by thechat_model(e.g. 128000 forgpt-4o-mini).
tokenGPT.yaml- This important YAML file contains token size parameters for supported OpenAI models.
- If the first time,
gpt-5,gpt-5-mini,gpt-5-nano,gpt-4o, andgpt-4o-miniget populated, but the user can specify more models with token size parameters as needed.
prompts- Contains prompt files for how the bot interacts with any user.test_personality.prmpt- This is a sample prompt file as a basis to test this library.
- The user can create more prompt files as needed for different personalities. See OpenAI Playground to test some ideas.
url_analysis.prmpt- This is a crucial prompt file to analyze URL content in brackets
[]in a different model (such asgpt-4oorgpt-4.1).
- This is a crucial prompt file to analyze URL content 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, three API keys are required:
- OpenAI - Drives the actual GPT AI.
- Telegram - Offers a Bot API through BotFather for the messaging platform.
- VirusTotal - 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 that can be pre-loaded with the three API keys respectively:
TELLMGRAMBOT_OPENAI_API_KEYTELLMGRAMBOT_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['GPTKey']
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 three files by the base path during execution or a specified environment variable TELLMGRAMBOT_KEYS_PATH. By default, three files are created for the user to input each API key:
openai.keytelegram.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. The bot communicates with OpenAI via the Responses API, which replaces the older Chat Completions endpoint.
- 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:
telegram_bot = TeLLMgramBot.TelegramBot( bot_username = <Bot username like 'friendly_bot'>, bot_owner = <Bot owner's Telegram username>, bot_name = <Bot name like 'Friendly Bot'>, bot_nickname = <Bot nickname like 'Botty'>, bot_initials = <Bot initials like 'FB'>, chat_model = <Conversation model like 'gpt-4o-mini'>, url_model = <URL analysis model like 'gpt-4o'>, 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. - Typing
/helpshows all available commands reported by theconfigs/commands.txtfile. - Only as owner, type
/startdirectly to the bot to initiate user conversations.
Resources
- GitHub repository python-telegram-bot has guides to create a Telegram bot.
- For more information on OpenAI models like
gpt-4oand token limits, see the following: - OpenAI Playground is a great place to test out prompts and responses.
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.1.1.tar.gz.
File metadata
- Download URL: tellmgrambot-2.1.1.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45fc29d20010f73f463e8acaaa3ae92bd9eec46b3078d0aff78e254007801767
|
|
| MD5 |
ebc6ad9b9d43cc330367a7700052f08e
|
|
| BLAKE2b-256 |
9949fa26dda49993bb07f344de4139781d2f26d864e95dc04adfdd4536f8851e
|
File details
Details for the file tellmgrambot-2.1.1-py3-none-any.whl.
File metadata
- Download URL: tellmgrambot-2.1.1-py3-none-any.whl
- Upload date:
- Size: 23.6 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 |
6f9d927ded59c3850f2feea160b2dcea5366a7d992d5b8347b55531bdc8b1886
|
|
| MD5 |
5b25ca278383a97580b6549cbe445c76
|
|
| BLAKE2b-256 |
7cb1c8ddc8f29370a4ba43f42a0c9da5c1c56968ee0d9e0939cdd69d7df7882d
|