yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.4k stars 1.52k forks source link

The Offset Infinite Loop #36

Closed vhdmsm closed 6 years ago

vhdmsm commented 9 years ago

Hi My bot has been stuck in infinite loop and it's not responding anymore, what's problem? here is the stack trace:

Unhandled rejection Error: 403 {"ok":false,"error_code":403,"description":"Error: Bot was kicked from a chat"}
    at /Users/vahid/Desktop/node_modules/node-telegram-bot-api/src/telegram.js:72:15
    at tryCatcher (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:503:31)
    at Promise._settlePromiseAt (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:577:18)
    at Promise._settlePromises (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:693:14)
    at Async._drainQueue (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:123:16)
    at Async._drainQueues (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:133:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:367:17)
Unhandled rejection Error: 403 {"ok":false,"error_code":403,"description":"Error: Bot was kicked from a chat"}
    at /Users/vahid/Desktop/node_modules/node-telegram-bot-api/src/telegram.js:72:15
    at tryCatcher (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:503:31)
    at Promise._settlePromiseAt (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:577:18)
    at Promise._settlePromises (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:693:14)
    at Async._drainQueue (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:123:16)
    at Async._drainQueues (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:133:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/vahid/Desktop/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:367:17)
mvalipour commented 8 years ago

Any luck fixing this?

LostSenSS commented 8 years ago

I have the same problem

vhdmsm commented 8 years ago

Actually i've changed my method from polling to telegram webhooks, and my problems are gone now.

witnessmenow commented 8 years ago

I have the same issue as well:

Using version 0.19.0

Its just a test a bot and the person who the message only sent the message once.

My bot is constantly just getting the same message over and over

{ message_id: 506, from: { id: someId, first_name: 'name' }, chat: { id: -GroupId, title: 'Group Name', type: 'group' }, date: 1452347899, text: '/next' }

Unhandled rejection Error: 403 {"ok":false,"error_code":403,"description":"[Error]: Forbidden: bot was kicked from the group chat"} at /Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/src/telegram.js:121:15 at tryCatcher (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/util.js:26:23) at Promise._settlePromiseFromHandler (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:507:31) at Promise._settlePromiseAt (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:581:18) at Promise._settlePromises (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:697:14) at Async._drainQueue (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:123:16) at Async._drainQueues (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:133:10) at Immediate.Async.drainQueues as _onImmediate at processImmediate as _immediateCallback Unhandled rejection Error: 403 {"ok":false,"error_code":403,"description":"[Error]: Forbidden: bot was kicked from the group chat"} at /Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/src/telegram.js:121:15 at tryCatcher (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/util.js:26:23) at Promise._settlePromiseFromHandler (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:507:31) at Promise._settlePromiseAt (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:581:18) at Promise._settlePromises (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:697:14) at Async._drainQueue (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:123:16) at Async._drainQueues (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:133:10) at Immediate.Async.drainQueues as _onImmediate at processImmediate as _immediateCallback

yagop commented 8 years ago

Seems you are trying to send a message to non exists peer or haven't talk to you before El 9/1/2016 18:20, "Brian Lough" notifications@github.com escribió:

I have the same issue as well:

My bot is constantly just getting the same message back:

Unhandled rejection Error: 403 {"ok":false,"error_code":403,"description":"[Error]: Forbidden: bot was kicked from the group chat"} at /Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/src/telegram.js:121:15 at tryCatcher (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/util.js:26:23) at Promise._settlePromiseFromHandler (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:507:31) at Promise._settlePromiseAt (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:581:18) at Promise._settlePromises (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:697:14) at Async._drainQueue (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:123:16) at Async._drainQueues (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:133:10) at Immediate.Async.drainQueues as _onImmediate http:///Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:15:14 at processImmediate as _immediateCallback Unhandled rejection Error: 403 {"ok":false,"error_code":403,"description":"[Error]: Forbidden: bot was kicked from the group chat"} at /Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/src/telegram.js:121:15 at tryCatcher (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/util.js:26:23) at Promise._settlePromiseFromHandler (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:507:31) at Promise._settlePromiseAt (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:581:18) at Promise._settlePromises (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/promise.js:697:14) at Async._drainQueue (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:123:16) at Async._drainQueues (/Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:133:10) at Immediate.Async.drainQueues as _onImmediate http:///Users/brian/my-reaction-when-telegram-bot/node_modules/node-telegram-bot-api/node_modules/bluebird/js/main/async.js:15:14 at processImmediate as _immediateCallback

