+.TelegramObject {telegram.bot}R Documentation

Constructing an Updater

Description

With + you can add any kind of Handler to an Updater's Dispatcher (or directly to a Dispatcher).

Usage

## S3 method for class 'TelegramObject'
e1 + e2

Arguments

e1

An object of class Updater or Dispatcher.

e2

An object of class Handler.

Details

See add_handler for further information.

Examples

## Not run: 
# You can chain multiple handlers
start <- function(bot, update) {
  bot$sendMessage(
    chat_id = update$message$chat_id,
    text = sprintf(
      "Hello %s!",
      update$message$from$first_name
    )
  )
}
echo <- function(bot, update) {
  bot$sendMessage(
    chat_id = update$message$chat_id,
    text = update$message$text
  )
}

updater <- Updater("TOKEN") + CommandHandler("start", start) +
  MessageHandler(echo, MessageFilters$text)

# And keep adding...
caps <- function(bot, update, args) {
  if (length(args > 0L)) {
    text_caps <- toupper(paste(args, collapse = " "))
    bot$sendMessage(
      chat_id = update$message$chat_id,
      text = text_caps
    )
  }
}

updater <- updater + CommandHandler("caps", caps, pass_args = TRUE)

# Give it a try!
updater$start_polling()
# Send '/start' to the bot, '/caps foo' or just a simple text

## End(Not run)

[Package telegram.bot version 3.0.0 Index]