Creating chatbots is becoming increasingly popular in the world of technology. They are used in a variety of fields, from customer support to education and medicine. However, developing a chatbot can be quite a daunting task, requiring a deep knowledge of programming and artificial intelligence. In this article, we will look at how to use OpenAI ChatGPT to create a Python-based Telegram bot that will communicate with users using natural language.
- Creating an OpenAI account and API key
- Creating a telegram bot
- Preparing the server, running the bot
1. Creation of account and OpenAI API key
Go to the OpenAI page and create your account.
You can sign up with an email address or you can continue with an existing Google or Microsoft account.
The next step is to enter the number to which a confirmation SMS will be sent.
Note, OpenAI is not available in all countries. See the Supported countries and territories link for a list of supported countries.
Creating API key
Click on your avatar in the upper right corner and select View API Keys. Click Create new secret Key to create the API key and copy it.
Attention! Make sure you save the key, as you won't be able to copy it when you log in again. If you lose it, you will need to generate a new key.
2. Creating a telegram bot
We will use a special bot @BotFather to create a telegram bot.
And so, to create a new bot, run the command:
Next, assign a name and a nickname to the bot — you will be able to find the bot in Telegram by it. The nickname should be unique, in Latin characters and must end in bot.
For our example, let’s specify the nickname – uni_chat_gpt_bot
After the bot is created, you will see the following message:
For us it is important token generated when creating the bot, we will use it in the future to integrate with OpenAI ChatGTP.
3. Preparing the server, running the bot
In this tutorial we will create a simple chatbot using the Davinci AI model for generating responses. It is an accurate and expensive model, but it is slower because it works with a lot of data. It can do the tasks of all the other models, but requires less guidance in the “Instructions for the bot”.
In the Examples and OpenAI Cookbook sections, you can see examples of how to use the models, as well as experiment with the settings in Playground.
For the example in this tutorial, we’ve chosen the Friend chat pattern, which is a text-based conversation emulation. As a basis, we will take the API-request for Python of this template, but we will edit it to our requirements.
And so it’s time to create and host the Python script on the server. We will use a VPS based on Ubuntu Server 20.04.
Logging into the server via SSH.
By default, Ubuntu Server 20.04 already has python version 3 installed.
To check, enter the command:
Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
this means that Python 3.8.10 is already installed on the server and we can proceed to the next step.
apt install pip
Install the necessary libraries for our script:
pip install openai pip install pyTelegramBotAPI
Create the file main.py:
Insert the following code:
import os import openai import telebot openai.api_key = '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' bot = telebot.TeleBot('5868492437:AAHzr8E09ooGpXNw7lOf2On2K53FKz-ZTyc') @bot.message_handler(func=lambda _: True) def handle_message(message): response = openai.Completion.create( model="text-davinci-003", prompt=message.text, temperature=0.5, max_tokens=60, top_p=1.0, frequency_penalty=0.5, presence_penalty=0.0, ) bot.send_message(chat_id=message.from_user.id, text=response['choices']['text']) bot.polling()
openai.api_key = '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' API-key OpenAI
bot = telebot.TeleBot('5868492437:AAHzr8E09ooGpXNw7lOf2On2K53FKz-ZTyc') Telegram chatbot API key
Accordingly, they must be replaced with your own.
Run the script:
If no errors are not detected, you can proceed to the testing of our Telegram-bot, to do this, you need to find it by nickname – uni_chat_gpt_bot and you can start a dialogue: