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

Failed to get updates from Telegram servers #24

Closed riseremi closed 8 years ago

riseremi commented 8 years ago

Hi! I tried this lib back in 2015 and everything was fine. Today I'm getting this error while trying to send a message:

[TelegramBot]: Failed to get updates from Telegram servers

I have created this repo to help you figure out what's wrong: https://github.com/riseremi/telegram-bot-api-issue

mast commented 8 years ago

@riseremi

The problem is in your code. You bound method catch to wrong object.

It's what you have:

api.sendMessage({
                chat_id: message.chat.id,
                text: message.text ? message.text : 'This message doesn\'t contain text :('
        })
        .then(function(message) {
                console.log(message);
        }
        .catch(function(err) {
                console.log(err);
        }));

Need to be changed to:

api.sendMessage({
                chat_id: message.chat.id,
                text: message.text ? message.text : 'This message doesn\'t contain text :('
        })
        .then(function(message) {
                console.log(message);
        })  // This is a change
        .catch(function(err) {
                console.log(err);
        }); // This is a change
riseremi commented 8 years ago

My bad. Thank you! Could you please check one of your examples? I just copied that code. What's the difference? https://github.com/mast/telegram-bot-api/blob/master/examples/echo-bot.js

mast commented 8 years ago

@riseremi Thank you, my bad as well. :) I corrected example. Closing the ticket now.

realestninja commented 8 years ago

hey, i'm getting the same error.

var telegram = require('telegram-bot-api');
var api = new telegram({
  token: ':)',
  updates: {
    enabled: true,
    get_interval: 1000
  }
});

api.on('message', function(message)
{
  var chat_id = message.chat.id;

  if (message.photo || message.document.mime_type == "image\/jpeg") {
    console.log('received a photo');
  } else {
    console.log('received a message');
  }
});

i don't exactly understand how to attach the catch part to my code... i only get the error message if i send text messages to my bot, no error when i send pictures.

jkantr commented 8 years ago

In your example you are using an event listener.. so there's nothing to try/catch or no promise to be returned to bubble up from.

The real problem is that line 161 of the lib is just console dumping a vague message instead of the actual error. My suggestion is that either we dump the real error to the console (as ugly as it is) or we throw the actual error, expecting the bot client to handle it.

In your case, realestninja, you could temporarily change line 161 to:

console.dir('Failed to get updates: '+err);

and this would print the real error to your console instead of just the vague one. Likely your issue is something like bad encoding, if i had to guess. I think TG accepts utf8 only.

realestninja commented 8 years ago

Thanks for the fast response ! Replacing that line 161 really helped me find my own stupid mistake !