# Fix "ImportError: cannot import name 'Filters' from 'telegram.ext'"

Yes. An agent ports a python-telegram-bot 13 bot to the asyncio API of version 20 and later, from the Filters import to every handler, and tests it offline.

[Run this on Rush](https://cloud.getrush.ai/run/fix-telegram-ext-filters-import-error/python-telegram-bot/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

python-telegram-bot 20 rewrote the library around asyncio, and the class `Filters` did not survive the rewrite. Its replacement is the lowercase module `telegram.ext.filters`, with constants such as `filters.TEXT` and `filters.PHOTO`. A bot written for version 13 that installs the library unpinned now dies on its import line with `ImportError: cannot import name 'Filters' from 'telegram.ext'`, and current Python adds a hint: `Did you mean: 'filters'?` Following the hint fixes that line and exposes the next one, because the import error is only the first of several changes on the path to a running bot.

LuisMayo/ace-attorney-telegram-bot, which turns forwarded chat messages into courtroom-scene videos, has the typical shape. `requirements.txt` lists `python-telegram-bot` with no version. Line 25 of `main.py` imports `Updater, CommandHandler, MessageHandler, Filters, CallbackContext`; line 63 builds `Updater(token, use_context=True)`; the handlers are registered on `updater.dispatcher`; and line 73 filters with `(Filters.text | Filters.photo | Filters.sticker) & ~Filters.command`. With version 22.8 installed, the import fails first. After it, `Updater(token, use_context=True)` is a `TypeError`, since `Updater` now takes a bot and an update queue, and `updater.dispatcher` and `updater.idle()` no longer exist.

The agent ports the entry point to the `Application` API: `ApplicationBuilder().token(token).build()`, handlers added to the application, `run_polling()` in place of `start_polling()` and `idle()`. The filter expression becomes `(filters.TEXT | filters.PHOTO | filters.Sticker.ALL) & ~filters.COMMAND`. Every callback becomes `async def`, and every bot call inside it gains `await`, because `reply_text`, `send_message` and `send_chat_action` are coroutines now. A call left without `await` raises no exception. It returns a coroutine that is never run, and the bot goes silent, so the agent searches for each one instead of waiting for an error.

Then it follows the imports into the other modules, where this repository has changes no import error announces. `msg_queue.py` imports `ChatAction` from `telegram`, and that name now lives in `telegram.constants`. It reads `update.message.forward_from`, an attribute that is gone in favour of `forward_origin`. It schedules work with `threading.Timer` and calls bot methods from that thread, which has to move onto the event loop, with the blocking video render pushed to a worker thread. `message.py` calls `newFile.download(file_name)`, now `await file.download_to_drive(file_name)`. The agent has no bot token, so it verifies offline: it builds the application with a placeholder token, which contacts nobody, and drives each handler with constructed `Update` objects and a stubbed bot.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This Telegram bot fails at startup with ImportError: cannot import name 'Filters' from 'telegram.ext'. It was written for python-telegram-bot 13 and now installs a 20+ release. Read the dependency files and report how python-telegram-bot is specified and which Python version the project targets. In a clean virtual environment install the dependencies and run the entry point far enough to save the traceback and the installed version. Port the bot to the current python-telegram-bot API; do not pin back to version 13. Replace Updater and Dispatcher with an Application built by ApplicationBuilder, replace Filters with the telegram.ext.filters module, make every handler and callback async, and await every bot and message method. Search all modules, not only the entry point, for: imports that moved (for example ChatAction and ParseMode to telegram.constants), removed attributes (for example forward_from, replaced by forward_origin), File.download, job queue usage, and any use of threads or timers that call the bot; move those onto the event loop and run blocking work with asyncio.to_thread. List every call you changed with file and line. After the port, search for any coroutine call that is not awaited. If a heavy dependency that is not needed to test message handling cannot be installed, stub it and say so. Do not use a real bot token and do not contact Telegram: build the Application with a placeholder token and write tests that pass constructed Update objects to each handler with a mocked bot, covering every filter branch. Pin python-telegram-bot to the major version you tested. Open a pull request with the list of changes, the test output, and what could not be verified without a token.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that touches every module that talks to Telegram, not just the import line. The description groups the changes by kind: names that moved, the `Updater` to `Application` rewrite, callbacks made `async`, calls that gained `await`, and threading replaced by tasks. Each entry has its file and line, so a reviewer can walk the list against the diff. `requirements.txt` gains a bound on the major version the port was tested with.

The new tests are the part to read closely. They feed text, photo, sticker and command updates to the handlers and assert on the calls made to a mocked bot, which shows that the filters still route messages the way the version 13 expression did and that no reply was left as an unawaited coroutine. They do not show that Telegram accepts the requests, and the description says which behaviour remains untested for lack of a token: polling, file downloads and uploads.

A reviewer finishes the check the agent could not do: run the branch with the real token in a test chat and send one message of each kind. If something differs, a follow-up message to the same session, with the traceback pasted in, continues from the sandbox as it was left.

## Run it on every bot crash

A bot that fails on import exits at once, and whatever restarts it, systemd, a container platform or a process manager, sees a crash loop. Have that supervisor's failure hook post the last lines of the log to a handler on your server, and let the handler call a `webhook` trigger on an automation in the bot's Project, with `Authorization: Bearer` and the trigger's secret. The log text arrives after the automation's prompt as event data, and the prompt tells the agent to act only on import errors and attribute errors from the `telegram` package.

A crash loop delivers the same failure many times. Send an `Idempotency-Key` header to the trigger's URL built from the bot's name and the first line of the traceback, so repeated deliveries of one failure are deduplicated, and keep the automation's `max_runs_per_hour` low.

## Why run it on Rush?

This is a port, not a patch, and its failure mode is silence. An unawaited `reply_text` raises no exception; the bot starts, receives updates and answers nothing. Catching that takes tests that drive every handler and inspect what was sent, which nobody writes by hand for a small bot. An agent writes them as part of the run, executes them in a sandbox with the library version it is porting to, and leaves the output in the session next to the diff.

The sandbox also takes the install problem off your machine. Bots like the one above pull in rendering and media libraries that are slow to build and unrelated to the Telegram API. The agent can install what it needs, stub what it does not, and say which is which, while your own environment stays as it is. No token ever enters the run, so nothing it does can reach your users.

## Frequently asked questions

### Can I pin python-telegram-bot back to 13.15 instead?

On an old enough Python, yes. In a check, version 13.15 imported on Python 3.11 only with a setuptools release that still provides `pkg_resources`, failed on Python 3.12 inside its bundled urllib3, and failed on Python 3.13 on `import imghdr`. Pinning buys time on 3.11 and nothing after it.

### What replaces Filters.text and Filters.command?

Constants in the `filters` module: `filters.TEXT`, `filters.COMMAND`, `filters.PHOTO`, and `filters.Sticker.ALL` for stickers. They combine with `|`, `&` and `~` as before, so an expression keeps its shape and only the names change.

### Why did Updater(token, use_context=True) stop working?

`use_context` was a switch between two callback styles in version 13, and the old style is gone. In current releases `Updater` takes a bot and an update queue and is rarely constructed by hand. `ApplicationBuilder().token(token).build()` creates the bot, the updater and the handler registry together.

### Does every handler have to become async?

Yes. The library calls handlers as coroutines, and the methods they use to reply are coroutines too. Blocking work inside a handler, such as rendering a video, should run through `asyncio.to_thread` so the bot keeps answering other chats meanwhile.

### How can the agent test a bot without a token?

Building an `Application` does not contact Telegram, and a handler is a function of an `Update` and a context. The tests construct updates in code and replace the bot's network methods with mocks. Live behaviour, such as polling and file transfer, is listed in the pull request as unverified.

## More jobs like this: Fix a bug from an alert

- [Fix "Provider registry.terraform.io/hashicorp/template v2.2.0 does not have a package available for your current platform, darwin_arm64"](https://getrush.ai/run/fix-template-provider-darwin-arm64/terraform/)
- [Fix "AttributeError: module 'tensorflow' has no attribute 'Session'" in TensorFlow 2](https://getrush.ai/run/fix-tensorflow-session-attribute-error/tensorflow/)
- [Fix ClientClosedError: The client is closed in a Node.js Redis App](https://getrush.ai/run/fix-the-client-is-closed-error/node-redis/)
- [Fix "DeprecationWarning: The 'warn' method is deprecated, use 'warning' instead" in a Python Library](https://getrush.ai/run/fix-the-warn-method-is-deprecated-use-warning-instead/logging/)
- [Fix "AttributeError: 'Thread' object has no attribute 'isAlive'" on Python 3.9+](https://getrush.ai/run/fix-thread-isalive-attribute-error/threading/)
- [Fix "AttributeError: module 'time' has no attribute 'clock'" on Python 3.8+](https://getrush.ai/run/fix-time-clock-attribute-error/time/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