— Reply to this email directly or view it on GitHub https://github.com/yagop/node-telegram-bot-api/issues/36#issuecomment-170261100 .

witnessmenow commented 8 years ago

Sorry your right the stack is due to that, but the reason the issue is appearing is that the bot is constantly receiving the same message over and over again, even thought this user only sent "/next" once, when I start up my bot all I receive is that message, same message_id everytime as well

{ message_id: 506, from: { id: someId, first_name: 'name' }, chat: { id: -GroupId, title: 'Group Name', type: 'group' }, date: 1452347899, text: '/next' }

Does that make more sense?

yagop commented 8 years ago

The error causes it won't be marked as read

mvalipour commented 8 years ago

@witnessmenow IMPOV, using polling is very unreliable and not worth it. You can very easily switch to webhooks. -- see my blog post: http://mvalipour.github.io/node.js/2015/12/06/telegram-bot-webhook-existing-express

witnessmenow commented 8 years ago

@yagop Is there anyway to move past this error when it occurs?

Apologies if there is something straight forward I'm missing, I'm pretty new to node and even newer to bot development.

@mvalipour thanks for the link, looks very straight forward. I had planned on giving the bot a web interface at some stage anyways so would have needed to add an express part down the line as it was. The bot is still in development though, so it wont solve my current problem

witnessmenow commented 8 years ago

My specific issue has to be a telegram one, Even if I request a higher offset message directly to the Telegram API its not clearing the messages.

I tweeted telegram hopefully they get back to me.

https://twitter.com/witnessmenow/status/686341804799733760

idchlife commented 8 years ago

Same error guys. Bot receives same message with same message_id and does not change this.offset here

https://github.com/yagop/node-telegram-bot-api/blob/master/src/telegramPolling.js#L32

Loop is starting over and over again, I was testing it with breakpoints in WebStorm and it behaves strangely. Always index = 0 and loop restarts again.

I think we should try to find the problem and fix it guys.

@yagop in which case message wont be marked as read?

witnessmenow commented 8 years ago

I think its a telegram error, as I was even trying to clear the message using the telegram api directly and couldn't.

It will clear its self in a couple of days

idchlife commented 8 years ago

@witnessmenow I just used telegram api url, manually entered offset like this:

/getUpdates?offset=415204539

+1-ing every time I got update_id in json. So after some iterations I got rid of old messages.

Problem is not with telegram I think. Something wrong with incrementing update_id in code. If I encounter this problem again I will try to find a solution and create PR.

witnessmenow commented 8 years ago

If you look at my tweet from earlier I tried that and it didn't work. Messages for bots clear after 24 hours, did you just happen to wait that long maybe?

idchlife commented 8 years ago

@witnessmenow which update_id did you use? I got one from their json and just incremented it and entered and repeated until no old messages left. Maybe they are clearing them automatically but you can erase them manually if you have stuck messages?

