ebeneditos / telegram.bot

Develop a Telegram Bot with R
https://ebeneditos.github.io/telegram.bot/
GNU General Public License v3.0
107 stars 24 forks source link

Add Webhook Functionality #27

Closed virtualstaticvoid closed 2 years ago

virtualstaticvoid commented 2 years ago

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:

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:

Proposed Changes

virtualstaticvoid commented 2 years ago

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