【发布时间】:2020-11-14 11:41:18
【问题描述】:
我正在制作 Telegram Bot,我有两个内联按钮。当用户单击按钮机器人会回答“嗨@username”时,我需要这样做。该怎么做?
【问题讨论】:
标签: python button telegram inline telegram-bot
我正在制作 Telegram Bot,我有两个内联按钮。当用户单击按钮机器人会回答“嗨@username”时,我需要这样做。该怎么做?
【问题讨论】:
标签: python button telegram inline telegram-bot
您收到的任何消息对象都包含“from”作为用户对象的键,该对象包含“first_name”和其他属性 (https://core.telegram.org/bots/api#user)。
使用我推荐的 aiogram 库,您可以执行以下操作:
@dp.message_handler()
async def get_message(message):
await message.answer(f'Hello, @{message.user.username}')
【讨论】:
我找到了一种方法来做到这一点!您需要添加username = "@" + str(message.from_user.username),然后添加bot.send_message(message.chat.id, "Hi " + username)
【讨论】:
+作为字符串,改用f-strings