【问题标题】:telegram bot reply message for buttons按钮的电报机器人回复消息
【发布时间】:2022-10-19 19:17:04
【问题描述】:

最近我使用 python 创建了一个电报机器人,并向机器人添加了键盘按钮功能。但是,我很难从机器人获得用户选择的按钮的回复。

button7 = KeyboardButton('About Us',request_contact= False)
keyboard2 = ReplyKeyboardMarkup(resize_keyboard = True, one_time_keyboard = True).row(button7)
@dp.message_handler(commands=['info'])
async def mood(message: types.Message):
    await message.reply('Do you wish to know about us??', reply_markup=keyboard2)

在这种情况下,我创建了一个名为“关于我们”的按钮,如果用户单击该按钮,我希望机器人使用 webbrowser.open 打开一个 url。谁能帮我解决这个问题?

【问题讨论】:

    标签: python keyboard telegram telegram-bot


    【解决方案1】:

    试试看:

    markup = types.InlineKeyboardMarkup()
    button1 = types.InlineKeyboardButton(text='Ukraine', url="https://en.wikipedia.org/wiki/Ukraine", callback_data='1')
    markup.add(button1)
    bot.send_message(message.from_user.id, 'Do you know?, parse_mode='Markdown', reply_markup=markup)
    

    【讨论】:

    • 感谢您之前的回答,当我们单击 url 按钮时,它可以帮助我重定向到一个页面。但我还有一个问题,如果用户点击特定按钮,我们如何从机器人获得文本回复?
    • 您对哪个按钮处理程序感兴趣?内联或回复按钮?
    • 我对内联按钮感兴趣
    【解决方案2】:

    文本(内嵌按钮)

    from aiogram import Bot, Dispatcher, executor, types
    
    bot = Bot(token='token')
    dp = Dispatcher(bot)
    
    
    @dp.message_handler(commands=['start'])
    async def welcome(message: types.Message):
        markup = types.InlineKeyboardMarkup()
        button1 = types.InlineKeyboardButton(text='Ukraine', url="https://en.wikipedia.org/wiki/Ukraine", callback_data='1')
        button2 = types.InlineKeyboardButton(text='Hi bot!', callback_data="1")
        markup.add(button1, button2)
    
        await bot.send_message(message.from_user.id, "Do you know?", parse_mode="Markdown", reply_markup=markup)
    
    
    @dp.callback_query_handler(lambda call: True)
    async def sendText(call: types.CallbackQuery):
        msg = ""
        if call.data == "1":
            msg = "Hi, programmer!"
    
        await call.message.edit_text(msg)
    
    
    if __name__ == '__main__':
        executor.start_polling(dp, skip_updates=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 2020-10-10
      • 2020-08-27
      • 2017-03-19
      • 2017-07-17
      • 2021-09-09
      相关资源
      最近更新 更多