Node-telegram-bot-api: How to check if a command is executed? - bot.on()

Created on 27 Mar 2019  路  5Comments  路  Source: yagop/node-telegram-bot-api

How do I check if a user executed a command? I would like to separate normal text and commands from eachother so I could give them a diffrent tag/type in my database.

Do I need to do something like this?:

bot.on('message',(msg)
message = msg.chat.text
check = message.slice(0,1)
if(check = '/'){
//command
} else {
//Normal text
}
);
question

Most helpful comment

Because it msg object may not have it, if no entities are available. So, executing .some on undefined will report error.

All 5 comments

The message type has an entity field to show if its a command. You can inspect the msg object for it.

The message type has an entity field to show if its a command. You can inspect the msg object for it.

Do you have an example?

bot.on("message", (msg) => {
    if (msg.entities && msg.entities.some((e) => e.type === "bot_command")) {
        // here if the message has bot command
    }
});
bot.on("message", (msg) => {
    if (msg.entities && msg.entities.some((e) => e.type === "bot_command")) {
        // here if the message has bot command
    }
});

Why the msg.entities &&?

Because it msg object may not have it, if no entities are available. So, executing .some on undefined will report error.

Was this page helpful?
0 / 5 - 0 ratings