From 3ed45fb624bb6dbab54820524c54a22988ac1980 Mon Sep 17 00:00:00 2001 From: Daniil Yastremskiy Date: Sun, 29 Jan 2017 18:55:11 +0300 Subject: [PATCH] examples: Add webhook example for Heroku (#271) --- examples/herokuWebHook.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/herokuWebHook.js diff --git a/examples/herokuWebHook.js b/examples/herokuWebHook.js new file mode 100644 index 00000000..6ae5fc95 --- /dev/null +++ b/examples/herokuWebHook.js @@ -0,0 +1,36 @@ +/** + * This example demonstrates setting up webhook + * on the Heroku platform. + */ + + +const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN'; +const TelegramBot = require('..'); + +const options = { + webHook: { + //Port which you should bind to is assigned to $PORT variable + //See: https://devcenter.heroku.com/articles/dynos#local-environment-variables + port: process.env.PORT + // you do NOT need to set up certificates since Heroku provides + // the SSL certs already (https://.herokuapp.com) + // Also no need to pass IP because on Heroku you need to bind to 0.0.0.0 + } +}; +// Heroku routes from port :443 to $PORT +// Add URL of your app to env variable or enable Dyno Metadata +// to get this automatically +// See: https://devcenter.heroku.com/articles/dyno-metadata +const url = process.env.APP_URL || 'https://.herokuapp.com:443'; +const bot = new TelegramBot(TOKEN, options); + + +// This informs the Telegram servers of the new webhook. +// Note: we do not need to pass in the cert, as it already provided +bot.setWebHook(`${url}/bot${TOKEN}`); + + +// Just to ping! +bot.on('message', function onMessage(msg) { + bot.sendMessage(msg.chat.id, 'Running on Heroku!'); +});