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. 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:

KEYDescriptionDefault ValueSpecial Notes
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 and it will return the ID
I_AM_A_GENEROUS_PERSONDisable whitelist and allow access for everyonefalseSince 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_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 helpful 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 using /usage
HIDE_COMMAND_BUTTONSHide command buttonsnullWrite the buttons you want to hide, separated by commas, e.g., /start,/system. After modification, you need to reinitialize
DEBUG_MODEDebug modefalseCurrently, 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.

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