var TelegramBot = require('node-telegram-bot-api');
var token = '';
const bot = new TelegramBot(token, {polling:true});
bot.onText('message',function(msg) {
var text = msg.text;
var chatId = msg.chat.id;
var type = msg.chat.type;
var userid = msg.from.id;
if(text == "id"){
bot.sendMessage(chatId+"chtid");
}
});
var opt = {polling:true};
var bot = new Telegram(token,opt);
Add a listener in your code like:
bot.on("polling_error", console.log);
It is probably a syntax error in your code. Something undefined or such that makes crash your program.
I also had the same problem. After adding a listener like @alexandercerutti mentioned I got the following exception:
TypeError: reg.regexp.exec is not a function
at D:\Programming\notinglytelegrambot\node_modules\node-telegram-bot-api\src\telegram.js:607:37
at Array.some (<anonymous>)
at TelegramBot.processUpdate (D:\Programming\notinglytelegrambot\node_modules\node-telegram-bot-api\src\telegram.js:605:35)
at D:\Programming\notinglytelegrambot\node_modules\node-telegram-bot-api\src\telegramPolling.js:110:22
at Array.forEach (<anonymous>)
at D:\Programming\notinglytelegrambot\node_modules\node-telegram-bot-api\src\telegramPolling.js:106:17
at tryCatcher (D:\Programming\notinglytelegrambot\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (D:\Programming\notinglytelegrambot\node_modules\bluebird\js\release\promise.js:547:31)
at Promise._settlePromise (D:\Programming\notinglytelegrambot\node_modules\bluebird\js\release\promise.js:604:18)
at Promise._settlePromise0 (D:\Programming\notinglytelegrambot\node_modules\bluebird\js\release\promise.js:649:10)
at Promise._settlePromises (D:\Programming\notinglytelegrambot\node_modules\bluebird\js\release\promise.js:729:18)
at _drainQueueStep (D:\Programming\notinglytelegrambot\node_modules\bluebird\js\release\async.js:93:12)
at _drainQueue (D:\Programming\notinglytelegrambot\node_modules\bluebird\js\release\async.js:86:9)
at Async._drainQueues (D:\Programming\notinglytelegrambot\node_modules\bluebird\js\release\async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (D:\Programming\notinglytelegrambot\node_modules\bluebird\js\release\async.js:15:14)
at processImmediate (internal/timers.js:456:21)
To solve this I changed the regexp value from string to a RegExp object, which solved the problem for me. If you've the same error you've to change your call to the following:
bot.onText(RegExp('message'), function(msg) {
I hope this helps ;)
Edit: You can also just remove the ' Semicolon
@McCrafterIV Can you show what you wrote instead of the RegExp object? Because I've never seen that error happening.
Oh freak! I've seen now, in that unformatted piece of code in the first message, that the problem is, of course, the fact that it is being used the method onText with a string as parameter instead of a Regex. I think that happened too to you @McCrafterIV. Can you confirm?
@McCrafterIV Can you show what you wrote instead of the RegExp object? Because I've never seen that error happening.
I've basically removed the quotes and added a valid regex "string":
bot.onText(RegExp(/message/), function(msg) {
Oh freak! I've seen now, in that unformatted piece of code in the first message, that the problem is, of course, the fact that it is being used the method
onTextwith a string as parameter instead of a Regex. I think that happened too to you @McCrafterIV. Can you confirm?
Right that was my problem
Of course it was failing. Because it was trying to call exec method on a non-Regex Object (strings don't have it). You can also write just /message/, without using RegExp object.
bot.onText(/message/, (msg) => { ... });
I also have this problem who can help
Most helpful comment
Add a listener in your code like:
It is probably a syntax error in your code. Something undefined or such that makes crash your program.