Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sendMessage is not a function #192

Open
Daniele-Tentoni opened this issue Mar 13, 2021 · 1 comment
Open

sendMessage is not a function #192

Daniele-Tentoni opened this issue Mar 13, 2021 · 1 comment

Comments

@Daniele-Tentoni
Copy link

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
Copy link

devgioele commented Mar 26, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants