skip to Main Content

I am making Telegram Bot and I have two inline buttons. I need to do that when the user clicks on a button bot would answer like "Hi @username". How to do that?

2

Answers


  1. Any message objects that you receive contains ‘from’ as a key to the User object which contains ‘first_name’ and other attributes (https://core.telegram.org/bots/api#user).

    With the aiogram library, which I recommend, you can do something like this:

    @dp.message_handler()
    async def get_message(message):
        await message.answer(f'Hello, @{message.user.username}')
    
    Login or Signup to reply.
  2. I found a way to do this! You need to add username = "@" + str(message.from_user.username) and then bot.send_message(message.chat.id, "Hi " + username)

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