Closed rifler closed 8 years ago
Can you give short example where you want to see promises? In the API?
In all methods. Example:
api.getMe()
.then(function(data) {})
.catch(function(err) {});
There are native promises in nodejs
@Jokero you can use Q library ( https://github.com/kriskowal/q )
see my example of https://github.com/mast/telegram-bot-api/blob/master/examples/echo-bot.js:
var telegram = require('telegram-bot-api');
var Q = require('q');
var api = new telegram({
token: '<YOUR TOKEN>',
updates: {
enabled: true,
get_interval: 1000
}
});
api.on('message', function (message) {
var self = this;
console.log(message);
Q.npost(self, 'sendMessage', [{chat_id:message.chat.id,text: 'simple answer'}])
.then(function (msg) {
console.log(msg);
})
.catch(function (err) {
console.log(err);
});
});
Q makes Promises from everything.
Another question is: Why in 'on' method first param in callback is 'message'? It would be great, if you make callback like function(err, message)
. after that i can make something like this:
Q.npost(api,'on',['message'])
.then(function(msg){
console.log('msg');
console.log(msg);
})
.catch(function(err){
console.log('err');
console.log(err);
});
In current realization i get catch(!!). So i see print like:
err
{ message_id: 37,
from:
{ id: 1,
first_name: 'name',
last_name: 'lastname',
username: 'username' },
chat:
{ id: 1,
first_name: 'name',
last_name: 'lastname',
username: 'username' },
date: 1442951785,
text: 'sd' }
@ViGo5190 It should be out the box feature. q.npost
is like hack
@Jokero @rifler @mast Promise realesed in PR https://github.com/mast/telegram-bot-api/pull/18
Fixed in 1.0.0. Thanks to @ViGo5190 I updated it a bit to make it backwards compatible. So old code should also work with new "promisified" API.
What about promises? For example - https://github.com/dfilatov/vow
Can I send pull request ?