使用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()
答案 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}}。