1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-25 15:06:41 +07:00

added working vk event handlers

This commit is contained in:
Fedor Katurov 2021-04-28 12:17:47 +07:00
parent 3f908da91e
commit 12c8e30a1e
8 changed files with 184 additions and 15 deletions

View file

@ -1,7 +1,16 @@
import { VkConfig } from "./types";
import { ConfigGroup, GroupInstance, VkConfig, VkEvent } from "./types";
import { API, Upload, Updates } from "vk-io";
import logger from "../logger";
import { Request, Response } from "express";
import { flatten, has, keys } from "ramda";
import { NextFunction } from "connect";
import { VkEventHandler } from "./handlers/types";
import { vkEventToHandler } from "./handlers";
export class VkService {
public endpoint: string = "/";
private readonly instances: Record<string, GroupInstance>;
private readonly groups: Record<number, ConfigGroup>;
constructor(private config: VkConfig) {
if (!config.groups.length) {
@ -9,12 +18,87 @@ export class VkService {
}
this.endpoint = config.endpoint;
this.groups = config.groups.reduce(
(acc, group) => ({
...acc,
[group.id]: group,
}),
{}
);
this.instances = config.groups.reduce(
(acc, group) => ({
...acc,
[group.id]: this.createGroupInstance(group),
}),
{}
);
}
/**
* Handles incoming VK events
*/
public handle = async (event: any) => {
// TODO: handle events
public handle = async (req: Request, res: Response, next: NextFunction) => {
try {
const { body } = req;
const { groups } = this;
const groupId = body?.group_id;
if (!groupId || !has(groupId, groups) || !has(groupId, this.instances)) {
logger.warn(`vk received unknown call`, { body });
res.sendStatus(200);
return;
}
logger.debug(`received vk event`, { body });
const inst = this.instances[groupId] as GroupInstance;
inst.updates.getWebhookCallback(this.config.endpoint)(req, res, next);
} catch (e) {
next(e);
}
};
private createGroupInstance = (group: ConfigGroup): GroupInstance => {
const api = new API({
token: group.apiKey,
apiBaseUrl: this.config.endpoint,
});
const upload = new Upload({ api });
const updates = new Updates({
api,
upload,
webhookConfirmation: group.testResponse,
webhookSecret: group.secretKey,
});
const handlers = this.setupHandlers(group);
handlers.forEach((channel) => {
keys(channel).forEach((event) => {
console.log(`updates in ${String(event)}`);
updates.on(event as any, channel[event].execute);
});
});
return {
api,
upload,
updates,
};
};
/**
* Setups handlers
*/
private setupHandlers(group: ConfigGroup): Record<VkEvent, VkEventHandler>[] {
return flatten(
group.channels.map((chan) =>
chan.events.reduce((acc, event) => {
const handler = vkEventToHandler[event];
return { ...acc, [event]: new handler(group) };
}, {} as Record<VkEvent, VkEventHandler>[])
)
);
}
}