Webhook: Building a Telegram Bot with a Webhook

View source: R/webhook.R

WebhookR Documentation

Building a Telegram Bot with a Webhook

Description

This class, which employs the class Dispatcher, provides a front-end to class Bot to the programmer, so you can focus on coding the bot. Its purpose is to receive updates via webhook from Telegram and to deliver them to said dispatcher. The dispatcher supports Handler classes for different kinds of data: Updates from Telegram, basic text commands and even arbitrary types. See add (+) to learn more about building your Webhook.

Usage

Webhook(
  webhook_url,
  token = NULL,
  base_url = NULL,
  base_file_url = NULL,
  request_config = NULL,
  certificate = NULL,
  max_connections = NULL,
  allowed_updates = NULL,
  ip_address = NULL,
  drop_pending_updates = FALSE,
  verbose = FALSE,
  bot = NULL
)

is.Webhook(x)

Arguments

webhook_url

Webhook HTTPS url to send updates to. The url is conventionally suffixed with the /webhook path.

Note: The url must be publicly accessible, since Telegram will need to make HTTP POST requests to the end-point for each update.

For example, if you are deploying to Heroku, you can use the app's hostname, such as https://[name of app].herokuapp.com/webhook, or a custom hostname for a domain that belongs to you, such as https://app.yourcustomdomain.com/webhook.

token

(Optional). The bot's token given by the BotFather.

base_url

(Optional). Telegram Bot API service URL.

base_file_url

(Optional). Telegram Bot API file URL.

request_config

(Optional). Additional configuration settings to be passed to the bot's POST requests. See the config parameter from httr::POST for further details.

The request_config settings are very useful for the advanced users who would like to control the default timeouts and/or control the proxy used for HTTP communication.

certificate

(Optional). Upload your public key certificate so that the root certificate in use can be checked. See Telegram's self-signed guide for details.

max_connections

(Optional). Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40. Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput.

allowed_updates

(Optional). String or vector of strings with the types of updates you want your bot to receive. For example, specify c("message", "edited_channel_post", "callback_query") to only receive updates of these types. See Update for a complete list of available update types. Specify an empty string to receive all updates regardless of type (default). If not specified, the previous setting will be used.

Please note that this parameter doesn't affect updates created before the call to the get_updates, so unwanted updates may be received for a short period of time.

ip_address

(Optional). The fixed IP address which will be used to send webhook requests instead of the IP address resolved through DNS.

drop_pending_updates

(Optional). Pass True to drop all pending updates.

verbose

(Optional). If TRUE, prints status of the polling. Default is FALSE.

bot

(Optional). A pre-initialized Bot instance.

x

Object to be tested.

Format

An R6Class object.

Details

You must supply the webhook_url and either a bot or a token argument.

The webhook_url must be publicly accessible, since Telegram will need to make HTTP POST requests to the end-point for each update.

Security Note: Webhook encapsulates generating a secret_token which is used to validate that the request comes from a webhook set by you.

Methods

start_server

Starts listening for updates from Telegram.

stop_server

Stops listening for updates.

running

Returns TRUE when listening for updates.

References

Bots: An introduction for developers, Telegram Bot API and Marvin's Marvellous Guide to All Things Webhook

Examples

## Not run: 
webhook <- Webhook("https://example.com/webhook", "TOKEN")

# In case you want to set a proxy
webhook <- Webhook(
  webhook_url = "https://example.com/webhook",
  token = "TOKEN",
  request_config = httr::use_proxy(...),
  verbose = TRUE
)

# Add a handler
start <- function(bot, update) {
  bot$sendMessage(
    chat_id = update$message$chat_id,
    text = sprintf(
      "Hello %s!",
      update$message$from$first_name
    )
  )
}
webhook <- webhook + CommandHandler("start", start)

# Start polling
webhook$start_server() # Send '/start' to the bot

## End(Not run)

telegram.bot documentation built on Sept. 7, 2022, 5:07 p.m.