- Notifications
You must be signed in to change notification settings - Fork0
Telegram bot backed by OpenAI APIs
License
epieffe/telegram-openai
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Telegram bot backed by OpenAIChat Completions andImage 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 onplatform.openai.com
- Navigate to theBilling page and add credit to balance (minimum 5$ is required)
- Navigate to theAPI 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 toprompt.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, replacingyour-token
andyour-key
with actual values:
export TELEGRAM_TOKEN=your-tokenexport OPENAI_API_KEY=your-key
Now you are ready to start the bot:
npm start
For running in production it is better to use thebuild
script to generate a production build
npm run build
Now run the generated build files usingnode
:
node build/src/main.js
You can use theepieffeeth/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 theprompt.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 theCHAT_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 theWHITELISTED_USER_IDS
environment variable AND to members of any of the groups listed in theWHITELISTED_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 theWHITELISTED_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 readthis thread on Stack Overflow for usefull tips on how to get a group chat id.
This work is licensed under theAGPLv3. VisitWhy the Affero GPL on gnu.org to know why.
About
Telegram bot backed by OpenAI APIs