Answer the question
In order to leave comments, you need to log in
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
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();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question