mullwar / telebot

The easy way to write Telegram bots in Node.js
https://www.npmjs.com/package/telebot
MIT License
1.48k stars 270 forks source link

sendMessage is not a function #192

Open Daniele-Tentoni opened 3 years ago

Daniele-Tentoni commented 3 years ago

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?

devgioele commented 3 years ago

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.