Adding buttons to a bot using telegram.bot package in R

Viewed 228

I'm developing a telegram bot using telegram.bot package. I need to get some specific data from bot users. for example I want user to enter his/her name, month of birth etc. So I think I need to show them some kind of button for name, another button for month of birth and then I can use that data for processing and showing the results back to the user. Can you please help me with this case?? Thank you for your help.

1 Answers

below you can find an example on how to use buttons from a github question:

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()

Once it starts polling, send "/inline" to the bot to test it.

Related