A simple Telegram bot wrapping OpenAI API text models (like ChatGPT), build with python-telegram-bot
!
Just start a conversation and all messages in the chat will be used as inputs for ChatGPT.
Conversation/context is not stored permanently and will be removed when the bot is restarted.
This bot was built with Python 3.11
, python-telegram-bot
and openai-python
.
Full list of Python requirements is in the requirements.txt
file, you can use it to install all of them.
Configuration is done through a .env
file. You can copy example file .env.example
as .env
and fill required parameters.
cp .env.example .env
Only required parameter is a bot token.
You can also restrict who can access the bot via ALLOWED_USERNAMES
.
You can specify multiple usernames delimited by space.
If you don't want to restrict the bot at all you can remove this parameter or leave it empty.
BOT_TOKEN='<your secret bot token>'
ALLOWED_USERNAMES='myusername friendusername'
One required parameter is API key.
OPENAI_TOKEN='<your secret API key>'
Through .env
you can also configure other parameters:
OPENAI_MODEL
- which model to use (gpt-3.5-turbo is used by default)OPENAI_SYSTEM_MESSAGE
- system messageOPENAI_CONTEXT_LIMIT
- how many messages will be kept in the context aside from prompt, all messages will be kept if emptyOPENAI_INITIAL_MESSAGE
- additional message added after system message to all conversations, can be empty for no additional messages
Note that gpt-3.5-turbo
doesn't pay strong attention to system message, so changing it might not provide significant changes to responses.
You can use OPENAI_INITIAL_MESSAGE
to tweak initial behaviour of the model.
OPENAI_MODEL='gpt-3.5-turbo'
OPENAI_SYSTEM_MESSAGE='You are a helpful assistant.'
OPENAI_CONTEXT_LIMIT=1000
OPENAI_INITIAL_MESSAGE='You are a helpful assistant acting like 18th century butler,'
/start
- prints initial message returned from the model for just system message and optional initial message, doesn't impact conversation context/reset
- resets current conversation and removes all context, other than system message/promptset
- set new custom prompt, changes both system message and custom initial message (overwrites bothOPENAI_SYSTEM_MESSAGE
andOPENAI_INITIAL_MESSAGE
)/promptreset
- restore prompt to default/promptget
- get custom prompt, won't respond with default one to avoid leaking configuration to users/promptremove
- force-remove any prompts from the conversation, including one defined in configuration files/cancel
- cancels ongoing operation, applies only to/promptset
You can run the bot from the source code directly, or in a Docker container.
- Create a Telegram bot via BotFather
- Create OpenAI API key
- Install all packages from
requirements.txt
- Fill
.env
file - Run
main.py
file with Python
- Create a Telegram bot via BotFather
- Create OpenAI API key
- Fill
.env
file - Run
docker compose up -d --build
in terminal
Note that .env
file is used only for loading environment variables into Docker container through compose.
The file itself isn't added to the container.