jehy / telegram-test-api

Simple implimentation of telegram API which can be used for testing telegram bots
MIT License
98 stars 24 forks source link

Fix `TypeError: Cannot read property 'id' of undefined` (AKA improve bots clients support) #36

Closed micalevisk closed 1 month ago

micalevisk commented 4 years ago

Hi! Greate lib :+1:

The snippet bellow throw the error TypeError: Cannot read property 'id' of undefined since the following one do not normalize the message object (ie., req.body) before saving into the server's storage :/ So Telegram bot clients that do not send the message with the expected shape (like messaging-api-telegram) will not work 100% with this lib :disappointed: https://github.com/jehy/telegram-test-api/blob/c56a7eb1175cbb99f2afde6dac2424ddab4a524f/telegramServer.js#L195

https://github.com/jehy/telegram-test-api/blob/3deb60ccc352c49500b42e7c1eafad4fd6f5afa0/telegramServer.js#L59-L74

I manage to "fix" this changing the L65 above to:

message: {
   ...message, // keeping the original `req.body` here could break other tests
   chat: {
      id: ''+message.chat_id
   }
}

or, while using the messaging-api-telegram client I can simply do one small change in L195:

update.message.chat_id == chatId && update.messageId === messageId
//                ^^   ^^ equality since `chatId` could be number
micalevisk commented 4 years ago

Actually I think that the message payload sent to the Telegram server (as req.body): https://github.com/jehy/telegram-test-api/blob/eef08fb7f647d35c5e84ac9a523e47bdf923e1d5/routes/bot/sendMessage.js#L8 will have the same JSON shape reagardless of the bot client. Thus, add.message.chat.id will not exists due to this code: https://github.com/jehy/telegram-test-api/blob/eef08fb7f647d35c5e84ac9a523e47bdf923e1d5/telegramServer.js#L59-L70

So I don't understand why do you use update.message.chat.id instead of update.message.chat_id in this line: https://github.com/jehy/telegram-test-api/blob/eef08fb7f647d35c5e84ac9a523e47bdf923e1d5/telegramServer.js#L212-L215 https://github.com/jehy/telegram-test-api/blob/eef08fb7f647d35c5e84ac9a523e47bdf923e1d5/telegramServer.js#L223

Am I missing something? @quasiyoke

jehy commented 4 years ago

Sorry for the late answer. This library is only tested with telegraf and node-telegram-bot-api and support for some features like different data structures is not available. But PR with support for messaging-api-telegram and other libraries would be welcome.