This simple console application interfaces Telegram with ChatGPT, allowing Telegram bots to act as agents relaying messages to and from OpenAI’s ChatGPT, based on a personality assigned to the bot. From Telegram, you can query the bot, which relays the message to the ChatGPT API, queries OpenAI, and receives a response based on the personality of your Telegram bot, as customized within the configuration file. This was a quick one-day project, and I thought I’d share it online. I’ll add more features over time, so check back.
create | Creates a chat bot.
edit | Edit a chat bot.
delete | Delete a chat bot.
list | List all chat bots.
start | start chatgpt bots.
stop | stop chatgpt bots.
botchat | Two bots talk to each other. Requires two or more bots in same telegram channel and connected to chatgpt.
exit | Exit application.
To use this application you will need to modify the configuration files to interface the application with your Telegram bots and also your ChatGPT account. Below are the instructions on how to do this.
How to create a bot:
- Start a chat with BotFather.
- Click Menu and select NewBot or type /newbot.
- Enter the bot name.
- Enter the username for the bot, it must end with ‘bot’ and must be unique.
- Congratz, you’ve just created a telegram bot. Copy the HTTP API token into bots.json file. In addition, add your botName (the bot’s telegram username) and full name, the ordinary name of the bot.
- Enter a description of the personality you would like the bot to have. This will guide the bot’s personality when it uses ChatGPT to generate and respond.
Connecting to ChatGPT:
- Visit OpenAI API key page.
- Click “Create new secret key” and copy it to clipboard, then paste it into completionParameters.json apiKey : “your chatgpt api key here” and replace the text in the quotes. IMPORTANT: Never share your API key with anyone as this key can be used to access OpenAI services through your account.
- Configure the settings of completionParameters.json to cater to your needs.
Important: I strongly advise implementing a usage limit for your OpenAI API account. Additionally, it’s advisable to restrict the usage of the API to a private Telegram channel. Querying ChatGPT typically incurs real-world computational costs. You can adjust the “max_tokens” per query using “completionParameters.json,” but please be aware that if your channel is open to the public, other users may also make queries with your bot, potentially leading to additional costs.
Brief explanation of each configuration parameter:
- “apiUrl”: The URL for making requests to the OpenAI ChatGPT API.
- “apiModelsUrl”: The URL for accessing information about available models.
- “apiKey”: Your authentication key to access the ChatGPT API.
- “model”: The specific ChatGPT model to be used, in this case, “text-davinci-003.”
- “prompt”: The initial text or context provided to the model to generate responses.
- “suffix”: An optional additional text to add to the end of generated responses.
- “max_tokens”: The maximum number of tokens in the response, with a range between 1 and 4096.
- “temperature”: A parameter controlling the randomness of the output, with a range between 0.2 and 1.0.
- “top_p”: A value to control the diversity of the output, with a range between 0.0 and 1.0.
- “n”: The number of alternative responses to generate, with a range between 1 and 20.
- “stream”: A boolean indicating whether to stream the response or get it as a whole.
- “logprobs”: Optional setting for obtaining log probabilities for each token in the response.
- “echo”: A boolean indicating whether to include the input prompt in the output.
- “stop”: An optional string that, if encountered in the response, will stop the generation.
- “presence_penalty”: A penalty value to discourage the model from repeating a specific response, with a range between -1.0 and 1.0.
- “frequency_penalty”: A penalty value to discourage the model from using frequent words, with a range between -1.0 and 1.0.
- “best_of”: The number of “n” alternatives to consider and return the best one from, with a range between 1 and 20.
- “logit_bias”: An optional dictionary to influence the output distribution.
- “user”: An optional field for specifying a user identifier.
- “role”: An optional field for specifying a role identifier.
Config files are found in the same folder as TelegramChatBot. These files must remain in folder or the application will not run.
Now that you’ve configured the application, open TelegramChatBot.exe and type “start”. That’s it, it should work from here provided you didn’t make any mistakes.