This article serves as a comprehensive guide for beginners looking to create their own Telegram bot powered by OpenAI's ChatGPT. It explains the concept of Telegram bots and ChatGPT, guiding the reader through the process of setting up a Telegram bot, integrating it with ChatGPT, and running it. By the end of the article, the reader will have a functioning, conversational Telegram bot and a foundational understanding of how to further develop and enhance its capabilities.
Introduction
Welcome to the fascinating world of software development! This article is especially for beginners who have a keen interest in creating their own Telegram bot using ChatGPT, an advanced language model developed by OpenAI. Whether you're a hobbyist, a budding software developer, or just someone who loves technology, this guide is for you.
Contents
- Introduction to Telegram Bots and ChatGPT
- Setting Up a Telegram Bot and OpenAI API
- Integrating ChatGPT with Your Telegram Bot
- Running Your Bot
Introduction to Telegram Bots and ChatGPT
Telegram Messenger is a globally accessible messaging service. In June 2015, Telegram launched a platform to create bots. Telegram bots are essentially programs that respond to messages or commands they receive. They can do a variety of things, from providing weather updates to sending reminders. In our case, we will be building a chatbot, a bot that can carry out a conversation with a user.
To power our chatbot, we'll be using ChatGPT (API version of it). This is a powerful language model trained by OpenAI that can generate human-like text. By integrating ChatGPT with our Telegram bot, we can easily create a bot that can be as powerful as ChatGPT, but in your messenger!
Setting Up a Telegram Bot and OpenAI API
The first step is to create a Telegram bot. We'll need to register our bot with Telegram and get a token we can use to send and receive messages.
Here are the steps:
- Open Telegram and search for the "BotFather" bot with verified mark.
- Start a chat and type
/newbot
to create a new bot. - Follow the instructions to name your bot.
- Once done, BotFather will give you a token. Keep this token safe.
- Go to https://platform.openai.com/apps and choose API option.
- Click on Personal and then View API keys.
- Press Create new key.
Integrating ChatGPT with Your Telegram Bot
Now, it's time to make our bot smart! We will be using the telebot and OpenAI
library:
- First, install the necessary libraries. Open your terminal and type:
pip install telebot openai
- Create a new Python script (I named it bot.py), and paste code:
import telebot
import openai
bot = telebot.TeleBot ('<Your telegram bot token>')
openai.api_key = '<Your openai API token>'
def generate_answer(text):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": text},
]
)
result = ''
for choice in response.choices:
result += choice.message.content
except Exception as e:
return f"Oops!! Some problems with openAI. Reason: {e}"
return result
@bot.message_handler(content_types=['text'])
def send_text(message):
answer = generate_answer(message.text)
bot.send_message(message.chat.id, answer)
bot.polling()
We have defined a function called generate_answer
. This function takes text that our bot receives during conversation and makes an API call to OpenAI. We are using gpt-3.5-turbo to generate a response. You have a limit of 3 requests per minute. If you want to remove it, buy a premium subscription. Keep in mind, if you are using premium ChatGPT sub, you cannot use it in API, because it is an independent product and you need to buy an extra sub for it).
For I/O message, we are using a decorator @bot.message_handler from telebot lib. We have defined a text content and defied a function, that relieves message from telegram chat, generates a response from OpenAI API and returns answers to the chat using bot.send_message
. To execute the bot, we are using bot.polling()
command.
Running Your Bot
You can now run your bot! In your terminal, navigate to the directory containing your Python script and run:
python <your_script>.py
Your bot is now live! You can talk to it on Telegram, and it will respond using the power of ChatGPT.
Conclusion
Remember, this is a very basic example. There's a lot more you can do with Telegram bots and ChatGPT. You can add more commands, use different engines from OpenAI, and much more. So keep exploring and experimenting!
Happy coding!
History
- 30th May, 2023: Initial version