T
T
THEMOD2019-06-26 21:58:26
In contact with
THEMOD, 2019-06-26 21:58:26

Why is the bot not responding to mentions?

I am writing a bot on the vk-io library. The bot does not respond to mentions, that is, in a conversation, the bot responds to the "Help" command, but does not respond to "@bot Help", how to solve it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladlen Hellsite, 2019-06-28
@THEMOD

Everything is simple, since the text comes with a mention for handlers - this is not universal. Solving the problem is very simple, we need to clean up the text from mentioning to using it. You can write such a middleware, it will check for a mention in the chat and remove it from the text for any other options (it will also work in the LAN). In other cases, it simply will not skip further execution.

const GROUP_ID = 123456789;

const mentionPattern = new RegExp(
    String.raw`^(?:\[club${GROUP_ID}\|[^\]]+\])(?:[\s.,\'\"!?\-+]+|$)`,
    'i'
);

vk.updates.on('message', (context, next) => {
    if (context.isChat && !mentionPattern.test(context.text)) {
        return;
    }

    if (context.text) {
        context.text = context.text.replace(mentionPattern, '');   
    }

    return next();
});

Regular expression demo

S
SagePtr, 2019-06-26
@SagePtr

Most likely you are checking for an exact match to a command, and "@bot Help" !== "Help"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question