Create a ChatGPT Telegram Bot on Python with OpenAI

Create a ChatGPT Telegram Bot on Python with OpenAI

Table of Contents

  1. Introduction
  2. Getting the API Key from OpenAI.com
  3. Making a Simple ChatGPT Bot in Terminal
  4. Customizing the ChatGPT Bot
  5. Connecting the Script to Telegram Bot
  6. Making the Bot Work 24/7
  7. Keeping API Keys Safe
  8. Conclusion
  9. References
  10. FAQ

How to Make a ChatGPT Bot on Python and Connect it to Telegram Bot

Are You interested in learning how to make a ChatGPT bot on Python and connect it to Telegram bot? If so, you've come to the right place! In this article, we will guide you through the process of creating a ChatGPT bot on Python and connecting it to Telegram bot. This tutorial is perfect for those who want to learn how to make such bots on Python, work on Upwork or similar websites, or don't have access to OpenAI services from their country.

Getting the API Key from OpenAI.com

The first step in creating a ChatGPT bot on Python is to get the API key from OpenAI.com. You can make it only for personal purposes, and you haven't any organizations. Click "Create new secret key" and copy this key and save it somewhere because you won't be able to see it again. Save it in your bot file that you just created.

Making a Simple ChatGPT Bot in Terminal

The next step is to make a simple ChatGPT bot that works in the terminal. Go to examples, and you can get examples of that bot for the needed purposes. For example, just for answering the questions or making a code. Each one has its own settings. Choose the first one, and customize it later. You can get this code from the API requests. Copy this script and paste it to your python file. You have to use your own API key. Enter this key in quotes.

Customizing the ChatGPT Bot

You can also customize the answers you will get from ChatGPT. Temperature is the number of randomness in the answer that the chatbot will generate. Set it to 0.5. Not so much, but it can make some creativity in the generated text. Max tokens are the maximum words that it can generate. Top p is like temperature but controls the diversity of the output in a different way. You can check all the parameters on this page. There are a lot of settings that you can use to customize your bot for your purposes. Then print the answer in the terminal. You have to print response.choices.text. Try to run this script. Before that, you should install the OpenAI library to your PC. After that, just run the bot. As you can see, the bot is working. Ask it to code something. For example, write some calculator code on Python.

Connecting the Script to Telegram Bot

The next step is connecting this script to the Telegram bot. Go to Telegram and find Bot Father. This bot will help you to make your own bot. Click "menu," "new bot," enter the name of your bot, and then username. After that, you will get the token of that bot. Go to code editing again. Import the needed packages to the script. Use aiogram in this video because it's the most convenient for me. Create a new instance of the Telegram bot, enter the token of this bot, and make a new variable for the dispatcher. Then add message handlers. The first one is a welcome message. You will get this message if you use start or help commands. The Second one is the handler of all messages. These messages are your requests to the ChatGPT bot. Put the response inside this handler. But the prompt is your messages. And then add the reply to your messages. Remove the print, we don't need this anymore. And the last thing, add execution of the Telegram bot. Don't forget to install the aiogram Package to your PC. Then run the bot. Go to Telegram and start a dialogue with your bot. When you use the start command, you get the welcome message. And then ask something this bot like write some calculator code on Python. You got the answer, so this is working. Then you can make the next request.

Making the Bot Work 24/7

Now let's make this bot work all the time even your PC is turned off. Go to Replit.com. Create an account if you haven't it yet. Click "Create a new repl." The template is Python, and the name is anything you want. Copy the code you make to the main file. And create another python file, keep alive. Paste this code inside this file. You can find this in the description. It makes a local server, and it will help to run this bot for all the time. Go to the main file again. Import this file here. And also add the keep alive function here. Let's run the application. All is good if you get the same logs in the console. If you get some errors, try to restart the application. Now go to uptime robot, register here. Click "add new monitor." Monitor Type is HTTP. Name is anything you want. Paste here URL of the application you can get from replit. Then click "create monitor" and confirm this action. And now my bot works 24 hours a day all the time. As you can see, it's working.

Keeping API Keys Safe

One more important thing, don't put your API keys inside the code like this. Make secrets for this as shown in the video. And then add these tokens to the code. Use os.genenv and the name of the token in brackets. Then run this bot again. As you can see, it's working for me. You can customize it as you want. I just showed how to make a ChatGPT Telegram bot.

Conclusion

In conclusion, creating a ChatGPT bot on Python and connecting it to Telegram bot is not as difficult as it may seem. By following the steps outlined in this article, you can create your own ChatGPT bot and customize it to your liking. Remember to keep your API keys safe and secure, and don't hesitate to experiment with different settings to get the most out of your bot.

References

  • OpenAI.com
  • Telegram
  • aiogram
  • replit.com
  • uptime robot

FAQ

Q: What is ChatGPT? A: ChatGPT is a chatbot that uses the GPT-3 language model to generate human-like responses to user input.

Q: What is Python? A: Python is a high-level programming language that is widely used for web development, data analysis, artificial intelligence, and more.

Q: What is Telegram? A: Telegram is a cloud-based instant messaging app that allows users to send messages, photos, videos, and files of any type.

Q: What is Bot Father? A: Bot Father is a Telegram bot that helps users create and manage their own bots.

Q: How do I keep my API keys safe? A: To keep your API keys safe, you should create secrets for them and add them to your code using os.genenv and the name of the token in brackets.

Q: Can I customize the ChatGPT bot? A: Yes, you can customize the ChatGPT bot by adjusting the temperature, max tokens, and top p parameters, among others.

Q: How do I make the ChatGPT bot work 24/7? A: To make the ChatGPT bot work 24/7, you can use a local server and a keep alive function, as described in this article.

Most people like

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content