Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

AttributeError: 'Updater' object has no attribute 'dispatcher': how to fix it

AttributeError: 'Updater' object has no attribute 'dispatcher' means code written for python-telegram-bot v13 is running on v20 or newer. Version 20 moved to asyncio and replaced the Updater and Dispatcher pair with Application: build it with ApplicationBuilder().token(...).build(), register handlers with application.add_handler, make the handlers async, and start with application.run_polling(). Pinning python-telegram-bot==13.15 only buys time.

By Livops team, the people who build and run LivopsUpdated

The bot stops at startup with this at the end of the traceback:

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

The code was written for python-telegram-bot (PTB) v13 or older, and the installed PTB is v20 or newer. This page explains what v20 changed, shows the same bot before and after the rewrite, lists the errors that come from the same change, and what pinning v13 costs.

What does the error mean?

Version 20 rebuilt PTB’s structure. Updater is no longer the entry point of a PTB program, and the Dispatcher class was replaced by a new class, Application[1]. The Updater’s only job now is to fetch updates: it takes exactly two arguments, bot and update_queue, and has only those attributes[1][2][6]. There is no updater.dispatcher to add handlers to.

The other big change is asyncio. PTB no longer uses threads; every handler and job callback must be a coroutine function, async def, and every method of telegram.Bot must be awaited[1].

Which of the errors you see

v13 code breaks in several places at once, and Python stops at the first one it reaches. So the same cause shows up under different messages, depending on how the old code is written. Run on current PTB, they read:

Error The v13 line
ImportError: cannot import name 'Filters' from 'telegram.ext' from telegram.ext import ..., Filters: usually the first, because it sits in the import line
TypeError: Updater.__init__() got an unexpected keyword argument 'token' Updater(token=TOKEN)
TypeError: Updater.__init__() got an unexpected keyword argument 'use_context' Updater(TOKEN, use_context=True)
TypeError: Updater.__init__() missing 1 required positional argument: 'update_queue' Updater(TOKEN)
AttributeError: 'Updater' object has no attribute 'dispatcher' updater.dispatcher, once the Updater was built with two arguments, or application.updater.dispatcher
ImportError: cannot import name 'ParseMode' from 'telegram' from telegram import ParseMode, now from telegram.constants import ParseMode[1]

On Python 3.9 and older the TypeError lines read __init__() without the class name. Adding the update_queue argument that the last TypeError asks for does not help: it only moves the failure to updater.dispatcher. All of them have one fix, the rewrite below.

Which version is installed?

Run this with the same Python the bot runs with:

pip show python-telegram-bot

PTB 20.0 came out on 1 January 2023, and 13.15, the last v13 release, a month before it[7]. A requirements.txt that says only python-telegram-bot has installed v20 or newer on every fresh install since, while an old machine still has 13.x. That is why the same code runs in one place and fails in another.

The fix: move the bot to Application

A v13 bot, close to the project’s own v13.15 example[5]. This is the code that fails:

import os

from telegram import Update
from telegram.ext import CallbackContext, CommandHandler, Filters, MessageHandler, Updater


def start(update: Update, context: CallbackContext) -> None:
    update.message.reply_text("Hi!")


def echo(update: Update, context: CallbackContext) -> None:
    update.message.reply_text(update.message.text)


def main() -> None:
    updater = Updater(os.environ["BOT_TOKEN"])
    dispatcher = updater.dispatcher
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
    updater.start_polling()
    updater.idle()


if __name__ == "__main__":
    main()

On current PTB it stops at the Filters import first; each line it reaches after that is one of the errors in the table above.

The same bot for v20 and later, following the current example[4]:

import logging
import os

from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes, MessageHandler, filters


async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text("Hi!")


async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text(update.message.text)


def main() -> None:
    application = ApplicationBuilder().token(os.environ["BOT_TOKEN"]).build()
    application.add_handler(CommandHandler("start", start))
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
    application.run_polling(allowed_updates=Update.ALL_TYPES)


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    main()

What changed, line by line:

  • ApplicationBuilder().token(...).build() instead of Updater(TOKEN). The Application is created with a builder: you set what you need on it, then call build()[1]. Application.builder() returns the same builder[3].
  • application.add_handler(...) instead of dispatcher.add_handler(...). The handler classes, CommandHandler and MessageHandler, keep their names.
  • async def handlers, and await on every Bot call, reply_text included[1]. A call without await is never sent; Python only prints RuntimeWarning: coroutine '...' was never awaited[13].
  • filters.TEXT instead of Filters.text. telegram.ext.Filters no longer exists; use the module telegram.ext.filters. Filters that need no arguments are in capitals (filters.TEXT, filters.COMMAND), filter classes that take arguments are in CamelCase (filters.User), and related filters are grouped: any document is filters.Document.ALL[1].
  • ContextTypes.DEFAULT_TYPE as the type of context, as the current examples write it[4].
  • run_polling() instead of start_polling() plus idle(). It initializes and starts the app, polls, and blocks until it receives a stop signal, by default SIGINT, SIGTERM or SIGABRT outside Windows, then shuts down cleanly[3]. Call it from a plain def main(), as above, not from inside asyncio.run(): why is in This event loop is already running.

