From 59d47f1fba0434427705bde74c20bef0cc51ad4b Mon Sep 17 00:00:00 2001 From: Rey Date: Tue, 20 Oct 2015 00:01:02 +0800 Subject: [PATCH 1/2] Updated Example to use onText --- examples/polling.js | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/examples/polling.js b/examples/polling.js index 22f54e8..2dc98c4 100644 --- a/examples/polling.js +++ b/examples/polling.js @@ -11,26 +11,30 @@ var bot = new TelegramBot(token, options); bot.getMe().then(function (me) { console.log('Hi my name is %s!', me.username); }); -bot.on('text', function (msg) { - var chatId = msg.chat.id; - if (msg.text == '/photo') { - // From file - var photo = __dirname+'/../test/bot.gif'; - bot.sendPhoto(chatId, photo, {caption: "I'm a bot!"}); - } - if (msg.text == '/audio') { - var url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'; + +bot.onText(/\/photo.*/, function (msg) { + var fromId = msg.from.id; + // From file + var photo = __dirname+'/../test/bot.gif'; + bot.sendPhoto(fromId, photo, {caption: "I'm a bot!"}); +}); + +bot.onText(/\/audio.*/, function (msg) { + var fromId = msg.from.id; + var url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'; // From HTTP request! var audio = request(url); - bot.sendAudio(chatId, audio) + bot.sendAudio(fromId, audio) .then(function (resp) { // Forward the msg var messageId = resp.message_id; - bot.forwardMessage(chatId, chatId, messageId); + bot.forwardMessage(fromId, fromId, messageId); }); - } - if (msg.text == '/love') { - var opts = { +}); + +bot.onText(/\/love.*/, function (msg) { + var fromId = msg.from.id; + var opts = { reply_to_message_id: msg.message_id, reply_markup: JSON.stringify({ keyboard: [ @@ -38,6 +42,11 @@ bot.on('text', function (msg) { ['No, sorry there is another one...']] }) }; - bot.sendMessage(chatId, 'Do you love me?', opts); - } + bot.sendMessage(fromId, 'Do you love me?', opts); +}); + +bot.onText(/\/echo (.+)/, function (msg, match) { + var fromId = msg.from.id; + var resp = match[1]; + bot.sendMessage(fromId, resp); }); From d03134e66752760c549230320cce82f79cc8267d Mon Sep 17 00:00:00 2001 From: Rey Date: Tue, 20 Oct 2015 01:01:16 +0800 Subject: [PATCH 2/2] Replace msg.from.id with msg.chat.id --- examples/polling.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/polling.js b/examples/polling.js index 2dc98c4..081ca45 100644 --- a/examples/polling.js +++ b/examples/polling.js @@ -13,27 +13,27 @@ bot.getMe().then(function (me) { }); bot.onText(/\/photo.*/, function (msg) { - var fromId = msg.from.id; + var chatId = msg.chat.id; // From file var photo = __dirname+'/../test/bot.gif'; - bot.sendPhoto(fromId, photo, {caption: "I'm a bot!"}); + bot.sendPhoto(chatId, photo, {caption: "I'm a bot!"}); }); bot.onText(/\/audio.*/, function (msg) { - var fromId = msg.from.id; + 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(fromId, audio) + bot.sendAudio(chatId, audio) .then(function (resp) { // Forward the msg var messageId = resp.message_id; - bot.forwardMessage(fromId, fromId, messageId); + bot.forwardMessage(chatId, chatId, messageId); }); }); bot.onText(/\/love.*/, function (msg) { - var fromId = msg.from.id; + var chatId = msg.chat.id; var opts = { reply_to_message_id: msg.message_id, reply_markup: JSON.stringify({ @@ -42,11 +42,11 @@ bot.onText(/\/love.*/, function (msg) { ['No, sorry there is another one...']] }) }; - bot.sendMessage(fromId, 'Do you love me?', opts); + bot.sendMessage(chatId, 'Do you love me?', opts); }); bot.onText(/\/echo (.+)/, function (msg, match) { - var fromId = msg.from.id; + var chatId = msg.chat.id; var resp = match[1]; - bot.sendMessage(fromId, resp); + bot.sendMessage(chatId, resp); });