telegram.bot icon indicating copy to clipboard operation
telegram.bot copied to clipboard

Add Webhook Functionality

Open virtualstaticvoid opened this issue 1 year ago • 1 comments

This PR adds the capability to receive Telegram updates via Webhook delivery.

Following the pattern established in the Updater class, the Webhook class follows similar steps for setup and running.

# define handlers as before
start <- function(bot, update) {
 bot$sendMessage(
   chat_id = update$message$chat_id,
   text = sprintf(
     "Hello %s!",
     update$message$from$first_name
   )
 )
}

# instantiate Webhook
webhook <- Webhook("https://example.com/webhook", Sys.getenv("TOKEN"), verbose = TRUE)

# wire up handler
webhook <- webhook + CommandHandler("start", start)

# start polling
webhook$start_server()

# OR, start polling, binding to all interfaces on port 3000
webhook$start_server(host = "0.0.0.0", port = 3000)

# stop polling
webhook$stop_server()

A working example application can be found at virtualstaticvoid/heroku-telegram-bot-webhook which is deployed to Heroku.

Start a chat with HerokuRBot to try it out. Send /plot command to receive a simple pie chart rendered in R.

All Submissions:

  • [x] Have you followed the guidelines in our Contributing document?
  • [x] Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. [x] Does your submission pass tests?
  2. [x] Have you lint your code locally prior to submission?
  3. [x] Documentation has been generated?

Changes to Core Features:

  • [x] Have you added an explanation of what your changes do and why you'd like us to include them?
  • [x] Have you written new tests for your core changes, as applicable?
  • [x] Have you successfully ran tests with your changes locally?

Proposed Changes

  • Implements Webhook capabilities as per Marvin's Marvellous Guide to All Things Webhook.
  • Extends the Bot$set_workhook method to include all supported arguments; including ip_address, drop_pending_updates and secret_token.
  • Fixes Bot.request method so that when an error occurs, it is reported with the response text, which helps when debugging.
  • Bumps the package version to v2.4.3

virtualstaticvoid avatar Aug 26 '22 13:08 virtualstaticvoid

I realised I haven't updated the main README page to include details of the new Webhook class, so I will add that shortly.

virtualstaticvoid avatar Aug 26 '22 13:08 virtualstaticvoid