Three more things old code often uses:

  • Constants and errors moved into modules. from telegram import ParseMode becomes from telegram.constants import ParseMode, and errors are imported from telegram.error[1].
  • JobQueue is an optional extra. Since v20 only httpx is installed by default[1]. If the bot schedules jobs, put python-telegram-bot[job-queue] in requirements.txt[12].
  • There is a transition script. The PTB wiki links a regex-based script that does part of the search and replace. Its authors say it is no substitute for reading the guide and fixing the code by hand[1].

Pinning 13.15: the short-term option

The quick way out is to install the version the code was written for:

python-telegram-bot==13.15

The bot runs again, and the cost grows with time:

  • No more v13 releases. 13.15, from December 2022, is the last; everything since is v20 or later[7].
  • Old pinned dependencies. 13.15 pins tornado==6.1 and APScheduler==3.6.3 exactly[8]. tornado 6.1 ships ready-built wheels only up to Python 3.9[9], so on a newer Python pip builds it from source. When that fails, see Failed building wheel.
  • Python 3.13 breaks it. 13.15 does import imghdr in telegram/files/inputfile.py[10], a module loaded as soon as you import telegram. Python 3.13 removed imghdr from the standard library[11], so on 3.13 or newer the bot stops at ModuleNotFoundError: No module named 'imghdr' before any of your code runs.

If you pin, pin Python 3.12 or older with it, and use the time to do the rewrite. The current PTB release needs Python 3.10 or newer[7].

On Livops

What Livops does with a python-telegram-bot bot, and what it does not:

  • It installs what requirements.txt names. An unpinned python-telegram-bot line gets the current release on every deploy, the same as on any fresh machine. Once the code is rewritten, write the major version you tested, for example python-telegram-bot>=22,<23.

  • The error stops the deploy as start_failed. The build passes, the bot fails as it starts, and the traceback is on the project’s Logs page. A bot’s previous version is stopped before the new one starts, because two copies polling Telegram would conflict, so the bot is down until a deploy succeeds. Run the rewritten bot locally first.

  • You choose the Python. Livops runs Python 3.10 to 3.14 and reads the version from a .python-version file at the project root. For the 13.15 stopgap, use 3.12 or older:

    3.12
  • The start command. Livops finds bot.py, main.py, app.py or run.py by itself. To name it, write .venv/bin/python bot.py in Settings → Start, or a Procfile:

    worker: .venv/bin/python bot.py
  • The token is a variable. Put it under Settings → Variables as BOT_TOKEN, which the code above reads.

A bot costs 19 credits a month (19,000 so’m). A VPS is the better fit when you run many bots on one machine, or a bot needs root for system packages of its own; on Livops a push is the deploy and restarts are the platform’s job, but there is no root. Hosting details are in python-telegram-bot hosting, and startup failures in The app did not start.

Questions

Why did this start without any change to my code?

Your requirements.txt names python-telegram-bot without a version, so a fresh install picked the current release, which is v20 or newer. The code was written for v13. Check the installed version with pip show python-telegram-bot.

Where did the Dispatcher go?

Version 20 replaced the Dispatcher class with Application, which is now the entry point of the program. Handlers are added to the application with application.add_handler, and the Updater only fetches updates.

Is Filters.text the same as filters.TEXT?

Yes. telegram.ext.Filters no longer exists; use the lowercase module telegram.ext.filters. Filters that need no arguments are written in capitals, such as filters.TEXT and filters.COMMAND.

What replaces updater.idle()?

application.run_polling(). It starts the bot, keeps it running until it receives a stop signal such as Ctrl+C or SIGTERM, and shuts it down cleanly.

Is pinning python-telegram-bot 13.15 a real fix?

It is a stopgap. 13.15 is the last v13 release, from December 2022. It pins old dependencies and cannot be imported on Python 3.13 or newer, because it imports the imghdr module that Python 3.13 removed.

Do I have to make every handler async?

Yes. From v20 every handler and job callback must be a coroutine function, async def, and every Bot method call must be awaited. A call without await never runs, and Python prints a RuntimeWarning that the coroutine was never awaited.

Does Livops rewrite v13 code for me?

No. Livops installs what requirements.txt names and starts your bot. When the bot fails at startup, the deploy stops as start_failed and the traceback is on the Logs page; the rewrite is yours.

Sources

  1. python-telegram-bot wiki — Transition guide to Version 20.0 (github.com). Checked .
  2. python-telegram-bot docs — telegram.ext.Updater (docs.python-telegram-bot.org). Checked .
  3. python-telegram-bot docs — telegram.ext.Application (docs.python-telegram-bot.org). Checked .
  4. python-telegram-bot — examples/echobot.py (github.com). Checked .
  5. python-telegram-bot v13.15 — examples/echobot.py (github.com). Checked .
  6. python-telegram-bot source — telegram/ext/_updater.py (github.com). Checked .
  7. PyPI — python-telegram-bot, release history (pypi.org). Checked .
  8. PyPI — python-telegram-bot 13.15 (pypi.org). Checked .
  9. PyPI — tornado 6.1, files (pypi.org). Checked .
  10. python-telegram-bot v13.15 source — telegram/files/inputfile.py (github.com). Checked .
  11. Python docs — What's New In Python 3.13 (docs.python.org). Checked .
  12. python-telegram-bot — README, optional dependencies (github.com). Checked .
  13. Python docs — Developing with asyncio (docs.python.org). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your bot

Got a question? — @livops_uz