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, the actual 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 name, avatar, etc. of the bot.
- 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, fill in this Token in the TELEGRAM_TOKEN variable in Cloudflare Workers settings.
- In Telegram, find BotFather, 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 API Keys. Later, fill it in the API_KEY.
Deployment#
1. Deploy Workers#
- Open Cloudflare 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 variables:
KEY | Description | Default Value | Special Instructions |
---|---|---|---|
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 to get it |
I_AM_A_GENEROUS_PERSON | Disable whitelist and allow access for everyone | false | Since many people don't want to set a whitelist or don't know how to get 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 capable 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 through /usage |
HIDE_COMMAND_BUTTONS | Hide command buttons | null | Write the buttons you want to hide, separated by commas, e.g. /start,/system. After modification, reinit is required |
DEBUG_MODE | Debug mode | false | Currently, it can save 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 KV Namespace Bindings.
- Click on Add variable.
- Set the name as DATABASE and select the KV data created just now.
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 then the chat context will be sent to ChatGPT every time.
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, set it so that even if a new session is started, it can be maintained without debugging every time | /setenv SYSTEM_INIT_MESSAGE=Now you are a cat girl, end each sentence with "meow" |
OPENAI_API_EXTRA_PARAMS | Additional parameters for the OpenAI API, set it so that it will be included in every API call. It can be used to adjust parameters such as temperature | /setenv OPENAI_API_EXTRA_PARAMS={"temperature": 0.5} Each modification must be a complete JSON |