I got error when running python script to integrate openai with telegram bot. here is my code:
# Import the necessary modules
import telegram
import openai
from telegram.ext import CommandHandler, MessageHandler, Updater
from queue import Queue
# Initialize the Telegram bot
bot = telegram.Bot(token='')
# Initialize the OpenAI API
openai.api_key = ''
# Define a function to handle /start command
def start(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Hi! I'm a ChatGPT bot. Send me a message and I'll try to respond to it.")
# Define a function to handle messages
def message(bot, update):
# Get the message text
message_text = update.message.text
# Call the OpenAI API to generate a response
response = openai.Completion.create(
engine="davinci",
prompt=message_text,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
# Get the response text from the API
response_text = response.choices[0].text.strip()
# Send the response back to the user
bot.send_message(chat_id=update.message.chat_id, text=response_text)
# Initialize the Telegram bot updater and dispatcher
update_queue = Queue()
updater = Updater(bot=bot, update_queue=update_queue)
dispatcher = updater.dispatcher
# Add command handlers
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
# Add message handler
message_handler = MessageHandler(None, message)
dispatcher.add_handler(message_handler)
# Start the Telegram bot
updater.start_polling()
The error :
dispatcher = updater.dispatcher
^^^^^^^^^^^^^^^^^^
AttributeError: 'Updater' object has no attribute 'dispatcher'
I don’t know how to fix this, because I’ve already see many solution but it tell me to update telegram-bot.
is there any solution to this? I’ve already upgrade telegram-bot but still error.
2
Answers
I think
'dispatcher'
is no longer used in version20.0a0
If you want to use your code then you can downgrade as:
If you are using
v20
then you have to build your application differently and you have to useasync
functions.It looks that "dispacher" is no longer used, does somebody have a new way of coding using version 20.0?