Search code examples
pythonattributeerrorpython-telegram-bot

'Updater' object has no attribute 'dispatcher'


I try to make a bot to send messages automatically every 1 minute and also if I write to the bot as the owner, it sends the message to all the channels that subscribe to this bot.

def send_to_channel(message):
    bot = Bot(token=BOT_TOKEN)
    bot.send_message(chat_id=CHANNEL_ID, text=message)

def send_random_ayah(context: CallbackContext):
    response = requests.get(QURAN_API_URL)
    if response.status_code == 200:
        data = response.json()
        ayah = data.get('verse_key', '')
        send_to_channel(f"Random Ayah: {ayah}")
    else:
        print(f"Failed to fetch Ayah. Status Code: {response.status_code}")

def main():
    updater = Updater(BOT_TOKEN, update_queue=None)
    dp = updater.dispatcher

    # Command handler to manually send a message to the channel
    dp.add_handler(CommandHandler("send", send_to_channel))

    # Job to send a random Ayah every 1 minute
    updater.job_queue.run_repeating(send_random_ayah, interval=2, first=0)

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
  main()

and I got this error

AttributeError                            Traceback (most recent call last)
<ipython-input-107-174f78df48e2> in <cell line: 1>()
      1 if __name__ == '__main__':
----> 2   main()

<ipython-input-106-8197ed041923> in main()
      2 def main():
      3     updater = Updater(BOT_TOKEN, update_queue=None)
----> 4     dp = updater.dispatcher
      5 
      6     # Command handler to manually send a message to the channel

AttributeError: 'Updater' object has no attribute 'dispatcher'`


I updated the library and I also reinstall it but it doesn't solve the problem. if you have any other tips for this please tell me


Solution

  • 'dispatcher' is no longer used in version 20.

    If you want to use your code then you can downgrade python-telegram-bot as:

    pip install python-telegram-bot==13.3
    

    If you are using v20 then you have to build your application differently.