skip to Main Content

I am a beginner with python-telegram-bot, I have installed version the latest version 20.2.

I installed a package "python-telegram bot" but I got an import error upon running the code

ImportError: cannot import name "Dispatcher" from telegram.ext

I delved into the python-telegram-bot package and discovered that there is no module called Dispatcher, but several online tutorials directly imported Dispatcher from telegram.ext.

In my attempt to build a bot using Telegram, I have tried several times, but I have still received a message that says it cannot import the name ‘Dispatcher’ from the extension telegram.ext.As far as I am concerned, this error should be solved as soon as possible and I would appreciate if you could help me with it.

2

Answers


  1. You seem to be using a version >=20.0 of python-telegram-bot but your code is written for version <=13.15. Please either upgrade your code to v20 by reading the release notes and the transition guide or install a version of PTB that is compatible with your code base.
    Please note that the PTB team does not provide support for versions older than v20 anymore.


    Disclaimer: I’m currently the maintainer of python-telegram-bot.

    Login or Signup to reply.
  2. According to the latest documentation suggested by @CallMeStag, you need to do some tweaking to your code lol. There’s a lot of upgrade happening with the api. Here is a sample code i tried:

    import telegram
    from telegram.ext import Updater, CommandHandler, MessageHandler, Application, ContextTypes
    
    import requests
    Token = 'My Token'
    
    application = Application.builder().token(Token).build()
    
    
    
    async def start(update: telegram.Update, context: ContextTypes.DEFAULT_TYPE):
        await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
    
    
    async def help(update: telegram.Update, context: ContextTypes.DEFAULT_TYPE):
        await context.bot.send_message(chat_id=update.effective_chat.id, text='''
        /start -> Welcome to tryTalk url shortener
        /help -> Send me a url you'll like to shorten and I'll reply back with a short version of your url
        /shorten -> Enter a url 
        /contact -> Contact the bot creator
        ''')
    
    
    async def shorten(update: telegram.Update, context: ContextTypes.DEFAULT_TYPE):
        await context.bot.send_message(chat_id=update.effective_chat.id, text="Enter your URL")
    
    
    async def contact(update: telegram.Update, context: ContextTypes.DEFAULT_TYPE):
        await context.bot.send_message(chat_id=update.effective_chat.id, text='''
        email: [email protected]
        linkedin: https://www.linkedin.com/in/prince-odoi/
        ''')
    
    
    
    
    application.add_handler(CommandHandler('contact', contact))
    application.add_handler(CommandHandler('help', help))
    application.add_handler(CommandHandler('shorten', shorten))
    application.add_handler(CommandHandler('start',start))
    
    application.run_polling()
    application.idle()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search