Fix long-polling

This commit is contained in:
Mahdi Dibaiee 2015-07-05 15:38:07 +04:30
parent ac625708f9
commit e036c11152
4 changed files with 8 additions and 25 deletions

View File

@ -188,13 +188,13 @@ var Bot = (function (_EventEmitter) {
update.forEach(function (res) { update.forEach(function (res) {
var text = res.message.text; var text = res.message.text;
if (text.startsWith('/')) { var selfUsername = '@' + _this2.info.username;
if (text.startsWith('/') && text.indexOf(selfUsername) > -1) {
// Commands are sent in /command@thisusername format in groups // Commands are sent in /command@thisusername format in groups
var regex = new RegExp('(/.*)@' + _this2.info.username); var regex = new RegExp('(/.*)@' + _this2.info.username);
text = text.replace(regex, '$1'); text = text.replace(regex, '$1');
res.message.text = text; res.message.text = text;
console.log(res.message.text);
} }
var ev = _this2._userEvents.find(function (_ref) { var ev = _this2._userEvents.find(function (_ref) {

View File

@ -7,21 +7,13 @@ exports['default'] = poll;
function poll(bot) { function poll(bot) {
return bot.api.getUpdates(bot.update).then(function (response) { return bot.api.getUpdates(bot.update).then(function (response) {
var again = wait(bot.update.timeout * 1000, bot).then(poll);
if (!response.result.length) { if (!response.result.length) {
return again; return poll(bot);
} }
bot.emit('update', response.result); bot.emit('update', response.result);
return again; return poll(bot);
}); });
} }
var wait = function wait(miliseconds, value) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(value);
}, miliseconds);
});
};
module.exports = exports['default']; module.exports = exports['default'];

View File

@ -1,19 +1,10 @@
export default function poll(bot) { export default function poll(bot) {
return bot.api.getUpdates(bot.update).then(response => { return bot.api.getUpdates(bot.update).then(response => {
const again = wait(bot.update.timeout * 1000, bot).then(poll);
if (!response.result.length) { if (!response.result.length) {
return again; return poll(bot);
} }
bot.emit('update', response.result); bot.emit('update', response.result);
return again; return poll(bot);
}); });
} }
const wait = (miliseconds, value) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(value);
}, miliseconds);
});
};

View File

@ -1,6 +1,6 @@
{ {
"name": "telegram-api", "name": "telegram-api",
"version": "0.4.62", "version": "0.4.63",
"description": "Control Telegram bots easily using the new Telegram API", "description": "Control Telegram bots easily using the new Telegram API",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {