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.
I think 'dispatcher' is no longer used in version 20.0a0
If you want to use your code then you can downgrade as:
pip install python-telegram-bot==13.3
If you are using v20 then you have to build your application differently and you have to use async functions.
Related
I try to create a Telegram bot but I can't use property Update class.
import logging
from multiprocessing import Queue
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
def main():
update_queue = Queue()
updater = Updater("API KEY", use_context=True, update_queue=update_queue)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Idle say that there is no dispatcher in Update class. Try update Telegram api, didn't help. Can't find another way to update bot
Since version 20.0 the Updater is only used to fetch updates, from the docs:
Changed in version 20.0:
Removed argument and attribute user_sig_handler
The only arguments and attributes are now bot and update_queue as now the sole purpose of this class is to fetch updates. The entry point to a PTB application is now telegram.ext.Application.
So if you want to add a handler, use an Application.
An example, taken from the echo bot example:
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Echo the user message."""
await update.message.reply_text(update.message.text)
def main() -> None:
"""Start the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token("TOKEN").build()
# on different commands - answer in Telegram
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("help", help_command))
# on non command i.e message - echo the message on Telegram
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
# Run the bot until the user presses Ctrl-C
application.run_polling()
if __name__ == "__main__":
main()
I made my first simple telegram bot with python-telegram-bot. Here's the code:
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
token = '1234567890'
updater = Updater(token=token, use_context=True)
dispatcher = updater.dispatcher
def start(update, context):
context.bot.send_message(chat_id = update.effective_chat.id, text=f"hey,{update.effective_chat.username}!")
def unknown(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, didn't understand")
unknown_handler = MessageHandler(Filters.command, unknown)
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(unknown_handler)
updater.start_polling()
I kept changing the message text in the code and then executing it, checking what happens in the bot. What I ended up is every time I do start, the bot sends any of the old message versions together with new one. I revoked the token and it helped.
My question is what happens under the hood and why the latest version of the code doesn't replace the old ones? Also, how can I deal with it without having to revoke token every time?
I am trying to deploy a simple echo2 bot using webhook. It's working fine with polling. But, I want to use the bot using webhook. I didn't find any easy to understand answer for this question on internet.
What changes do I need to make in the below code to make the bot work using webhook..?
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi!')
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Help!')
def echo(update, context):
"""Echo the user message."""
update.message.reply_text(update.message.text)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
updater = Updater("TOKEN", use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()
You can just remove the updater.start_polling() call and use this instead.
Webhook for Heroku python-telegram-bot
Make sure you've already created your application on heroku.
Using the webhook:
# Use env variable PORT
PORT = int(os.environ.get("PORT", 3978))
updater.start_webhook(listen="0.0.0.0", port=PORT, url_path='TELEGRAM_TOKEN')
updater.bot.setWebhook('https://mywebhook/TELEGRAM_TOKEN')
Notes:
set your TELEGRAM_TOKEN received by the BotFather
set the relevant port (that might depend on the app running your webhook): the example above shows how to bind the port on Heroku
webhook must listen on HTTPS
I am recreating the basic telegram bot example from here, but I am having a slight problem.
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO)
logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi!')
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Help!')
def echo(update, context):
"""Echo the user message."""
update.message.reply_text(update.message.text)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
updater = Updater("YOUR TOKEN HERE",use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()
I am getting below error after executing the script:
$ python test1.py
Traceback (most recent call last):
File "test1.py", line 64, in <module>
main()
File "test1.py", line 39, in main
updater = Updater(TOKEN,use_context=True)
TypeError: __init__() got an unexpected keyword argument 'use_context'
use_context is available as of version 12 of python-telegram-bot. You can find out the version you have installed via pip show python-telegram-bot.
The simplest solution would be to just remove the parameter use_context, i.e. replace
updater = Updater("YOUR TOKEN HERE",use_context=True)
with
updater = Updater("YOUR TOKEN HERE")
The error is telling you that use_context is not a valid keyword argument for the Updater initializer. That argument is no longer supported in the 12th version of Python Telegram Bot.
I guess you did pip install python-telegram-bot==12.0.0b1 --upgrade to install the library and it's installing the version 12.0.0b1.
You can do again the installation by running the following commnads:
Uninstall the current version of python-telegram-bot: pip uninstall python-telegram-bot
Install the 11 version of the library: pip install python-telegram-bot==11
Also, if you don't want to change your library version and keep using python-telegram-bot 12.0.0, you can just remove the use_context argument from the instantiation of the Updater class.
This line:
updater = Updater("YOUR TOKEN HERE",use_context=True)
Would become:
updater = Updater("YOUR TOKEN HERE")
i am developing a python script for my telegram right now. The problem is:
How do I know when my bot is added to a group? Is there an Event or something else for that?
I want the Bot to send a message to the group he´s beeing added to which says hi and the functions he can.
I dont know if any kind of handler is able deal with this.
Very roughly, you would need to do something like this: register an handler that filters only service messages about new chat members. Then check if the bot is one of the new chat members.
from telegram.ext import Updater, MessageHandler, Filters
def new_member(bot, update):
for member in update.message.new_chat_members:
if member.username == 'YourBot':
update.message.reply_text('Welcome')
updater = Updater('TOKEN')
updater.dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, new_member))
updater.start_polling()
updater.idle()
With callbacks (preferred)
As of version 12, the preferred way to handle updates is via callbacks. To use them prior to version 13 state use_context=True in your Updater. Version 13 will have this as default.
from telegram.ext import Updater, MessageHandler, Filters
def new_member(update, context):
for member in update.message.new_chat_members:
if member.username == 'YourBot':
update.message.reply_text('Welcome')
updater = Updater('TOKEN', use_context=True) # use_context will be True by default in version 13+
updater.dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, new_member))
updater.start_polling()
updater.idle()
Please note that the order changed here. Instead of having the update as second, it is now the first argument. Executing the code below will result in an Exception like this:
AttributeError: 'CallbackContext' object has no attribute 'message'
Without callbacks (deprecated in version 12)
Blatantly copying from mcont's answer:
from telegram.ext import Updater, MessageHandler, Filters
def new_member(bot, update):
for member in update.message.new_chat_members:
if member.username == 'YourBot':
update.message.reply_text('Welcome')
updater = Updater('TOKEN')
updater.dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, new_member))
updater.start_polling()
updater.idle()