Chatgpt telegram bot
designed with canva

Chatgpt telegram bot

Today I started to explore and integrate the openAI chatgpt-3.5 engine into a telegram bot. The below written simple python script is capable of doing that.

Before you experience it. There are three key steps to be followed:

1) Signup to openai account and create an api key to access the service.

link: Account API Keys - OpenAI API

2) Create a telegram bot using bot father in telegram to create a new bot and store the http api key it generates after successful bot creation.

3) Paste the API keys into respective placeholders in the below code.

import telebot
import openai

openai.api_key = "Your chatgpt api key"
MODEL_NAME = "gpt-3.5-turbo"

# Set up your bot token
bot_token = "you bot father bot HTTP token"

# Create a bot instance
bot = telebot.TeleBot(bot_token)

# Define a function to handle incoming messages
@bot.message_handler(func=lambda message: True)
def handle_message(message):
? ? # Get the text of the incoming message
? ? message_text = message.text.lower()

? ? # use ChatCompletion class from openai
? ? response = openai.ChatCompletion.create(
? ? model=MODEL_NAME,
? ? messages=[
? ? {"role": "user", "content": message_text}
? ? ]
? ? )
? ? content = response['choices'][0]['message']['content']
? ? bot.send_message(message.chat.id, content)
? ?  ? 
# Start the bot
bot.polling()        

Run the script and start interacting in the bot.

Syed Riaz Raza

Junior Research Engineer @ IRI UPC | Mixed Reality & Computer Vision

1 年

Love this

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了