DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Building a Dynamic Chat Application: Setting up ChatGPT in FastAPI and Displaying Conversations in ReactJS
  • Kafka Event Streaming AI and Automation
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT

Trending

  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  • Observability Expenses: When ‘Pennies on the Dollar’ Add Up Quickly
  • How to Merge HTML Documents in Java
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Own ChatGPT Bot in Telegram

Own ChatGPT Bot in Telegram

Now popular are mirror websites that provide access to ChatGPT. Today we will write our own chatbot based on ChatGPT in the Telegram messenger.

By 
Ilya Sinenko user avatar
Ilya Sinenko
·
Jun. 29, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.2K Views

Join the DZone community and get the full member experience.

Join For Free

Recently, mirror sites providing access to ChatGPT have started to appear. However, using them is unsafe because such sites can collect and analyze all your correspondence. Especially those sites that provide such access completely free of charge. There is a demand for such sites because some countries block access to ChatGPT, and OpenAI blocks access to some countries. And for some users, 20 dollars for a ChatGPT subscription is expensive. 

So today, we will write our own chatbot based on ChatGPT in the Telegram messenger. 

This is very easy to do, and in some tasks, using it may be more convenient than on the official website chat.openai.com.

We will write the bot in Python, and we will send requests via the OpenAI API. 

Sometimes when registering, OpenAI gives grants of 5-18 dollars. But even if no grants are currently being issued, $5 will be enough for you for about 650,000 tokens of chatGPT v3.5, and this is a lot for a chat. 

For comparison, the book "Gone with the Wind" by Margaret Mitchell contains approximately 420,000 words, and "War and Peace" by Leo Tolstoy contains 590,000 words. 

Plus, APIs work faster than the free version of chat.openai.com, and unlike the paid subscription to chat.openai.com, which costs $20 per month, using APIs, you only pay for what you use, and if you don't have many requests, it's more profitable to use the API.

Perhaps by the time the article is published, the ChatGPT-4 API will not be open to everyone yet, but the GPT-3.5 version also solves many tasks well.

So, let's get started.

First, we need to create a Telegram BOT, for which we need to ask BotFather, the main Telegram bot, to create a bot. We type @botfather in the search bar on Telegram, open it, and press /start.

Telegram BOT

To create a new bot, we write/newbot, after which you will be asked to enter the name of your bot, and come up with a unique name, noting that it should end with the word "bot."

I will name my bot sinenko_gpt4_bot, because all the interesting names were already taken.

Next, you need to specify the username, which will represent your bot. It should also end with the word bot.

I will name it the same — sinenko_gpt4_bot. After this, BotFather will give you a token, which we will use to access the Telegram API.

BotFather

To get an API key for ChatGPT from OpenAI, we need to follow the link and register.

After registration, you need to create a new API key and save it.

create a new API key and save it

create a secret key

Now let's create a directory with our project. I'll call it TelegramBot. In this directory, we create a Python script. I'll call it bot.py. We will need Python libraries: telebot, requests, and JSON. But first, let's create a new Python environment in the project directory (this is not a mandatory step, but I'm used to creating a separate environment for each project).

Open the terminal, go to the project directory, and execute the command 

Shell
 
python -m venv tbot_env


Where tbot is the arbitrary name of your environment. After successfully creating the new environment, a folder with the environment's name will appear in the project directory. In my case, it's tbot_env. Now we need to activate it by running the activation script:

Shell
 
.\tbot_env\bin\activate


Depending on your system and Python version, the activation script might also be found at the following path:

Shell
 
.\tbot_env\Scripts\activate


After successful activation, a green inscription with the name of your environment will appear in your command line, in my case (tbot_env).

name of your environment

Attention! Sometimes on Windows 8 and above, when activating the environment, an error is given, prohibiting the execution of the executable file. To solve this problem, execute the command:

PowerShell
 
Set-ExecutionPolicy RemoteSigned


After which, a confirmation will be requested, and you need to enter [Y].

Now let's update pip and install the libraries we will need for our work:

Shell
 
python -m pip install --upgrade pip
pip install telebot, mysql-connector-python


Now let's write a simple Telegram bot:

Python
 
import telebot  
from telebot import types


bot = import telebot  
from telebot import types


bot = telebot.TeleBot('TELEGRAM_API_KEY')


@bot.message_handler(commands=['start']) # This decorator says that the next function should be called when the bot receives a message with the /start command.
def start(message): # This is the definition of the start function. This function will be called when the bot receives a message with the /start command.
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    btn1 = types.KeyboardButton("Start")
    markup.add(btn1)
    bot.send_message(message.from_user.id, "Hi, I'm the Telegram bot ChatGPT!", reply_markup=markup)
   
bot.polling(none_stop=True, interval=1) # for the bot to start "listening" or "polling" the Telegram server for new messages. "Polling" means regularly receiving data from the Telegram server by the bot to find out if new messages have been received for it.


After this, let's run our script with the command:

Shell
 
python .\bot.py


Now let's open Telegram and go to the chat with our bot using the link that FatherBot gave us. In my case it's: t.me/sinenko_gpt4_bot

And click on the Start button or enter the /start command. After that, we should receive a greeting message: “Hi, I'm the Telegram bot ChatGPT!” this means that our bot is working and successfully processing user messages. This code works through the polling function, which means the script itself will go to the Telegram server once a second and check if there are new messages. This is very convenient when the script is running on your local computer or when the server does not have a white IP address and a domain on the internet.

Hi, I'm the Telegram bot ChatGPT!

Now let's add processing for any text entered by the user. To do this, after the start function, we add the following code, which will respond with “OK” to any message entered by the user:

Python
 
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    bot.send_message(message.from_user.id, 'OK', reply_markup=markup)


Now let's test our bot and write some text.

response

Now we have learned how to accept incoming messages from users and give a response.

Now we need to redirect received messages to the GPT chat and return its answer to our users.

To make a request to the API, we need to send a request to the URL in JSON format.

The request should look like this:

Python
 
data = {
    'model': 'gpt-3.5-turbo',  # Selected GPT model
    'messages': [
        {'role': 'system','content': 'You are my assistant.'}, # We specify the role of the bot
        {"role": "user", "content": 'some user message'}, # User request
        {"role": "assistant", "content": 'Answer of ChatGPT'} # Answer of the Chat-GPT
    ],
    'max_tokens': 1000,  # Maximum number of tokens in the response
    'n': 1,  #  Number of text variants
    'temperature': 0.1  # Temperature (creative component)
}


To send a user request to the GPT chat, we need to pass {"role": "user," "content": 'some user message'} in the messages array.

But for ChatGPT to understand its role in the dialogue, you can also pass {'role': 'system','content': 'You are my assistant.'}.

If you want ChatGPT to remember your entire dialogue, then all past dialogue needs to be passed in each request:

{"role": "user," "content": 'first user message'},

{"role": "assistant," "content": 'first answer of ChatGPT'},

{"role": "user," "content": 'second user message'},

{"role": "assistant," "content": 'second answer of ChatGPT'},

{"role": "user," "content": 'some user message'}

But please note that tokens are also spent on sending all the previous dialogue.

Let's now add the forwarding of user messages to the GPT chat into our bot's code:

Python
 
import requests
import json


api_key_openai = 'OPENAI_API_KEY'


url = 'https://api.openai.com/v1/chat/completions'
headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer '+api_key_openai
}


