Search code examples
pythonpython-asynciotelegramtelegram-botaiogram

How to get bot's username in aiogram?


For the sake of studying the capabilities of the Telegram API, the question arose, how to get the @username of a bot in a telegram using aiogram?

I tried username = bot.get_me().username but gives the following error: AttributeError: 'coroutine' object has no attribute 'username' How can this be fixed?


Solution

  • You can get the username of the bot by calling get_me on the Bot object.

    The returned objects hold the username you are looking for.


    Example code:

    async def main():
        bot = Bot(token='859163076:AAEjLUL8Lx67blablalblalblalbal')
        info = await bot.get_me();
        name = info.username
    
        print(name) 
    
    asyncio.run(main())