depoio / node-telegram-bot

Client wrapper for Telegram Bot API (Under heavy development)
MIT License
136 stars 30 forks source link

Edited messages causes library to break #80

Open fcanela opened 8 years ago

fcanela commented 8 years ago

When the server sends a message like (personal information redacted and user id modified):

{ update_id: 404781694,
  edited_message: 
   { message_id: 11,
     from: 
      { id: 152564221,
        first_name: 'REDACTED',
        last_name: 'Redacted',
        username: 'redacted' },
     chat: 
      { id: 152564221,
        first_name: 'Redacted',
        last_name: 'Redacted',
        username: 'redacted',
        type: 'private' },
     date: 1471044047,
     edit_date: 1471044102,
     text: '/asignatura ean',
     entities: [ [Object] ] } }

The following error is raised:

/home/fcanela/workspace/lab/silly_motivational_bot/node_modules/node-telegram-bot/lib/Bot.js:221
                if (msg.message.text && msg.message.text.charAt(0) === '/') {
                               ^

TypeError: Cannot read property 'text' of undefined
    at /home/fcanela/workspace/lab/silly_motivational_bot/node_modules/node-telegram-bot/lib/Bot.js:221:32
    at Array.forEach (native)
    at Request._callback (/home/fcanela/workspace/lab/silly_motivational_bot/node_modules/node-telegram-bot/lib/Bot.js:216:23)
    at Request.self.callback (/home/fcanela/workspace/lab/silly_motivational_bot/node_modules/request/request.js:187:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/home/fcanela/workspace/lab/silly_motivational_bot/node_modules/request/request.js:1044:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (/home/fcanela/workspace/lab/silly_motivational_bot/node_modules/request/request.js:965:12)

This behaviour can be reproduced this way:

  1. Sending a command using Linux client
  2. Pressing up key in the text area, an "Edit message" label will appear
  3. Resending the command or writing a new one

A dirty and quick fix that solved my issue was referencing msg.message to msg.edited_message. Starting from line 216:

          body.result.forEach(function (msg) {
                        if (msg.edited_message) msg.message = msg.edited_message;

There should be probably a better option, but I have no time to validate it right now. I hope this help.

Thanks for the library.

vbauer commented 8 years ago

I have the same problem: https://github.com/depoio/node-telegram-bot/issues/78

fcanela commented 8 years ago

Sorry, @vbauer, didn't saw your issue.

I hope the fix included in my original post help you with your issue.

Alavi1412 commented 8 years ago

I have added :

Bot.prototype.editMessageText = function (options, callback) { var self = this , deferred = Q.defer();

this._get({ method: 'editMessageText', params: { chat_id: options.chat_id, text: options.text, message_id: options.message_id, parse_mode: options.parse_mode, disable_web_page_preview: options.disable_web_page_preview, reply_to_message_id: options.reply_to_message_id, reply_markup: JSON.stringify(options.reply_markup) } }, function (err, res) { if (err) { return deferred.reject(err); }

if (res.ok) {
  deferred.resolve(res.result);
} else {
  deferred.reject(res);
}

});

return deferred.promise.nodeify(callback); };

and edit message works very well

Alavi1412 commented 8 years ago

add : if(!msg.callback_query)

before : if (msg.message.text && msg.message.text.charAt(0) === '/') {

and your error will be fixed

vbauer commented 8 years ago

Any news about this problem? Is it possible to fix this issue in library in the nearest future?

shernshiou commented 8 years ago

@vbauer I am sorry. Currently I am very busy. I would accept any pull request to fix this issue.

theshaun commented 8 years ago

Hi guys,

Are you testing based on NPM or from git master?

There was a fix pushed #73 that split out and gracefully handles normal and edited messages.

Based on the code examples I am seeing, they don't seem to match master

Alavi1412 commented 8 years ago

Bot.zip

hi,I have edited something ,and added new futures, and it supports inline_keyborads and answerCallbackQuery method and editMessageText and ... I wish it could help you.

vbauer commented 8 years ago

@theshaun I've been testing library with #73 during last week and everything looks fine, except a new one problem that I've caught few minutes ago: #81