r - 使用 R 中的 telegram.bot 包向机器人添加按钮
问题描述
我正在使用 telegram.bot 包开发电报机器人。我需要从机器人用户那里获取一些特定的数据。例如,我希望用户输入他/她的姓名、出生月份等。所以我想我需要向他们显示某种名称按钮,另一个按钮显示出生月份,然后我可以使用该数据进行处理和显示结果返回给用户。你能帮我处理这个案子吗?谢谢您的帮助。
解决方案
您可以在下面找到有关如何使用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”给机器人来测试它。
推荐阅读
- java - 非活动视图中的警报对话框
- java - 构建我的 Java Swing 应用程序。IE何时何地使用类而不是
- html - 将文本对齐到按钮单选的右侧
- c# - 控制所有呈现的模式 iOS
- python - 魔术 8 球程序循环未按预期工作
- r - 在 R 中循环查找上一个匹配项
- r - R Highcharter Drilldown Set Ymin & Ymax + Title for each layer
- twitter-bootstrap - Bootstrap 3 到 4 迁移(模式停止出现)
- android - Firebase 返回 null 但数据存在
- c# - Xamarin 形式 - MVVM 可观察集合始终为空