Bug Report
I have read:
I am using the latest version of the library.
v 0.26.0
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.
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.
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.
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
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
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!
});
});
~~~
node index.jsQuestion
My project, as at now, only tests sending messages but I could test other features If given approval
What node version are you currently using?
@preco21 node version 4.4.3
@crazyabdul I guess some of features are not supported in node@4: https://kangax.github.io/compat-table/es6/#node4
Could you upgrade your node to version 6 or 7?
Would be better to target a 4.x Node.js version.
For some reason, Travis tests for version 4.x are passing.
What do you think @GochoMugo ?
Seems fallbacks in index.js only works when if the node version is 0.x.
Still have no idea for about TravisCI..
Maybe this opts file is causing the problem? It is requiring babel-register internally, so I guess this is reason how it could worked even on node v4.
@preco21 Thanks for looking into this issue. It seems Node v4 does not support the syntax fully.
We can have the fallback work also for Node v4.x. However, I was hoping to only deprecate Node v0.x! Do we deprecate v4.x too?
Also, we should check if it runs on v5.x, v6.x, v7.x!
@preco21 It seems the quickest solution for me is to upgrade node to version 6. I'll update this thread on how it turns within the next few hours.
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.
@gochomugo I could look into adding support for node v4 upwards
@crazyabdul We could really do with some help! Thanks in advance!
@crazyabdul But we should discuss how to achieve that, to avoid having you do so much work only to be discarded!
What about simply do:
const majorVersion = process.versions.node.split('.')[0];
if (majorVersion === '0') {
const deprecate = require('depd')('node-telegram-bot-api');
deprecate('Node.js v0.12 and below will no longer be supported in the future');
module.exports = require('./lib/telegram');
} else if (majorVersion === '4') {
module.exports = require('./lib/telegram');
} else {
module.exports = require('./src/telegram');
}
or even:
const majorVersion = process.versions.node.split('.')[0];
if (majorVersion === '0') {
const deprecate = require('depd')('node-telegram-bot-api');
deprecate('Node.js v0.12 and below will no longer be supported in the future');
}
module.exports = require(majorVersion === '0' || majorVersion === '4' ? './lib/telegram' : './src/telegram');
Also, you may need to fix mocha config to correct TravisCI.
@gochoMugo you earlier mentioned something about a fallback. What versions of node are those fallbacks for and how were they implemented. I'd want to learn from what the team has done so far before coming with a plan
I guess you guys could consider about using babel-preset-env over static es2015 preset.
@preco21 then i can put the 2 js files I made node v4 compatible in ./src/telegram
I think that'll solve the issue
@crazyabdul I guess it should be on index.js but not on src/. It does not need to be transpiled.
@preco21 yeah. I see that now
@gochoMugo with @preco21 code snippet and the changes i mentioned in the bug report, this can be resolved fairly quickly.
What do you think?
My major issue is whether we should deprecate node v4.x, or fix the syntax to support node v4.x ? We really want to move away from babel!
@gochoMugo I came across this pdf showing stats on the various node versions
On page 18, it shows that majority of people use v4
And most of those using older versions intend to upgrade to v4
I think it's worth fixing the syntax
@gochoMugo what alternative to babel are you considering?
@crazyabdul we are hoping to use the source as is i.e. no transpiling!!!
According with https://nodesource.com/node-by-numbers v6 is more popular
But supporting v4 without transpiling would be the best IMO
@yagop checked your link out and it is clearly more up to date than the pdf i talked about.
The trend suggests that the number of v4 users are dropping fast.
So maybe there could be a v4 fallback for now till we are convinced that there are very few of v4 users left before deprecating it
So, I think we should push our community to using newer versions of Node.js. So we could deprecate older versions continuously. So it seems we will continue transpiling until the moment we will deprecate Node v4.x!
@gochoMugo should i go ahead and make a pull request?
@crazyabdul Good to go.
IMO, transpiles are needed. I don't think it makes sense to simply change the codebase to the old grammar to support older version of the node.
Newer grammar gives us readability, maintainable code, such advantages.
@crazyabdul Everything seems okay. We are waiting for a PR to fallback Node v4.x to use transpiled code. (@preco21's comment above is helpful.)
@preco21 I agree. At the rate at which Node releases newer versions, it seems unwise to downgrade our syntax to support older versions, which will be surpassed in quite a short time I presume.
@GochoMugo Pull request (https://github.com/yagop/node-telegram-bot-api/pull/280) sent
Great job guys!
PR #280 merged into master in commit d4a469df6b6eeae4c045faf7b6e63bdc765b0a15. Please try out the fix and report your results.
@GochoMugo @preco21 @yagop it's working now. I was able to run node index.js with no problems on v4.4.3. Didn't have to add the --use_strict option.
Thanks for letting me help out. Deeply appreciated
Fixed in v0.27.0.