mast / telegram-bot-api

First Telegram Bot API node.js library
http://mast.github.io/telegram-bot-api/
MIT License
246 stars 64 forks source link

Promisify #15

Closed rifler closed 8 years ago

rifler commented 8 years ago

What about promises? For example - https://github.com/dfilatov/vow

Can I send pull request ?

mast commented 8 years ago

Can you give short example where you want to see promises? In the API?

Jokero commented 8 years ago

In all methods. Example:

api.getMe()
  .then(function(data) {})
  .catch(function(err) {});

There are native promises in nodejs

gumeniukcom commented 8 years ago

@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' }
Jokero commented 8 years ago

@ViGo5190 It should be out the box feature. q.npost is like hack

gumeniukcom commented 8 years ago

@Jokero @rifler @mast Promise realesed in PR https://github.com/mast/telegram-bot-api/pull/18

mast commented 8 years ago

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.