Deploy Your Own ChatGPT Telegram Bot#
date: March 24, 2023
slug: 19
status: Published
tags: Technology
type: Post
Open source project link: Link A Telegram conversation bot developed based on Cloudflare Workers. It has been tested and the experience is good. After all, who doesn't love something that is free and easy to use?
Preparation#
1. Create a Telegram Bot#
Create a new Telegram bot at BotFather, obtain the token, and customize the bot's name, avatar, etc.
- Open Telegram and send the /start command to BotFather.
- Send the /newbot command and give your bot a name.
- Give your bot a unique username.
- BotFather will generate a token, copy and save it. Later, you will need to fill in this token in the TELEGRAM_TOKEN variable in Cloudflare Workers settings.
- Find BotFather on Telegram, send /setcommands, find your bot, and send new - start a new conversation.
2. Register an OpenAI Account and Create an API Key#
Register an account at openai.com, click on the profile picture in the upper right corner to enter the personal settings page, and create a new API Key in the API Keys section. Later, you will need to fill in this key in the API_KEY variable.
Deployment#
1. Deploy Workers#
- Open Cloudfalre Workers and register an account.
- Click on "Create a Worker".
- Copy the code into the editor and save it.
2. Configure Environment Variables#
Open Setting-Variables-Environment Variables and add the following variables:
KEY | Description | Default Value | Special Notes |
---|---|---|---|
API_KEY | OpenAI API Key | null | |
TELEGRAM_AVAILABLE_TOKENS | Supports multiple Telegram Bot Tokens | null | Separate multiple tokens with commas |
CHAT_WHITE_LIST | Chat ID whitelist | null | Separate multiple IDs with commas. If you don't know the ID, you can chat with the bot and it will return the ID |
I_AM_A_GENEROUS_PERSON | Disable whitelist and allow access for everyone | false | Since many people do not want to set a whitelist or do not know how to obtain the ID, this option allows access for everyone. It takes effect when the value is true |
AUTO_TRIM_HISTORY | Automatically trim the message history | true | To avoid the 4096 character limit, messages will be trimmed |
MAX_HISTORY_LENGTH | Maximum length of the message history | 20 | When AUTO_TRIM_HISTORY is enabled, messages will be trimmed to avoid the 4096 character limit |
CHAT_MODEL | OpenAI model selection | gpt-3.5-turbo | |
SYSTEM_INIT_MESSAGE | System initialization message | You are a helpful assistant | Default bot setting |
SYSTEM_INIT_MESSAGE_ROLE | System initialization message role | system | Default bot setting |
ENABLE_USAGE_STATISTICS | Enable usage statistics | false | When enabled, each API call will be recorded in KV and can be viewed using /usage |
HIDE_COMMAND_BUTTONS | Hide command buttons | null | Write the buttons you want to hide, separated by commas, e.g., /start,/system. After modification, you need to reinitialize |
DEBUG_MODE | Debug mode | false | Currently, it saves the latest message to KV for debugging purposes |
3. Bind KV Data#
(Settings-Variables-KV Namespace Bindings), set the name as DATABASE
- Click on "Create a Namespace" in the upper right corner
- Set the name as DATABASE
- Open Cloudflare Workers and click on your Workers
- Click on "Setting" -> "Variables" in the upper right corner
- Click on "Edit variables" in the KV Namespace Bindings section
- Click on "Add variable"
- Set the name as DATABASE and select the KV data you just created
4. Run#
Visit https://your_workers_name.your_workers_subdomain.workers.dev/init to bind Telegram and start a new conversation. Use the /new command to start, and the chat context will be sent to ChatGPT for each subsequent interaction.
User Configuration After Running#
Each user's custom configuration can only be modified by sending a message through Telegram. The message format is /setenv KEY=VALUE
.
KEY | Description | Example |
---|---|---|
SYSTEM_INIT_MESSAGE | System initialization parameter. Once set, it will be preserved even when starting a new session, eliminating the need for debugging every time | /setenv SYSTEM_INIT_MESSAGE=Now you are a cat girl, and every sentence ends with "meow" |
OPENAI_API_EXTRA_PARAMS | Additional parameters for the OpenAI API. Once set, they will be included in every API call and can be used to adjust parameters such as temperature | /setenv OPENAI_API_EXTRA_PARAMS={"temperature": 0.5} Each modification must be a complete JSON |