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.
|
||||
|
||||
# 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)
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
54
demo.js
54
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));
|
||||
});
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user