From a8c4c0e33f368c5394315961c934e74c1f1a2f83 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Mon, 29 Jun 2015 04:19:09 +0430 Subject: [PATCH] Fix bot not working properly in groups Change example a little bit --- README.md | 58 +++++++++++++++++------------------------ build/index.js | 5 ++-- build/types/Message.js | 2 ++ build/types/Question.js | 3 +-- demo.js | 54 ++++++++++++++++---------------------- lib/index.js | 5 ++-- lib/types/Message.js | 2 ++ lib/types/Question.js | 3 +-- 8 files changed, 59 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index f28a7bf..ae00426 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,6 @@ npm install telegram-api The code is well documented. I'm trying to integrate JSDoc / ESDoc into our repository for an easy to access documentation. # Example -Take a look at [demo.js](https://github.com/mdibaiee/node-telegram-api/blob/master/demo.js). - [@JavaScriptBot](https://telegram.me/JavaScriptBot) runs on `demo.js`, you can test it. ```javascript @@ -22,53 +20,46 @@ var Bot = require('telegram-api'); var Message = require('telegram-api/types/Message'); var Question = require('telegram-api/types/Question'); -var smartBot = new Bot({ +var bot = new Bot({ token: 'YOUR_KEY' }); -// getMe is called before polling starts, setting info property of bot -smartBot.start().then(() => { - console.log(smartBot.info); +bot.start().then(() => { + console.log(bot.info); }); -// Create a new question -// answers is a keyboard layout as defined in Telegram API -// we're going to reuse this by modifying it's target -const question = new Question() - .text('How should I greet you?') - .answers([['Hey'], ['Hello, Sir'], ['Yo bro']]); +// polling +bot.on('update', update => { + console.log('Polled\n', update); +}); -// Called when a message starting with Hi is received -// You can use Regular Expressions, too -// update is an Update object as defined in Telegram API -smartBot.get('Hi', update => { - const message = update.message; +const question = new Question({ + text: 'How should I greet you?', + answers: [['Hey'], ['Hello, Sir'], ['Yo bro']] +}); - question.to(message.chat.id).reply(message.message_id); +bot.get(/Hi|Hey|Yo/, message => { + const id = message.chat.id; - // Send the question, returns a promise, resolves on valid answer, - // rejects in case of an invalid answer - smartBot.send(question).then(answer => { + question.to(id).reply(message.message_id); + + bot.send(question).then(answer => { const msg = new Message().to(id).text('Your answer: ' + answer); - smartBot.send(msg); + bot.send(msg); }, () => { const msg = new Message().to(id).text('Invalid answer'); - smartBot.send(msg); + bot.send(msg); }); }); -// Commands are in the format `/command` or `/command@botusername` in groups -const test = new Message().text('Test Command'); -smartBot.command('test', update => { - const message = update.message; - const id = message.chat.id; - - smartBot.send(test.to(id)); +const hello = new Message().text('Hello'); +bot.command('start', message => { + bot.send(hello.to(message.chat.id)); }); -const hello = new Message().text('Hello'); -smartBot.command('start', update => { - smartBot.send(hello.to(update.message.chat.id)); +const test = new Message().text('Test Command'); +bot.command('test', message => { + bot.send(test.to(message.chat.id)); }); ``` @@ -76,7 +67,6 @@ This will result in: ![@JavaScriptBot](https://github.com/mdibaiee/node-telegram-api/raw/master/demo.gif) - # Bots using this module [@JavaScriptBot](https://telegram.me/JavaScriptBot) diff --git a/build/index.js b/build/index.js index 1676429..aa4b9bc 100644 --- a/build/index.js +++ b/build/index.js @@ -99,7 +99,6 @@ var Bot = (function (_EventEmitter) { _this.update.offset += 1; } - _this.emit('update', response.result); result.forEach(function (res) { var text = res.message.text; if (text.startsWith('/')) { @@ -112,8 +111,10 @@ var Bot = (function (_EventEmitter) { var pattern = _ref.pattern; return pattern.test(text); }); - ev.listener(res); + ev.listener(res.message); }); + + _this.emit('update', result); }); }).bind(this); diff --git a/build/types/Message.js b/build/types/Message.js index 8979a3e..0bd6b9a 100644 --- a/build/types/Message.js +++ b/build/types/Message.js @@ -111,6 +111,8 @@ var Message = (function (_Base) { value: function send(bot) { var _this = this; + console.log('sending message'); + var messageId = undefined; var reply_markup = JSON.stringify(this._keyboard.getProperties()); this.properties.reply_markup = reply_markup; diff --git a/build/types/Question.js b/build/types/Question.js index e02d762..9553f71 100644 --- a/build/types/Question.js +++ b/build/types/Question.js @@ -80,8 +80,7 @@ var Question = (function (_Message) { var answers = this.answers; return new Promise(function (resolve, reject) { - _get(Object.getPrototypeOf(Question.prototype), 'send', _this).call(_this, bot).then(function (update) { - var message = update.message; + _get(Object.getPrototypeOf(Question.prototype), 'send', _this).call(_this, bot).then(function (message) { var answer = undefined; answers.forEach(function find(a) { diff --git a/demo.js b/demo.js index ae78d3e..35e15d3 100644 --- a/demo.js +++ b/demo.js @@ -4,52 +4,44 @@ var Bot = require('telegram-api'); var Message = require('telegram-api/types/Message'); var Question = require('telegram-api/types/Question'); -var smartBot = new Bot({ +var bot = new Bot({ token: 'YOUR_KEY' }); -// getMe is called before polling starts, setting info property of bot -smartBot.start().then(() => { - console.log(smartBot.info); +bot.start().then(() => { + console.log(bot.info); }); -// Create a new question -// answers is a keyboard layout as defined in Telegram API -// we're going to reuse this by modifying it's target -const question = new Question() - .text('How should I greet you?') - .answers([['Hey'], ['Hello, Sir'], ['Yo bro']]); +// polling +bot.on('update', update => { + console.log('Polled\n', update); +}); -// Called when a message starting with Hi is received -// You can use Regular Expressions, too -// update is an Update object as defined in Telegram API -smartBot.get('Hi', update => { - const message = update.message; +const question = new Question({ + text: 'How should I greet you?', + answers: [['Hey'], ['Hello, Sir'], ['Yo bro']] +}); + +bot.get(/Hi|Hey|Yo/, message => { const id = message.chat.id; question.to(id).reply(message.message_id); - // Send the question, returns a promise, resolves on valid answer, - // rejects in case of an invalid answer - smartBot.send(question).then(answer => { + bot.send(question).then(answer => { const msg = new Message().to(id).text('Your answer: ' + answer); - smartBot.send(msg); + bot.send(msg); }, () => { const msg = new Message().to(id).text('Invalid answer'); - smartBot.send(msg); + bot.send(msg); }); }); -// Commands are in the format `/command` or `/command@botusername` in groups -const test = new Message().text('Test Command'); -smartBot.command('test', update => { - const message = update.message; - const id = message.chat.id; - - smartBot.send(test.to(id)); -}); - const hello = new Message().text('Hello'); -smartBot.command('start', update => { - smartBot.send(hello.to(update.message.chat.id)); +bot.command('start', message => { + bot.send(hello.to(message.chat.id)); +}); + +const test = new Message().text('Test Command'); +bot.command('test', message => { + bot.send(test.to(message.chat.id)); }); diff --git a/lib/index.js b/lib/index.js index 036982f..d563cef 100644 --- a/lib/index.js +++ b/lib/index.js @@ -64,7 +64,6 @@ export default class Bot extends EventEmitter { this.update.offset += 1; } - this.emit('update', response.result); result.forEach(res => { let text = res.message.text; if (text.startsWith('/')) { @@ -74,8 +73,10 @@ export default class Bot extends EventEmitter { } let ev = this._userEvents.find(({pattern}) => pattern.test(text)); - ev.listener(res); + ev.listener(res.message); }); + + this.emit('update', result); }); }.bind(this); diff --git a/lib/types/Message.js b/lib/types/Message.js index 1e1a455..f94b230 100644 --- a/lib/types/Message.js +++ b/lib/types/Message.js @@ -72,6 +72,8 @@ export default class Message extends Base { * @return {promise} returns a promise, resolved with message:answer */ send(bot) { + console.log('sending message'); + let messageId; const reply_markup = JSON.stringify(this._keyboard.getProperties()); this.properties.reply_markup = reply_markup; diff --git a/lib/types/Question.js b/lib/types/Question.js index 965c30f..3a8068f 100644 --- a/lib/types/Question.js +++ b/lib/types/Question.js @@ -45,8 +45,7 @@ export default class Question extends Message { const answers = this.answers; return new Promise((resolve, reject) => { - super.send(bot).then(update => { - const message = update.message; + super.send(bot).then(message => { let answer; answers.forEach(function find(a) {