data = {
    'model': 'gpt-3.5-turbo',  # Selected GPT model
    'messages': [
        {'role': 'system','content': 'You are my assistant.'} # We specify the role of the bot
    ],
    'max_tokens': 1000,  # Maximum number of tokens in the response
    'n': 1,  #  Number of text variants
    'temperature': 0.1  # Temperature (creative component)
}
Python
 
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)


    data['messages'].append({"role": "user", "content": message.text}) # Add a new user request to our dialog array, form a request to the server on behalf of the user, with the text received in telegram
    response = requests.post(url, headers=headers, data=json.dumps(data))  # Send a request to the server in json format
    result = response.json() # get the answer in json format and convert to an array
    print(result)
    bot.send_message(message.from_user.id, result['choices'][0]['message']['content'], reply_markup=markup) # Take the text from the array from ChatGPT and send it to Telegram
    data['messages'].append({"role": "assistant", "content": result['choices'][0]['message']['content']}) # Add a new bot response to our dialog array


Now let's run our script and try to write some message:

write a message

Now we have a fully functional chatbot that can be used.

The API will be sent your entire dialogue; over time, the dialogue can become large, and it would be pointless to spend your tokens, so after sending a message, you can delete the first messages from the messages array. And leave, for example, the last 40 messages. This will be quite enough. To clear old messages, add the following code after sending a message:

Python
 
# Delete the first element of the array if the array is longer than 40 elements, but leave the first element of the array, which is the initial text of the bot
while len(data['messages']) > 40:
    data['messages'].pop(1)


If there are more than 40 messages, we delete the oldest messages, except for the first one (because the first message stores the role of our bot).

We can also change the role of our bot to anyone, and we can make it write poems from your sentences, invent horror stories or solve math problems. For this, it is enough to change the content field of the system message. Let's change it in the following way:

Python
 
data = {
    'model': 'gpt-3.5-turbo',  # Selected GPT model
    'messages': [
        {'role': 'system','content': 'You are my Chinese translator, translate all messages I write in Chinese and write the transcription in square brackets using English letters.'}, # We specify the role of the bot
    ],
    'max_tokens': 1000,  # Maximum number of tokens in the response
    'n': 1,  #  Number of text variants
    'temperature': 0.1  # Temperature (creative component)
}


Let's launch and check the operation:

check the operation

By the way, for the role of a translator in the 'messages' variable, you can delete all messages except the system one. This will save your tokens very much, and five dollars will be enough for translating 300,000+ words.

The article showed the simplest example of a bot, and this bot can be infinitely improved. You can also make a chat division for each separate Telegram user, save it to a database, and add advertisements and subscriptions. There are also missing error handlers in case API requests fail.

But these are topics for other articles:)

Link to the bot on GitHub.

API Data structure Python (language) ChatGPT

Opinions expressed by DZone contributors are their own.

Related

  • Building a Dynamic Chat Application: Setting up ChatGPT in FastAPI and Displaying Conversations in ReactJS
  • Kafka Event Streaming AI and Automation
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!