lukefx / hubot-telegram

Hubot adapter for Telegram
MIT License
160 stars 42 forks source link

Error: Message is too long #17

Closed n0mer closed 9 years ago

n0mer commented 9 years ago

hi, got "message is too long" error

[Mon Aug 31 2015 01:01:55 GMT+0000 (UTC)] INFO Receiving message_id: 4
[Mon Aug 31 2015 01:01:55 GMT+0000 (UTC)] ERROR Error: Error: Message is too long
  at Function.TelegramBot.error (/hubot/node_modules/hubot-telegram/node_modules/telegrambot/lib/telegrambot.js:26:15)
  at Request._callback (/hubot/node_modules/hubot-telegram/node_modules/telegrambot/lib/telegrambot.js:47:66)
  at Request.self.callback (/hubot/node_modules/hubot-telegram/node_modules/telegrambot/node_modules/request/request.js:198:22)
  at Request.emit (events.js:110:17)
  at Request.<anonymous> (/hubot/node_modules/hubot-telegram/node_modules/telegrambot/node_modules/request/request.js:1073:14)
  at Request.emit (events.js:129:20)
  at IncomingMessage.<anonymous> (/hubot/node_modules/hubot-telegram/node_modules/telegrambot/node_modules/request/request.js:1019:12)
  at IncomingMessage.emit (events.js:129:20)
  at _stream_readable.js:908:16
  at process._tickCallback (node.js:355:11)

Same bot works well with Slack back-end.

$ wc ~/temp/hubot-help.txt 72 915 5565 /Users/n0mer/temp/hubot-help.txt

So my /help mesage contains 72 lines, 915 words and 5565 chars

arcturial commented 9 years ago

This is a limitation of the Telegram API itself https://core.telegram.org/method/messages.sendMessage#return-errors

It seems their API can only support 4096 bytes per message.

@lukefx, do you think it's worth limiting messages to that size so that they send multiple messages if the byte size is more than supported by Telegram?

arcturial commented 9 years ago

Hi

Please try out the branch https://github.com/lukefx/hubot-telegram/tree/issue-17

You can include it in your package.json file like so:

"hubot-telegram": "lukefx/hubot-telegram#issue-17"

I have changed the sending code to split the messages into parts when they are above 4096 characters. Let me know if this solves your problem.

n0mer commented 9 years ago

@arcturial okay, i'll check right now and respond how is it going :)

btw, here is the command to install that via shell

npm install --save "git://github.com/lukefx/hubot-telegram#issue-17"
n0mer commented 9 years ago

so - it works, i'm getting 2 messages.

But, for some reason, i'm getting 2nd part as 1st message and 1st part as 2nd msg.

corresponding log records

Sep  6 01:04:03 sbg docker/e54ebf98f743[15891]: [Sun Sep 06 2015 01:04:03 GMT+0200 (CEST)] INFO Sending message to room: 815356
Sep  6 01:04:03 sbg docker/e54ebf98f743[15891]: [Sun Sep 06 2015 01:04:03 GMT+0200 (CEST)] INFO Sending message to room: 815356
arcturial commented 9 years ago

Please try the latest commit on https://github.com/lukefx/hubot-telegram/tree/issue-17

I had to make the requests sequential since it seems that the message with more bytes gets delivered last possibly due to network latency (ie. since the smaller message gets sent to Telegram faster it is delivered first). This is unfortunately still a problem if you do two send calls one after the other in your script. The one with the smallest packet will most likely be delivered first. However, for the scenario you described (where your bot sends a single request)...the messages should now deliver in the intended order even though it will be slightly slower than previously.

n0mer commented 9 years ago

I tried, now order is fine, thanks.

One thing - response is delivered as "one message per one line of hubot response". Is it intended behaviour?

And, how will it work for single-line string longer than 4096 ?

arcturial commented 9 years ago

Not sure I understand, the code splits the message using regex.

/.{1,4096}/g

Meaning that it should split the string based on any character for 4096 characters regardless of new lines etc. It should only send multiple messages if the character count of a string is more than 4096, otherwise it should work like it normally does. Does that answer you question?

n0mer commented 9 years ago

this is how output looks like now

image

yesterday it looked like this

image

arcturial commented 9 years ago

Ah, I understand

This might be the regex, standby while I fix.

n0mer commented 9 years ago

just curious - did you run this code by yourself :) ?

arcturial commented 9 years ago

Yeah, but I ran it with a test script that just adds a whole bunch of characters on a single line. I will create a test for multiple lines now.

n0mer commented 9 years ago

okay, glad that my use case helped to corner this issue

arcturial commented 9 years ago

I pushed another update with a test, hopefully I caught all the gremlins this time.

n0mer commented 9 years ago

wow, now it is much better - both fast and in correct order :)

also newline is handled nice

image

i am not sure how do you actually split the text

but if you do it the way words are not trimmed in the middle of a single word - that's super )

arcturial commented 9 years ago

Gosh, I will have a look at that...don't think I can use straightforward regex for it though :(

n0mer commented 9 years ago

Well, this is not necessary. Especially if it will be implemented as long "hairy" regexp :)

I'm serious - please just truncate, w/out any regexp aimed to be smarter than regexp should be.

Let's merge what you already have into master, and close this issue. Everything is already good enough and fast enough.

arcturial commented 9 years ago

Ok, I merged it to master. I will update my own Hubot to user #master for a few days to see if any issues pop up. After this period I will tag a new official release. Thanks for the help with debugging this issue.

n0mer commented 9 years ago

:+1:

lukefx commented 9 years ago

As alternative we could check the length of the result string here: https://github.com/lukefx/hubot-telegram/blob/master/src/telegram.coffee#L105

because we join every message in one big string...couldn't just split there and do multiple send?

arcturial commented 9 years ago

The strings... entries could still be more than the allowed characters, which is why I combined it and then split it into appropriate chunks. Can you maybe provide and example of what you mean?