Also you may be right. I have this problem since yesterday (don't really remember time). I'll try to create another one.

idchlife commented 8 years ago

Yep. I did create another 'situation'.

Recipe is: turn off bot. Send several same messages for which you created callback onText, turn bot on and you get this:

Got message. Id: 1476 and time: 1454681507065
Got message. Id: 1476 and time: 1454681507540
Got message. Id: 1476 and time: 1454681508017
Got message. Id: 1476 and time: 1454681508480
Got message. Id: 1476 and time: 1454681508938
Got message. Id: 1476 and time: 1454681509399

After this I manually entered url which is used inside this bot

https://api.telegram.org/botNUMBER:TOKEN/getUpdates?offset=415204539

(offset is kinda random)

I got update_id like this:

{"ok":true,"result":[{"update_id":415204547,

After it I incremented this update_id and sent it via offset parameter

One by one (with each increment) stacked messages started to disappear. Until I got empty array and bot resumed to work properly. Exactly what this bot is supposed to do when reading updates. This is kinda major bug I think, because you can that easily create it.

We need to go deeper and investigate the code :smiley:

0xVasconcelos commented 8 years ago

+1 Same problem here, I need flush my bot manually incrementing +1 on offset manually.

yagop commented 8 years ago

This should be fixed by commit 178f9cd9051b63377fcbb3a13f564b175b1cd3e7. The matter was it wasn't updating the offset if there was an error and can't reach the last message. Now every message processed updates the offset.

idchlife commented 8 years ago

Thank you @yagop!

0xVasconcelos commented 8 years ago

Working perfectly now! Thanks @yagop!

phillipadsmith commented 8 years ago

@yagop :100:

omnidan commented 8 years ago

@yagop can you please release a new version with this fix? I think the issue here: https://github.com/yagop/node-telegram-bot-api/issues/15#issuecomment-214911783 is related to this, but I noticed 0.20.1 doesn't have this fix yet and there hasn't been a release since then.

phillipadsmith commented 8 years ago

@yagop can you please release a new version with this fix? I think the issue here: #15 (comment) is related to this, but I noticed 0.20.1 doesn't have this fix yet and there hasn't been a release since then.

@omnidan Upgrading to 0.20.1 did fix the issue for me, I believe.

omnidan commented 8 years ago

@phillipadsmith the fix isn't part of 0.20.1 (it's from Feb 1 and the fix was made on Mar 13) - I also contacted @yagop a while ago and he told me he meant to release a new version but forgot. Still no new release now 😞

phillipadsmith commented 8 years ago

@omnidan Bah, you're totally right.

And, having managed to get into this infinite loop again, I'm wondering if @yagop's commit https://github.com/yagop/node-telegram-bot-api/commit/178f9cd9051b63377fcbb3a13f564b175b1cd3e7 actually fixed it?

I've just tried using that version of telegramPolling.js and -- if there's a failure -- the incrementing doesn't appear to happen, and the loop begins as far as I can tell.

Have others resolved this with a patch? cc @idchlife @lucaslg26

RacerBas commented 8 years ago

I'm having the same issue right now @phillipadsmith

GochoMugo commented 7 years ago

This is what I have gathered so far:


when and how does the bug occur?

The bug occurs when an error is thrown during processing of an update.

During this processing of an update (at src/telegramPolling.js#L46), an error could be thrown while:

Such an error would bubble up and be handled by the enveloping promise (see src/telegramPolling.js#L40). The promise would simply re-throw the error (at src/telegramPolling.js#L51). This results in the "Unhandled rejection Error" as seen above. So far, such unhandled errors do NOT terminate the Node.js process. The unterminated process will continue polling (see src/telegramPolling.js#L58)!

However, @yagop's fix, updates the offset before processing an update. Since an update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id, all the updates received, including the update that caused the chain of errors to occur, would be confirmed, as soon as we enter the next poll interval (see src/telegramPolling.js#L94). Thus, the fix (kinda) works!

Before the fix, the offset would not have been updated (see old code at src/telegramPolling.js#L34), causing the next poll interval to retrieve already-processed updates, including the update that originally caused the error! Thus LOOP!

Note that if the process terminated and we restarted it again, this bug would re-surface since we would not have marked the already-processed updates as confirmed! This is important considering that in the future, unhandled promise rejections will terminate the process. Thus, the fix will stop working some time in the future!

In some cases, the bug would disappear, as the offending update would have been discarded by Telegram after their 24hr limit!


fixing:

It is currently fixed!!!

The current fix works, as long as unhandled promise rejections do not terminate the process. Once Node.js behaves by terminating, this loop will re-surface! To prevent this Loop completely, we need a way to consider this:

  1. Processed updates MUST be marked confirmed
  2. The update causing the error may remain unconfirmed to allow re-processing (we need more discussion around this)

To achieve the above, in our .catch() (at src/telegramPolling.js#L49), we may do the following, in order:

  1. send a getUpdates request with the updated offset + 1, therefore marking all processed updates as confirmed!
  2. re-throw the error, thus terminating the process!

notes:

Reproducing this bug is important. If you believe that this bug has not been fixed, please provide an elaborate way to reproduce it. (@yagop had a hard time doing so)


related issues:

GochoMugo commented 7 years ago

The current fix works, as long as unhandled promise rejections do not terminate the process. Once Node.js behaves by terminating, this loop will re-surface! To prevent this Loop completely...

comment above

PR #265 handles (future) cases where unhandled promise rejections will terminate the process. Please proceed to the PR to leave comments on the implementation!

kamikazechaser commented 7 years ago

👍For fixing this!

luckydonald commented 7 years ago

@mvalipour link is down

mvalipour commented 7 years ago

@luckydonald Fixed it. damn trailing slash :/

GochoMugo commented 6 years ago

Fixed in v0.30.0.