#Telegram bot API + chatGPT API (Python)
Create Telegram bot using chatGPT API
BotFather steps: (From Telegram find BotFather contact)
User:
*/start*
BotFather, [Feb 5, 2023 at 2:08:07 PM]:
I can help you create and manage Telegram bots. If you're new to the Bot API, please see the manual.
You can control me by sending these commands:
*/newbot* - create a new bot
...
User:, [Feb 5, 2023 at 2:08:40 PM]:
*/newbot*
BotFather, [Feb 5, 2023 at 2:08:40 PM]:
Alright, a new bot. How are we going to call it? Please choose a name for your bot.
User, [Feb 5, 2023 at 2:09:23 PM]:
*Dec_22_chatGPT*
BotFather, [Feb 5, 2023 at 2:09:23 PM]:
Good. Now let's choose a username for your bot. It must end in `bot`.
Like this, for example: TetrisBot or tetris_bot.
User, [Feb 5, 2023 at 2:10:14 PM]:
*chat_dec_22_bot*
BotFather, [Feb 5, 2023 at 2:10:14 PM]:
Done! Congratulations on your new bot. You will find it at t.me/chat_dec_22_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.
Use this token to access the HTTP API:
6185472765:PIbwepibvWEfvweurwEO$I#)$#$D
Keep your token secure and store it safely, it can be used by anyone to control your bot.
For a description of the Bot API, see this page: https://core.telegram.org/bots/apiPre-requisites:
OpenAI.com valid API key
Telegram API key
Steps:
The best practice is to run the app in their own virtual environment
Install the aiogram and openai libraries:
Register a new bot with BotFather on Telegram and obtain the API key.
Create a new Python file like "chatGPT_telegram_bot.py" and import the necessary libraries:
Initialize the OpenAI API client and the Telegram bot with the API key obtained from BotFather:
Define a function to handle user messages:
Define a function to handle user text messages and send them to the OpenAI API:
Start the bot and make it listen for incoming messages by running "run.sh":
Now the bot should be ready to receive questions from users and respond to them using the OpenAI API.
Create requirements and Dockerfile
Put Dockerfile into the same directory with run.sh and chatGPT_telegram_bot.py ifles
Last updated