Node-telegram-bot-api: updates.ForEach is not a function

Created on 8 Feb 2017  路  19Comments  路  Source: yagop/node-telegram-bot-api

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)

bug

Most helpful comment

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.

All 19 comments

Are you able to reproduce the bug?

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.

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.

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.

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.

telegramPolling.js:102 updates return boolean true not array

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.

Stack trace upper:

src/telegram.js:172

data is

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

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

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.

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

Try to change polling and return if result data not array

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.

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.

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.

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,
...

Do you have other instances of your bot running?

Created new bot. All works fine. 10x

Fixed in v0.27.0.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Hostile picture Hostile  路  3Comments

dimawebmaker picture dimawebmaker  路  3Comments

Lemmmy picture Lemmmy  路  3Comments

hems picture hems  路  3Comments

Panthro picture Panthro  路  3Comments