R:从Telegram Bot阅读键盘答案

时间:2018-11-26 22:02:15

标签: r telegram-bot r-telegram-bot

使用R的telegram.bot包时,我不知道如何从键盘上读取用户给出的答案。这是最小的示例代码:

library(telegram.bot)

updater <- Updater(token = Sys.getenv('TOKEN'))
dispatcher <- updater$dispatcher

start_keyboard <- ReplyKeyboardMarkup(
  keyboard = list(
    list(KeyboardButton('Choice_1')),
    list(KeyboardButton('Choice_2'))
  ),
  one_time_keyboard = TRUE
)

start <- function(bot, update) {
  bot$sendMessage(chat_id = update$message$chat_id,
                  text = "Hello!",
                  reply_markup = start_keyboard
  )
}

dispatcher$add_handler(CommandHandler('start', start))

updater$start_polling()

1 个答案:

答案 0 :(得分:1)

ReplyKeyboardMarkup的答案由用户以纯文本形式发送,因此您可以针对这种情况构建文本处理程序:

start_handler <- function(bot, update){
  text <- "foo"
  if (update$message$text == "Choice_1"){
    text <- "Response 1"
  }else if (update$message$text == "Choice_2"){
    text <- "Response 2"
  }
  bot$sendMessage(chat_id = update$message$chat_id, text = text)
}

dispatcher$add_handler(MessageHandler(start_handler, MessageFilters$text))

updater$start_polling()

注意:如果您使用InlineKeyboardMarkup,答案将不再是文本,而是应通过answerCallbackQuery处理,您可以使用特定的{ {1}}。