node-telegram-bot-api: Block scoped declarations (let, const, function,class) not yet supported outside strict mode

Bug Report

I have read:

I am using the latest version of the library. v 0.26.0

Expected Behavior

I was trying to run the bot after writing some lines of code. I expected that after runningnode index.js, there will be no errors and when i send a request from telegram, get the desired result.

Actual Behavior

what happened instead was this error in the console

class TelegramBot extends EventEmitter {
^^^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (E:\Code\dobo\node_modules\node-telegram-bot-api\index.js:12:20)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

So i run this instead node --use_strict index.js and got different set of errors

constructor(token, options = {}) {
                             ^

SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (E:\Code\dobo\node_modules\node-telegram-bot-api\index.js:12:20)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

So after a while, I went through telegram.js and telegramPolling.js and changed the following function parameters

  1. options = {} to options
  2. forms = {} to forms
  3. fileOpts = {} to fileOpts

and in some cases, initialized the variable to an empty object within the function. For example

startPolling(options) {
    options = {};
    if (this.hasOpenWebHook()) {
      return Promise.reject(new Error('Polling and WebHook are mutually exclusive'));
    }
    options.restart = typeof options.restart === 'undefined' ? true : options.restart;
    if (!this._polling) {
      this._polling = new TelegramBotPolling(this._request.bind(this), this.options.polling, this.processUpdate.bind(this));
    }
    return this._polling.start(options);
  }

It was after taking these steps that it finally worked

Steps to reproduce the Behavior

  1. write an index.js file with following code
var token = '<insert token here>';

var Bot = require('node-telegram-bot-api'),
    bot = new Bot(token, { polling: true });

bot.onText(/^\/say_hello (.+)$/, function (msg, match) {
  var name = match[1];
  bot.sendMessage(msg.chat.id, 'Hello ' + name + '!').then(function () {
    // reply sent!
  });
});
  1. run node index.js

Question

My project, as at now, only tests sending messages but I could test other features If given approval

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 34 (12 by maintainers)

Commits related to this issue

Most upvoted comments

I guess you guys could consider about using babel-preset-env over static es2015 preset.

If you can use the latest version of Node, I would suggest you go with that. However, since we are promising our users that we support Node v4.x, we need to fix this.