From f1b8e2d4f547acd2f41fabb41c413d8c760e47e9 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Sat, 7 Jan 2017 12:44:29 +0300 Subject: [PATCH] [examples] Update examples --- examples/httpsWebHook.js | 30 +++++++++++--- examples/openShiftWebHook.js | 40 ++++++++++++------ examples/polling.js | 79 ++++++++++++++++++------------------ examples/replyToMessage.js | 24 ----------- package.json | 2 +- 5 files changed, 92 insertions(+), 83 deletions(-) delete mode 100644 examples/replyToMessage.js diff --git a/examples/httpsWebHook.js b/examples/httpsWebHook.js index ed15de4..f03b861 100644 --- a/examples/httpsWebHook.js +++ b/examples/httpsWebHook.js @@ -1,12 +1,30 @@ -var TelegramBot = require('../src/telegram'); +/** + * This example demonstrates setting up a webook, using a + * self-signed certificate. + */ -var options = { + +const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'; +const TelegramBot = require('..'); +const options = { webHook: { port: 443, - key: __dirname+'/key.pem', - cert: __dirname+'/crt.pem' + key: `${__dirname}/key.pem`, // Path to file with PEM private key + cert: `${__dirname}/crt.pem` // Path to file with PEM certificate } }; +// This URL must route to the port set above (i.e. 443) +const url = 'https://'; +const bot = new TelegramBot(TOKEN, options); -var bot = new TelegramBot('BOT_TOKEN', options); -bot.setWebHook('IP:PORT/botBOT_TOKEN', __dirname+'/crt.pem'); + +// This informs the Telegram servers of the new webhook. +bot.setWebHook(`${url}/bot${TOKEN}`, { + certificate: options.webHook.cert, +}); + + +// Just to ping! +bot.on('message', function onMessage(msg) { + bot.sendMessage(msg.chat.id, "I'm alive!"); +}); diff --git a/examples/openShiftWebHook.js b/examples/openShiftWebHook.js index 4c4634e..7227b9f 100644 --- a/examples/openShiftWebHook.js +++ b/examples/openShiftWebHook.js @@ -1,16 +1,32 @@ -// An example for OpenShift platform. -var TelegramBot = require('node-telegram-bot-api'); +/** + * This example demonstrates setting up webhook + * on the OpenShift platform. + */ -var token = 'YOUR_TELEGRAM_BOT_TOKEN'; + +const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'; +const TelegramBot = require('..'); // See https://developers.openshift.com/en/node-js-environment-variables.html -var port = process.env.OPENSHIFT_NODEJS_PORT; -var host = process.env.OPENSHIFT_NODEJS_IP; -var domain = process.env.OPENSHIFT_APP_DNS; +const options = { + webHook: { + port: process.env.OPENSHIFT_NODEJS_PORT, + host: process.env.OPENSHIFT_NODEJS_IP, + // you do NOT need to set up certificates since OpenShift provides + // the SSL certs already (https://.rhcloud.com) + }, +}; +// OpenShift routes from port :443 to OPENSHIFT_NODEJS_PORT +const domain = process.env.OPENSHIFT_APP_DNS; +const url = `${domain}:443`; +const bot = new TelegramBot(TOKEN, options); -var bot = new TelegramBot(token, {webHook: {port: port, host: host}}); -// OpenShift enroutes :443 request to OPENSHIFT_NODEJS_PORT -bot.setWebHook(domain+':443/bot'+token); -bot.on('message', function (msg) { - var chatId = msg.chat.id; - bot.sendMessage(chatId, "I'm alive!"); + +// 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, "I'm alive on OpenShift!"); }); diff --git a/examples/polling.js b/examples/polling.js index 7dccf4b..b4e5a9d 100644 --- a/examples/polling.js +++ b/examples/polling.js @@ -1,55 +1,54 @@ -var TelegramBot = require('../src/telegram'); -var request = require('request'); +/** + * This example demonstrates using polling. + * It also demonstrates how you would process and send messages. + */ -var options = { + +const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'; +const TelegramBot = require('..'); +const request = require('request'); +const options = { polling: true }; +const bot = new TelegramBot(TOKEN, options); -var token = process.env.TELEGRAM_BOT_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN'; - -var bot = new TelegramBot(token, options); -bot.getMe().then(function (me) { - console.log('Hi my name is %s!', me.username); -}); // Matches /photo -bot.onText(/\/photo/, function (msg) { - var chatId = msg.chat.id; - // From file - var photo = __dirname+'/../test/bot.gif'; - bot.sendPhoto(chatId, photo, {caption: "I'm a bot!"}); +bot.onText(/\/photo/, function onPhotoText(msg) { + // From file path + const photo = `${__dirname}/../test/data/photo.gif`; + bot.sendPhoto(msg.chat.id, photo, { + caption: "I'm a bot!" + }); }); + // Matches /audio -bot.onText(/\/audio/, function (msg) { - var chatId = msg.chat.id; - var url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'; - // From HTTP request! - var audio = request(url); - bot.sendAudio(chatId, audio) - .then(function (resp) { - // Forward the msg - var messageId = resp.message_id; - bot.forwardMessage(chatId, chatId, messageId); - }); +bot.onText(/\/audio/, function onAudioText(msg) { + // From HTTP request + const url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'; + const audio = request(url); + bot.sendAudio(msg.chat.id, audio); }); + // Matches /love -bot.onText(/\/love/, function (msg) { - var chatId = msg.chat.id; - var opts = { - reply_to_message_id: msg.message_id, - reply_markup: JSON.stringify({ - keyboard: [ - ['Yes, you are the bot of my life ❤'], - ['No, sorry there is another one...']] - }) - }; - bot.sendMessage(chatId, 'Do you love me?', opts); +bot.onText(/\/love/, function onLoveText(msg) { + const opts = { + reply_to_message_id: msg.message_id, + reply_markup: JSON.stringify({ + keyboard: [ + ['Yes, you are the bot of my life ❤'], + ['No, sorry there is another one...'] + ] + }) + }; + bot.sendMessage(msg.chat.id, 'Do you love me?', opts); }); -bot.onText(/\/echo (.+)/, function (msg, match) { - var chatId = msg.chat.id; - var resp = match[1]; - bot.sendMessage(chatId, resp); + +// Matches /echo [whatever] +bot.onText(/\/echo (.+)/, function onEchoText(msg, match) { + const resp = match[1]; + bot.sendMessage(msg.chat.id, resp); }); diff --git a/examples/replyToMessage.js b/examples/replyToMessage.js deleted file mode 100644 index fb7deda..0000000 --- a/examples/replyToMessage.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict'; - -var TelegramBot = require('node-telegram-bot-api'); - -var TOKEN = 'BOT_TOKEN'; -var USER = 'USER_ID'; - -var bot = new TelegramBot(TOKEN, {polling: {timeout: 1, interval: 100}}); - -var opts = { - reply_markup: JSON.stringify( - { - force_reply: true - } - )}; - -bot.sendMessage(USER, 'How old are you?', opts) - .then(function (sended) { - var chatId = sended.chat.id; - var messageId = sended.message_id; - bot.onReplyToMessage(chatId, messageId, function (message) { - console.log('User is %s years old', message.text); - }); - }); diff --git a/package.json b/package.json index d356673..6d1caa8 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "test": "istanbul cover ./node_modules/mocha/bin/_mocha", "prepublish:test": "npm run prepublish && npm run test", "gen-doc": "jsdoc2md --src src/telegram.js -t README.hbs > README.md", - "eslint": "eslint ./src ./test", + "eslint": "eslint ./src ./test ./examples", "pretest": "npm run eslint && npm run build" }, "author": "Yago Pérez ",