首页 > 解决方案 > 使用 R 中的 telegram.bot 包向机器人添加按钮

问题描述

我正在使用 telegram.bot 包开发电报机器人。我需要从机器人用户那里获取一些特定的数据。例如,我希望用户输入他/她的姓名、出生月份等。所以我想我需要向他们显示某种名称按钮,另一个按钮显示出生月份,然后我可以使用该数据进行处理和显示结果返回给用户。你能帮我处理这个案子吗?谢谢您的帮助。

标签: rtelegram

解决方案


您可以在下面找到有关如何使用github 问题中的按钮的示例:

library(telegram.bot)

updater <- Updater(token = "TOKEN")

inline <- function(bot, update) {
  
  text <- "Yes or no?"
  IKM <- InlineKeyboardMarkup(
    inline_keyboard = list(
      list(
        InlineKeyboardButton("Yes", callback_data = 'yes'),
        InlineKeyboardButton("No", callback_data = 'no')
      )
    )
  )
  
  # Send Inline Keyboard
  bot$sendMessage(update$message$chat_id, text, reply_markup = IKM)
}

updater <- updater + CommandHandler('inline', inline)

answer_cb <- function(bot, update) {

  data <- update$callback_query$data
  
  # Send Custom Keyboard
  bot$sendMessage(chat_id = update$callback_query$message$chat$id, 
                  text = paste0("Hello"))
  
  
  bot$answerCallbackQuery(callback_query_id = update$callback_query$id,
                          text = paste("Answer recorded:", data))
}
    
updater <- updater + CallbackQueryHandler(answer_cb)

updater$start_polling()

一旦它开始轮询,发送“/inline”给机器人来测试它。


推荐阅读