Node-telegram-bot-api: Send message if user message is not matched at any point.

Created on 31 May 2017  路  9Comments  路  Source: yagop/node-telegram-bot-api

Question

Is there a way to send a message to user if his text does not match any of the regex bot.onText()? Something like the welcome text but only if there is no match for user input.
Is there a built in method, or must I use some sort of logic to acheive this?
Looking through the documentation has not yielded any results for me. Any help would be greatly appreciated. Thanks.

question

Most helpful comment

You could TelegramBot#onText() with the options.onlyFirstMatch set to true. For example,

const TelegramBot = require("node-telegram-bot-api");
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, {
    polling: true,
    onlyFirstMatch: true,
});

bot.onText(/\/hello/, function(msg, match) {
    bot.sendMessage(msg.chat.id, "Hello, human!");
});

bot.onText(/\/world/, function(msg, match) {
    bot.sendMessage(msg.chat.id, "Welcome to my World!");
});

bot.onText(/.+/, function(msg, match) {
    bot.sendMessage(msg.chat.id, "I got you!");
});

Some simple demo:

demo

Also, we have a PR (https://github.com/yagop/node-telegram-bot-api/pull/313) proposing a middleware solution that might be of interest to you.

All 9 comments

You can listen to message sub-types shown here. E.g


// listen for any text message
bot.on("text", msg => {
   bot.sendMessage(msg.chat.id, "Hello World!");
});

// listen for new chat members
bot.on("new_chat_member", msg => {
   bot.sendMessage(msg.chat.id, "Welcome to the group!");
});

@kamikazechaser thanks, but on message this will send a message for any text + any response to matched text. So if user writes some text which is regex matched to a response, two messages will appear. What if I want to send only if the whole script does not match?

@kvyb
You can put all your regex in one array , and in function you want to send if there is no match , check it with for () or if ()

Thank you! I'll give that a try. An example is most welcome if possible.

@kvyb I did not understand what you meant earlier, but it is now clear. You want to send a message if none of the regex match, right?

@kamikazechaser that's right :+1:

Still not sure about the logic of they way to set this up if you have multiple bot.onText. Is there a way to tie them into some sort of logic which can be carried forward to default text which is given to user if not matched?

You could TelegramBot#onText() with the options.onlyFirstMatch set to true. For example,

const TelegramBot = require("node-telegram-bot-api");
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, {
    polling: true,
    onlyFirstMatch: true,
});

bot.onText(/\/hello/, function(msg, match) {
    bot.sendMessage(msg.chat.id, "Hello, human!");
});

bot.onText(/\/world/, function(msg, match) {
    bot.sendMessage(msg.chat.id, "Welcome to my World!");
});

bot.onText(/.+/, function(msg, match) {
    bot.sendMessage(msg.chat.id, "I got you!");
});

Some simple demo:

demo

Also, we have a PR (https://github.com/yagop/node-telegram-bot-api/pull/313) proposing a middleware solution that might be of interest to you.

Hi, how can the bot will reply to a message? as I can see, the bot is only 'messaging' not actually reply to the certain msg

Was this page helpful?
0 / 5 - 0 ratings