telebot icon indicating copy to clipboard operation
telebot copied to clipboard

sendMessage is not a function

Open Daniele-Tentoni opened this issue 3 years ago • 1 comments

bot.sendMessage is not a function

Hi all, probably is a problem with my entry level of Javascript. I made a module with a sample code for webhook bot:

const TelegramBot = require('telebot');
const bot = new TelegramBot({
    token: process.env.TELEGRAM_BOT_TOKEN
});
bot.on('text', msg => bot.sendMessage(msg.from.id, msg.text));
bot.on('/hello', msg => msg.reply.test('welcome'));
bot.on('edit', (msg) => {
    return msg.reply.text('I saw it! You edited message!', { asReply: true });
});
bot.setWebhook(`${process.env.URL}:443`);
bot.start();
module.exports = { bot }

And then required it in main module:

const TelegramBot = require('telebot');
const bot = require('./telegram');

const server = app.listen(port, (err) => {
    if (!err) {
        console.log(`App started on ${process.env.URL}:${port}`);
        if (process.env.TELEGRAM_CHAT_NOTIFICATION)
            bot.sendMessage(process.env.TELEGRAM_CHAT_NOTIFICATION, `Application deployed and running without errors on ${process.env.URL}.`);
    } else {
        console.error(err);
    }
});

But console log me that bot.sendMessage is not a function.

Solution proposal

The solution I made was to create a new function:

const sendMessageTo = (id, msg) => bot.sendMessage(id, msg);
module.exports = { bot, sendMessageTo }

and then call it in main module:

const { bot, sendMessageTo } = require('./telegram');

sendMessageTo(process.env.TELEGRAM_CHAT_NOTIFICATION, `Application deployed and running without errors on ${process.env.URL}.`);

Question

Is this the only solution can be made? There's one better than this?

Daniele-Tentoni avatar Mar 13 '21 22:03 Daniele-Tentoni

I had the same issue with JetBrain's WebStorm IDE. It's simply because the IDE is not smart enough to figure it out. You can disable the inspection for that method to remove the warning. The developers of telebot could JSDoc the methods such that IDEs figure it out.

devgioele avatar Mar 26 '21 17:03 devgioele