Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates.ForEach is not a function #284

Closed
dcparga opened this issue Feb 8, 2017 · 19 comments
Closed

updates.ForEach is not a function #284

dcparga opened this issue Feb 8, 2017 · 19 comments
Labels

Comments

@dcparga
Copy link

dcparga commented Feb 8, 2017

Bug Report

I have read:

I am using the latest version of the library.

Expected Behavior

The process should be able to run without problem and indefinitely without throwing an error.

Actual Behavior

After the whole day running without problems, around 9pm my process start to throw this message continuously:

Unhandled rejection TypeError: updates.forEach is not a function
at _lastRequest._getUpdates.then.updates (/app/node_modules/node-telegram-bot-api/src/telegramPolling.js:102:17)
at tryCatcher (/app/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/app/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (/app/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (/app/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (/app/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (/app/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/app/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/app/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:649:20)
at tryOnImmediate (timers.js:622:5)
at processImmediate [as _immediateCallback] (timers.js:594:5)

@GochoMugo
Copy link
Collaborator

Are you able to reproduce the bug?

@dcparga
Copy link
Author

dcparga commented Feb 9, 2017

Not in an easy way. I'm not sure why it happens. The only thing I know it happened to me twice, both of them at almost the same time on two conseqcutives days (around 9pm CET).

I'm not sure, but I think is the same error that appears when you have more than one instance of the bot running.

@GochoMugo
Copy link
Collaborator

If you want to run multiple instances of the application, please use WebHooks. Using polling is not scalable. See more help information on scaling (Sorry! Help information is a bit incomplete! We really need help in that area.).

Otherwise, it will be hard for us to fix the bug (comfortably) if we can not pinpoint why and how it occurs. We need to investigate more on this.

@dcparga
Copy link
Author

dcparga commented Feb 9, 2017

Gocho, maybe I have explained myself wrong. The thing is that I don't want to run multiple instances of my bot, only one. But I think that the error that appears is the same as if I run more than one. I just said that to see if it could help to understand better the error.

I'll try to give more information if I'm able to get a better understading of the error. I just wanted to see if someone could bring some light on the cause of the error.

BTW, I'm going to try the webhook, just to see if the behaviour improves and errors are avoided.

@lokhmakov
Copy link

lokhmakov commented Feb 10, 2017

This simple script:

import TelegramBot from 'node-telegram-bot-api'

const token = `...`

const bot = new TelegramBot(token, { polling: true })

Result:

Unhandled rejection TypeError: updates.forEach is not a function
at _lastRequest._getUpdates.then.updates (PROJECT_DIR/node_modules/node-telegram-bot-api/src/telegramPolling.js:102:17)
at tryCatcher (PROJECT_DIR/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (PROJECT_DIR/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (PROJECT_DIR/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (PROJECT_DIR/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (PROJECT_DIR/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (PROJECT_DIR/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (PROJECT_DIR/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (PROJECT_DIR/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:651:20)
at tryOnImmediate (timers.js:624:5)
at processImmediate [as _immediateCallback] (timers.js:596:5)

Month ago all works fine.

@lokhmakov
Copy link

telegramPolling.js:102 updates return boolean true not array

@mjanssen
Copy link

Having the same issue after updating to 0.26.
The _getUpdates() function is returning a bool instead of an useable array. No clue why...
I also noticed the setWebHook request is called every time it's polling as well, even though I'm not using a webook.

@lokhmakov
Copy link

Stack trace upper:

src/telegram.js:172

data is

{ ok: true, result: true, description: 'Webhook is already deleted' }

@mjanssen
Copy link

Seems to be working fine now. Didn't change anything 🤔

@dcparga
Copy link
Author

dcparga commented Feb 10, 2017

I've been using the polling option for a whole day whitout problems, it just started to fail suddenly, my app crashed, and after rebooting it, it worked again like a charm. That make me thing that it could be something related with Telegram servers, but I cannot assure it. Is just a intuition.

@lokhmakov
Copy link

I think something wrong with logic of enable polling. Polling wait array of polling results but get in result boolean. May be telegram servers automatically delete webHook and no need to call it manually

@lokhmakov
Copy link

Try to change polling and return if result data not array

@lokhmakov
Copy link

lokhmakov commented Feb 10, 2017

Change src/telegramPolling.js:97 to:

  _polling() {
    this._lastRequest = this
      ._getUpdates()
      .then(updates => {
        if (!(!!updates && Array === updates.constructor)) return
        this._lastUpdate = Date.now();
        debug('polling data %j', updates);
        updates.forEach(update => {

And all works good.

@GochoMugo
Copy link
Collaborator

Thanks to this discussion that I have identified the bug. Yes, this is a bug in the library.

This occurs when the bot, while polling, tries to delete the already-set webhook, so as to be able to get updates. I have been able to reproduce this bug (a test for this bug will be added) and am working to push a fix for this.

@GochoMugo GochoMugo added bug and removed investigate labels Feb 10, 2017
GochoMugo added a commit that referenced this issue Feb 10, 2017
Bug:

  During polling, deleting the already-set webhook, caused
  the `TelegramBotPolling#_getUpdates()` return an unexpected
  value.

  We expect the method to return an array (in the `.then()` clause).
  However, deleting the webhook returns its value, which is an object,
  from the method `_getUpdates()`.

Fix:

  Simply retry the polling request and return the promise.

Notes:

  Should we use recursion? I do not think so.
  Why? The chances of getting the error (having a webhook set) AGAIN
  is quite rare. And if it happens, there must be some problem with
  different instances invoking polling and webhook simultaneously.
  In that case, we wont struggle to recover from such a scenario.
  User is on their own! Isht!

References:

  * Bug report:   #284
  * Reported by:  @dcparga
@GochoMugo
Copy link
Collaborator

Just pushed a fix for this in commit 130f6940ceb9972a7b0ac0636ac16e7e53984a46 (see how to reproduce the bug, and how we have fixed it!). Please try it out and let us know of the results.

@lokhmakov
Copy link

Console repeat this:

{ Error: ETELEGRAM: 409 Conflict: terminated by other long poll or webhook
    at request.then.resp (PROJECT_DIR/node_modules/node-telegram-bot-api/src/telegram.js:181:15)
    at tryCatcher (PROJECT_DIR/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (PROJECT_DIR/node_modules/bluebird/js/release/promise.js:510:31)
    at Promise._settlePromise (PROJECT_DIR/node_modules/bluebird/js/release/promise.js:567:18)
    at Promise._settlePromise0 (PROJECT_DIR/node_modules/bluebird/js/release/promise.js:612:10)
    at Promise._settlePromises (PROJECT_DIR/node_modules/bluebird/js/release/promise.js:691:18)
    at Async._drainQueue (PROJECT_DIR/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (PROJECT_DIR/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (PROJECT_DIR/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:651:20)
    at tryOnImmediate (timers.js:624:5)
    at processImmediate [as _immediateCallback] (timers.js:596:5)
  code: 'ETELEGRAM',
  response: 
   IncomingMessage {
     _readableState: 
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: [Object],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: true,
...

@GochoMugo
Copy link
Collaborator

Do you have other instances of your bot running?

@lokhmakov
Copy link

Created new bot. All works fine. 10x

GochoMugo added a commit that referenced this issue Feb 10, 2017
Added:

1. Add constructor options:
  * (#243) `options.polling.params` (by @GochoMugo, requested-by @sidelux)
1. Add methods:
  * (#74) *TelegramBot#removeReplyListener()* (by @githugger)
1. (#283) Add proper error handling (by @GochoMugo)
1. (#272) Add health-check endpoint (by @mironov)
  * `options.webHook.healthEndpoint`
1. (#152) Add test for TelegramBot#sendDocument() using 'fileOpts'
   param (by @evolun)
1. Document `options.webHook.host` (by @GochoMugo)
1. (#264) Add Bot API version to README (by @kamikazechaser)
1. Add examples:
  - (#271) WebHook on Heroku (by @TheBeastOfCaerbannog)
  - (#274) WebHook on Zeit Now (by @ferrari)

Changed:

1. (#147) Use *String#indexOf()*, instead of *RegExp#test()*, to
   find token in webhook request (by @AVVS)

Fixed:

* Fix bug:
  - (#275, #280) fix es6 syntax error on Node.js v4.x (by @CrazyAbdul)
  - (#276) promise.warning from `request-promise` (by @GochoMugo,
    reported-by @preco21)
  - (#281) fix handling error during polling (by @GochoMugo,
    reported-by @dimawebmaker)
  - (#284) fix error during deletion of already-set webhook, during
    polling (by @GochoMugo, reported-by @dcparga)
1. Fix links in documentation (by @Ni2c2k)
@GochoMugo
Copy link
Collaborator

Fixed in v0.27.0.

Thanks @dcparga, @lokhmakov and @mjanssen for your time and effort in fixing this bug.

N-Agency-member pushed a commit to N-Agency-member/TelegramBot-API-Node.js- that referenced this issue Sep 3, 2024
Bug:

  During polling, deleting the already-set webhook, caused
  the `TelegramBotPolling#_getUpdates()` return an unexpected
  value.

  We expect the method to return an array (in the `.then()` clause).
  However, deleting the webhook returns its value, which is an object,
  from the method `_getUpdates()`.

Fix:

  Simply retry the polling request and return the promise.

Notes:

  Should we use recursion? I do not think so.
  Why? The chances of getting the error (having a webhook set) AGAIN
  is quite rare. And if it happens, there must be some problem with
  different instances invoking polling and webhook simultaneously.
  In that case, we wont struggle to recover from such a scenario.
  User is on their own! Isht!

References:

  * Bug report:   yagop/node-telegram-bot-api#284
  * Reported by:  @dcparga
passion-27 added a commit to passion-27/node-telegram-bot that referenced this issue Oct 2, 2024
Bug:

  During polling, deleting the already-set webhook, caused
  the `TelegramBotPolling#_getUpdates()` return an unexpected
  value.

  We expect the method to return an array (in the `.then()` clause).
  However, deleting the webhook returns its value, which is an object,
  from the method `_getUpdates()`.

Fix:

  Simply retry the polling request and return the promise.

Notes:

  Should we use recursion? I do not think so.
  Why? The chances of getting the error (having a webhook set) AGAIN
  is quite rare. And if it happens, there must be some problem with
  different instances invoking polling and webhook simultaneously.
  In that case, we wont struggle to recover from such a scenario.
  User is on their own! Isht!

References:

  * Bug report:   yagop/node-telegram-bot-api#284
  * Reported by:  @dcparga
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants