From 0174b875ff69f6750fc80a049315c9a0d7a5e471 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Sat, 7 Jan 2017 18:30:07 +0300 Subject: [PATCH] [examples] Update examples Feature: * Add example demonstrating use of inline keyboards and callback queries (see PR #150) * Use token from environment variable, if available References: * PR #150 - Provided example with inline keyboard and callbacks: https://github.com/yagop/node-telegram-bot-api/pull/150 --- examples/httpsWebHook.js | 2 +- examples/openShiftWebHook.js | 2 +- examples/polling.js | 40 +++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/examples/httpsWebHook.js b/examples/httpsWebHook.js index f03b861..ad0f40a 100644 --- a/examples/httpsWebHook.js +++ b/examples/httpsWebHook.js @@ -4,7 +4,7 @@ */ -const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'; +const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN'; const TelegramBot = require('..'); const options = { webHook: { diff --git a/examples/openShiftWebHook.js b/examples/openShiftWebHook.js index 7227b9f..90b15fa 100644 --- a/examples/openShiftWebHook.js +++ b/examples/openShiftWebHook.js @@ -4,7 +4,7 @@ */ -const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'; +const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN'; const TelegramBot = require('..'); // See https://developers.openshift.com/en/node-js-environment-variables.html const options = { diff --git a/examples/polling.js b/examples/polling.js index b4e5a9d..a086f7e 100644 --- a/examples/polling.js +++ b/examples/polling.js @@ -4,7 +4,7 @@ */ -const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'; +const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN'; const TelegramBot = require('..'); const request = require('request'); const options = { @@ -52,3 +52,41 @@ bot.onText(/\/echo (.+)/, function onEchoText(msg, match) { const resp = match[1]; bot.sendMessage(msg.chat.id, resp); }); + + +// Matches /editable +bot.onText(/\/editable/, function onEditableText(msg) { + const opts = { + reply_markup: { + inline_keyboard: [ + [ + { + text: 'Edit Text', + // we shall check for this value when we listen + // for "callback_query" + callback_data: 'edit' + } + ] + ] + } + }; + bot.sendMessage(msg.from.id, 'Original Text', opts); +}); + + +// Handle callback queries +bot.on('callback_query', function onCallbackQuery(callbackQuery) { + const action = callbackQuery.data; + const msg = callbackQuery.message; + const opts = { + chat_id: msg.chat.id, + message_id: msg.message_id, + }; + let text; + + if (action === 'edit') { + text = 'Edited Text'; + } + + bot.editMessageText(text, opts); +});