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:
parent
3f908da91e
commit
12c8e30a1e
8 changed files with 184 additions and 15 deletions
|
@ -56,7 +56,8 @@ export class HttpApi {
|
|||
this.app.get(url.pathname, this.testWebhook);
|
||||
}
|
||||
|
||||
this.app.post(this.vk.endpoint, this.handleVkEvent);
|
||||
// VK event handler
|
||||
this.app.post(this.vk.endpoint, this.vk.handle);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,12 +74,4 @@ export class HttpApi {
|
|||
private testWebhook = async (req: Request, res: Response) => {
|
||||
res.sendStatus(200);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles VK events
|
||||
*/
|
||||
private handleVkEvent = async (req: Request, res: Response) => {
|
||||
await this.vk.handle(req.body);
|
||||
res.sendStatus(200);
|
||||
};
|
||||
}
|
||||
|
|
12
src/service/vk/handlers/MessageNewHandler.ts
Normal file
12
src/service/vk/handlers/MessageNewHandler.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { VkEventHandler } from "./types";
|
||||
import { ContextDefaultState, MessageContext } from "vk-io";
|
||||
import { NextMiddleware } from "middleware-io";
|
||||
|
||||
export class MessageNewHandler extends VkEventHandler<
|
||||
MessageContext<ContextDefaultState>
|
||||
> {
|
||||
public execute = async (context, next: NextMiddleware) => {
|
||||
console.log("received message!");
|
||||
await next();
|
||||
};
|
||||
}
|
11
src/service/vk/handlers/index.ts
Normal file
11
src/service/vk/handlers/index.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { VkEvent } from "../types";
|
||||
import { VkEventHandler } from "./types";
|
||||
import { MessageNewHandler } from "./MessageNewHandler";
|
||||
|
||||
export const vkEventToHandler: Record<VkEvent, typeof VkEventHandler> = {
|
||||
[VkEvent.GroupJoin]: VkEventHandler,
|
||||
[VkEvent.GroupLeave]: VkEventHandler,
|
||||
[VkEvent.MessageNew]: MessageNewHandler,
|
||||
[VkEvent.PostSuggestion]: VkEventHandler,
|
||||
[VkEvent.WallPostNew]: VkEventHandler,
|
||||
};
|
13
src/service/vk/handlers/types.ts
Normal file
13
src/service/vk/handlers/types.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { NextMiddleware } from "middleware-io";
|
||||
import { ConfigGroup } from "../types";
|
||||
|
||||
export class VkEventHandler<T = any> {
|
||||
public constructor(protected config: ConfigGroup) {}
|
||||
public execute: (context: T, next: NextMiddleware) => Promise<void> = async (
|
||||
ctx,
|
||||
next
|
||||
) => {
|
||||
console.log(`vk received unknown event`, ctx);
|
||||
await next();
|
||||
};
|
||||
}
|
|
@ -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>[])
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
import { API, Upload, Updates } from "vk-io";
|
||||
|
||||
export interface VkConfig extends Record<string, any> {
|
||||
groups: ConfigGroup[];
|
||||
endpoint?: string;
|
||||
}
|
||||
|
||||
interface ConfigGroup {
|
||||
export interface ConfigGroup {
|
||||
id: number;
|
||||
name: string;
|
||||
testResponse: string;
|
||||
|
@ -18,10 +20,15 @@ interface GroupChannel {
|
|||
}
|
||||
|
||||
export enum VkEvent {
|
||||
Confirmation = "confirmation",
|
||||
WallPostNew = "wall_post_new",
|
||||
PostSuggestion = "post_suggestion",
|
||||
GroupJoin = "group_join",
|
||||
GroupLeave = "group_leave",
|
||||
MessageNew = "message_new",
|
||||
}
|
||||
|
||||
export interface GroupInstance {
|
||||
api: API;
|
||||
upload: Upload;
|
||||
updates: Updates;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue