Telegram bot backed by OpenAI Chat Completions and Image generations API.
/chat
: start a chat conversation/draw
: start a drawing conversation/exit
: exit current conversation
/chat <question>
: ask a question/draw <subject>
: generate an image
Conversations are not available in group chats, so the bot does not remember previously asked questions, while in private /chat
conversations it remembers previous messages until you /exit
the conversation.
To run the bot you need the following requirements:
Contact the @BotFather on Telegram, send the /newbot
command and follow the steps.
- Create an account on platform.openai.com
- Navigate to the Billing page and add credit to balance (minimum 5$ is required)
- Navigate to the API key page and create a new secret key
Environment variables can be read from the operating system or from a .env
file in the current directory. You can use the .env.example
file as a starting point to create your own .env
file. Some variables are optional, while others are required for the bot to start properly.
Variable | Description | Required |
---|---|---|
TELEGRAM_TOKEN | The Telegram bot token | true |
OPENAI_API_KEY | The OpenAI API key | true |
CHAT_PROMPT_PATH | Relative file path for a custom chat system prompt, defaults to prompt.txt |
false |
WHITELISTED_GROUP_IDS | Comma separated ids of groups whose members are allowed to chat with the bot | false |
WHITELISTED_USER_IDS | Comma separated ids of users allowed to chat with the bot | false |
Before running the bot for the first time you need to install the required dependencies:
npm install
Create a .env
file or set the required environment variables in the operating system. If you want to create the .env
file you can use the .env.example
file as an example. If you want to use OS environment variables you can run the following commands on UNIX systems like Mac or Linux, replacing your-token
and your-key
with actual values:
export TELEGRAM_TOKEN=your-token
export OPENAI_API_KEY=your-key
Now you are ready to start the bot:
npm start
For running in production it is better to use the build
script to generate a production build
npm run build
Now run the generated build files using node
:
node build/src/main.js
You can use the epieffeeth/telegram-openai
image on Docker Hub to run this bot using Docker:
docker run epieffeeth/telegram-openai \
-e TELEGRAM_TOKEN=your-token \
-e OPENAI_API_KEY=your-key
You can optionally define a system message to set the behavior of the bot during chats. For example, you can modify the personality of the bot or provide it with specific knowledge about your organization.
By default the bot searches for a system message in the prompt.txt
file in the current directory during startup. If not found, no system message is used. To change the file path where to search the system message use the CHAT_PROMPT_PATH
environment variable.
If you run the bot with Docker place the prompt file under /app/prompt.txt
in the container:
docker run epieffeeth/telegram-openai \
-e TELEGRAM_TOKEN=your-token \
-e OPENAI_API_KEY=your-key \
-v "$PWD/prompt.txt":/app/prompt.txt
By default the bot is accessible to anyone. You can restrict the bot access to a limited set of users or to members of specific groups.
The bot is accessible to all the users listed in the WHITELISTED_USER_IDS
environment variable AND to members of any of the groups listed in the WHITELISTED_GROUP_IDS
environment variable. If none of these variables are set, then the bot is accessible to anyone.
The bot needs to be member of all the groups listed in the WHITELISTED_GROUP_IDS
environment variable.
The easiest way to get your user id is to contact the @userinfobot, send a message and it will reply with your info, including the user id. Please note that I am not in any way affiliated with the @userinfobot and I am not responsible for how it works or how it manages data.
Please read this thread on Stack Overflow for usefull tips on how to get a group chat id.
This work is licensed under the AGPLv3. Visit Why the Affero GPL on gnu.org to know why.