lapis

lapis

理解以真实为本,但真实本身不会自动呈现

Deploy your own ChatGPT Telegram bot

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:

KEYDescriptionDefault ValueSpecial Instructions
API_KEYOpenAI API Keynull
TELEGRAM_AVAILABLE_TOKENSSupports multiple Telegram Bot TokensnullSeparate multiple Tokens with commas
CHAT_WHITE_LISTChat ID whitelistnullSeparate multiple IDs with commas. If you don't know the ID, you can chat with the bot to get it
I_AM_A_GENEROUS_PERSONDisable whitelist and allow access for everyonefalseSince 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_HISTORYAutomatically trim the message historytrueTo avoid the 4096 character limit, messages will be trimmed
MAX_HISTORY_LENGTHMaximum length of the message history20When AUTO_TRIM_HISTORY is enabled, messages will be trimmed to avoid the 4096 character limit
CHAT_MODELOpenAI model selectiongpt-3.5-turbo
SYSTEM_INIT_MESSAGESystem initialization messageYou are a capable assistantDefault bot setting
SYSTEM_INIT_MESSAGE_ROLESystem initialization message rolesystemDefault bot setting
ENABLE_USAGE_STATISTICSEnable usage statisticsfalseWhen enabled, each API call will be recorded in KV and can be viewed through /usage
HIDE_COMMAND_BUTTONSHide command buttonsnullWrite the buttons you want to hide, separated by commas, e.g. /start,/system. After modification, reinit is required
DEBUG_MODEDebug modefalseCurrently, 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.

KEYDescriptionExample
SYSTEM_INIT_MESSAGESystem 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_PARAMSAdditional 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
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.