mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
#6 added duplicate events detection
This commit is contained in:
parent
2feae715f0
commit
93f9898bdf
1 changed files with 22 additions and 1 deletions
|
@ -19,6 +19,9 @@ export class VkService {
|
|||
private readonly instances: Record<string, GroupInstance>;
|
||||
private readonly groups: Record<number, ConfigGroup>;
|
||||
|
||||
// Used to register all incoming events
|
||||
private incomingEvents: string[] = [];
|
||||
|
||||
constructor(
|
||||
private config: VkConfig,
|
||||
private telegram: TelegramService,
|
||||
|
@ -56,6 +59,9 @@ export class VkService {
|
|||
const { body } = req;
|
||||
const { groups } = this;
|
||||
const groupId = body?.group_id;
|
||||
const group = this.groups[groupId];
|
||||
const eventId = body?.event_id;
|
||||
const type = body?.type;
|
||||
|
||||
if (!groupId || !has(groupId, groups) || !has(groupId, this.instances)) {
|
||||
logger.warn(`vk received unknown call`, { body });
|
||||
|
@ -63,11 +69,26 @@ export class VkService {
|
|||
return;
|
||||
}
|
||||
|
||||
if (eventId) {
|
||||
if (this.incomingEvents.includes(eventId)) {
|
||||
logger.warn(
|
||||
`received duplicate event "${type} (${eventId})" for group "${group.name} (skipping)"`
|
||||
);
|
||||
return res.send("OK");
|
||||
}
|
||||
|
||||
this.incomingEvents.push(eventId);
|
||||
}
|
||||
|
||||
logger.debug(`received vk event`, { body });
|
||||
|
||||
const inst = this.instances[groupId] as GroupInstance;
|
||||
|
||||
inst.updates.getWebhookCallback(this.config.endpoint)(req, res, next);
|
||||
await inst.updates.getWebhookCallback(this.config.endpoint)(
|
||||
req,
|
||||
res,
|
||||
next
|
||||
);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue