This weblog describes how one can combine Telegram and create a python software primarily based on the Flask internet framework that may obtain person Telegram messages and make the most of ChatGPT to answer these messages intimately.
Step 1: Create Telegram Bot
To be able to automate the messages with ChatGPT, we simply must create the Telegram Bot. Please observe the directions in our weblog submit Telegram Bot setup from step 1 to step 11. Please verify that you just adopted the weblog’s directions earlier than continuing.
Step 2: ChatGPT API
Please check out our weblog submit, WhatsApp Chatbot With ChatGPT Step 2 “ChatGPT API” which has directions for establishing and utilizing ChatGPT.
Step 3: Combine ChatGPT API with Flask Software
After our ChatGPT API obtained put in efficiently, it’s time to combine it with the flask software.
Nevertheless, the ChatGPT API will proceed to run within the Firefox browser’s background, which may intrude with receiving responses from the ChatGPT API, subsequently we should finish the method that’s at the moment working the browser.
def course of():
attempt:
# iterating via every occasion of the method
for line in os.popen(“ps ax | grep firefox | grep -v grep”):
fields = line.cut up()
# extracting Course of ID from the output
pid = fields[0]
# terminating course of
os.kill(int(pid), sign.SIGKILL)
print(“Course of Efficiently terminated”)
besides:
print(“Error Encountered whereas working script”)
The flask software we developed in step 1 needs to be modified now. To obtain ChatGPT’s response to person messages, change the prevailing code in your Flask app with the next code.
import requests
import os, sign
from flask import Flask
from flask import request
from flask import Response
from chatgpt_wrapper import ChatGPT
TOKEN = “<Your BOT TOKEN>”
app = Flask(__name__)
def parse_message(message):
print(“message–>”,message)
chat_id = message[‘message’][‘chat’][‘id’]
txt = message[‘message’][‘text’]
print(“Textual content :”, txt)
return chat_id,txt
def tel_send_message(chat_id, textual content):
url = f’
payload = {
‘chat_id’: chat_id,
‘textual content’: textual content
}
response = requests.submit(url,json=payload)
return response
@app.route(‘/’, strategies=[‘GET’, ‘POST’])
def index():
if request.technique == ‘POST’:
msg = request.get_json()
chat_id,txt = parse_message(msg)
bot = ChatGPT()
response = bot.ask(txt)
print(“ChatGPT response: “,response)
tel_send_message(chat_id,response)
course of()
return Response(‘okay’, standing=200)
else:
return “<h1>Welcome!</h1>”
if __name__ == ‘__main__’:
app.run(debug=True)
Notice:
Run the flask software within the terminal: python SCRIPT_NAME.pyRun the ngrok on terminal: ngrok http 5000
Step 4: Check Telegram Chatbot
Please ship some messages to the Telegram bot to obtain the ChatGPT-generated response. Additionally, you will get a response on a server.
Right here is the ChatGPT response on our server.

We are going to get the beneath response within the Telegram Bot.

Do tell us for those who face any points in establishing or utilizing the script. We’d be joyful to assist! Contact us or submit your question within the feedback.
Additionally, take a look at our different tutorials to learn to construct a ChatGPT chatbot on completely different platforms.
WhatsApp with ChatGPT: Construct An Automated, AI-Powered WhatsApp Chatbot With ChatGPT Utilizing Flask
Fb Messenger with ChatGPT: Construct An Automated, AI-Powered Fb Messenger Chatbot With ChatGPT Utilizing Flask
Slack Chatbot with ChatGPT: Construct An Automated, AI-Powered Slack Chatbot With ChatGPT Utilizing Flask
We now have already created a weblog submit collection the place we now have offered a tutorial on methods to create a chatbot for various platforms. You possibly can discover these blogs and be taught how one can set completely different sorts of wealthy responses which might improve person engagement in your chatbot.
Telegram: Create Telegram Bot Utilizing Python Tutorial With Examples
Fb Messanger: Create Fb Messenger Bot Utilizing Python Tutorial With Examples
Slack: Create Slack Bot Utilizing Python Tutorial With Examples
Discord: Create Discord Bot Utilizing Python Tutorial With Examples