skip to Main Content

I get this exception: "Exception has occurred: AttributeError __aenter__" in line async with bot:. (I entered the token). Please help

import asyncio
import telegram


async def main():
    bot = telegram.Bot("token")
    async with bot:
        print(await bot.get_me())


if __name__ == '__main__':
    asyncio.run(main())

I found this code here: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Introduction-to-the-API

3

Answers


  1. Probobly that exception comming because you installed python-telegram-bot version 13.x.

    try to:

    pip freeze

    for check that, and if that true you have to make

    pip install python-telegram-bot –pre

    because only unstable version use async.

    Login or Signup to reply.
  2. Think that’s because you installed v13 (pip install python-telegram-bot) and the example you are looking at is from the v20 Github wiki.

    Head over here for a quick start guide for v13 wrapper: https://github.com/python-telegram-bot/v13.x-wiki/wiki/Extensions-%E2%80%93-Your-first-Bot

    Login or Signup to reply.
  3. You can replace

    print(bot.get_me())

    with

    async with bot: print(await bot.get_me())

    This is how I solved my problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search