Fix bot not working properly in groups
Change example a little bit
This commit is contained in:
parent
7174ad30b1
commit
a8c4c0e33f
58
README.md
58
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.
|
The code is well documented. I'm trying to integrate JSDoc / ESDoc into our repository for an easy to access documentation.
|
||||||
|
|
||||||
# Example
|
# 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.
|
[@JavaScriptBot](https://telegram.me/JavaScriptBot) runs on `demo.js`, you can test it.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
@ -22,53 +20,46 @@ var Bot = require('telegram-api');
|
|||||||
var Message = require('telegram-api/types/Message');
|
var Message = require('telegram-api/types/Message');
|
||||||
var Question = require('telegram-api/types/Question');
|
var Question = require('telegram-api/types/Question');
|
||||||
|
|
||||||
var smartBot = new Bot({
|
var bot = new Bot({
|
||||||
token: 'YOUR_KEY'
|
token: 'YOUR_KEY'
|
||||||
});
|
});
|
||||||
|
|
||||||
// getMe is called before polling starts, setting info property of bot
|
bot.start().then(() => {
|
||||||
smartBot.start().then(() => {
|
console.log(bot.info);
|
||||||
console.log(smartBot.info);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create a new question
|
// polling
|
||||||
// answers is a keyboard layout as defined in Telegram API
|
bot.on('update', update => {
|
||||||
// we're going to reuse this by modifying it's target
|
console.log('Polled\n', update);
|
||||||
const question = new Question()
|
});
|
||||||
.text('How should I greet you?')
|
|
||||||
.answers([['Hey'], ['Hello, Sir'], ['Yo bro']]);
|
|
||||||
|
|
||||||
// Called when a message starting with Hi is received
|
const question = new Question({
|
||||||
// You can use Regular Expressions, too
|
text: 'How should I greet you?',
|
||||||
// update is an Update object as defined in Telegram API
|
answers: [['Hey'], ['Hello, Sir'], ['Yo bro']]
|
||||||
smartBot.get('Hi', update => {
|
});
|
||||||
const message = update.message;
|
|
||||||
|
|
||||||
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,
|
question.to(id).reply(message.message_id);
|
||||||
// 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);
|
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');
|
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 hello = new Message().text('Hello');
|
||||||
const test = new Message().text('Test Command');
|
bot.command('start', message => {
|
||||||
smartBot.command('test', update => {
|
bot.send(hello.to(message.chat.id));
|
||||||
const message = update.message;
|
|
||||||
const id = message.chat.id;
|
|
||||||
|
|
||||||
smartBot.send(test.to(id));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const hello = new Message().text('Hello');
|
const test = new Message().text('Test Command');
|
||||||
smartBot.command('start', update => {
|
bot.command('test', message => {
|
||||||
smartBot.send(hello.to(update.message.chat.id));
|
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)
|
![@JavaScriptBot](https://github.com/mdibaiee/node-telegram-api/raw/master/demo.gif)
|
||||||
|
|
||||||
|
|
||||||
# Bots using this module
|
# Bots using this module
|
||||||
|
|
||||||
[@JavaScriptBot](https://telegram.me/JavaScriptBot)
|
[@JavaScriptBot](https://telegram.me/JavaScriptBot)
|
||||||
|
@ -99,7 +99,6 @@ var Bot = (function (_EventEmitter) {
|
|||||||
_this.update.offset += 1;
|
_this.update.offset += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
_this.emit('update', response.result);
|
|
||||||
result.forEach(function (res) {
|
result.forEach(function (res) {
|
||||||
var text = res.message.text;
|
var text = res.message.text;
|
||||||
if (text.startsWith('/')) {
|
if (text.startsWith('/')) {
|
||||||
@ -112,8 +111,10 @@ var Bot = (function (_EventEmitter) {
|
|||||||
var pattern = _ref.pattern;
|
var pattern = _ref.pattern;
|
||||||
return pattern.test(text);
|
return pattern.test(text);
|
||||||
});
|
});
|
||||||
ev.listener(res);
|
ev.listener(res.message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_this.emit('update', result);
|
||||||
});
|
});
|
||||||
}).bind(this);
|
}).bind(this);
|
||||||
|
|
||||||
|
@ -111,6 +111,8 @@ var Message = (function (_Base) {
|
|||||||
value: function send(bot) {
|
value: function send(bot) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
|
||||||
|
console.log('sending message');
|
||||||
|
|
||||||
var messageId = undefined;
|
var messageId = undefined;
|
||||||
var reply_markup = JSON.stringify(this._keyboard.getProperties());
|
var reply_markup = JSON.stringify(this._keyboard.getProperties());
|
||||||
this.properties.reply_markup = reply_markup;
|
this.properties.reply_markup = reply_markup;
|
||||||
|
@ -80,8 +80,7 @@ var Question = (function (_Message) {
|
|||||||
var answers = this.answers;
|
var answers = this.answers;
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
_get(Object.getPrototypeOf(Question.prototype), 'send', _this).call(_this, bot).then(function (update) {
|
_get(Object.getPrototypeOf(Question.prototype), 'send', _this).call(_this, bot).then(function (message) {
|
||||||
var message = update.message;
|
|
||||||
var answer = undefined;
|
var answer = undefined;
|
||||||
|
|
||||||
answers.forEach(function find(a) {
|
answers.forEach(function find(a) {
|
||||||
|
54
demo.js
54
demo.js
@ -4,52 +4,44 @@ var Bot = require('telegram-api');
|
|||||||
var Message = require('telegram-api/types/Message');
|
var Message = require('telegram-api/types/Message');
|
||||||
var Question = require('telegram-api/types/Question');
|
var Question = require('telegram-api/types/Question');
|
||||||
|
|
||||||
var smartBot = new Bot({
|
var bot = new Bot({
|
||||||
token: 'YOUR_KEY'
|
token: 'YOUR_KEY'
|
||||||
});
|
});
|
||||||
|
|
||||||
// getMe is called before polling starts, setting info property of bot
|
bot.start().then(() => {
|
||||||
smartBot.start().then(() => {
|
console.log(bot.info);
|
||||||
console.log(smartBot.info);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create a new question
|
// polling
|
||||||
// answers is a keyboard layout as defined in Telegram API
|
bot.on('update', update => {
|
||||||
// we're going to reuse this by modifying it's target
|
console.log('Polled\n', update);
|
||||||
const question = new Question()
|
});
|
||||||
.text('How should I greet you?')
|
|
||||||
.answers([['Hey'], ['Hello, Sir'], ['Yo bro']]);
|
|
||||||
|
|
||||||
// Called when a message starting with Hi is received
|
const question = new Question({
|
||||||
// You can use Regular Expressions, too
|
text: 'How should I greet you?',
|
||||||
// update is an Update object as defined in Telegram API
|
answers: [['Hey'], ['Hello, Sir'], ['Yo bro']]
|
||||||
smartBot.get('Hi', update => {
|
});
|
||||||
const message = update.message;
|
|
||||||
|
bot.get(/Hi|Hey|Yo/, message => {
|
||||||
const id = message.chat.id;
|
const id = message.chat.id;
|
||||||
|
|
||||||
question.to(id).reply(message.message_id);
|
question.to(id).reply(message.message_id);
|
||||||
|
|
||||||
// Send the question, returns a promise, resolves on valid answer,
|
bot.send(question).then(answer => {
|
||||||
// rejects in case of an invalid answer
|
|
||||||
smartBot.send(question).then(answer => {
|
|
||||||
const msg = new Message().to(id).text('Your answer: ' + 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');
|
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');
|
const hello = new Message().text('Hello');
|
||||||
smartBot.command('start', update => {
|
bot.command('start', message => {
|
||||||
smartBot.send(hello.to(update.message.chat.id));
|
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));
|
||||||
});
|
});
|
||||||
|
@ -64,7 +64,6 @@ export default class Bot extends EventEmitter {
|
|||||||
this.update.offset += 1;
|
this.update.offset += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('update', response.result);
|
|
||||||
result.forEach(res => {
|
result.forEach(res => {
|
||||||
let text = res.message.text;
|
let text = res.message.text;
|
||||||
if (text.startsWith('/')) {
|
if (text.startsWith('/')) {
|
||||||
@ -74,8 +73,10 @@ export default class Bot extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let ev = this._userEvents.find(({pattern}) => pattern.test(text));
|
let ev = this._userEvents.find(({pattern}) => pattern.test(text));
|
||||||
ev.listener(res);
|
ev.listener(res.message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.emit('update', result);
|
||||||
});
|
});
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
|
||||||
|
@ -72,6 +72,8 @@ export default class Message extends Base {
|
|||||||
* @return {promise} returns a promise, resolved with message:answer
|
* @return {promise} returns a promise, resolved with message:answer
|
||||||
*/
|
*/
|
||||||
send(bot) {
|
send(bot) {
|
||||||
|
console.log('sending message');
|
||||||
|
|
||||||
let messageId;
|
let messageId;
|
||||||
const reply_markup = JSON.stringify(this._keyboard.getProperties());
|
const reply_markup = JSON.stringify(this._keyboard.getProperties());
|
||||||
this.properties.reply_markup = reply_markup;
|
this.properties.reply_markup = reply_markup;
|
||||||
|
@ -45,8 +45,7 @@ export default class Question extends Message {
|
|||||||
const answers = this.answers;
|
const answers = this.answers;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
super.send(bot).then(update => {
|
super.send(bot).then(message => {
|
||||||
const message = update.message;
|
|
||||||
let answer;
|
let answer;
|
||||||
|
|
||||||
answers.forEach(function find(a) {
|
answers.forEach(function find(a) {
|
||||||
|
Loading…
Reference in New Issue
Block a user