Getting exception : AttributeError __aenter__

Question:

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

Asked By: aboka

||

Answers:

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.

Answered By: Comrade

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

Answered By: Jonathan Yuen

You can replace

print(bot.get_me())

with

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

This is how I solved my problem.

Answered By: Ali Yaghoubian