mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-26 07:26:41 +07:00
added templaters
This commit is contained in:
parent
1754092f7c
commit
d5228ef146
13 changed files with 406 additions and 17 deletions
|
@ -23,6 +23,9 @@ export class MessageNewHandler extends VkEventHandler {
|
|||
`received message from ${from.first_name} ${from.last_name}: ${context.text}`
|
||||
);
|
||||
|
||||
const template = this.template.template;
|
||||
const fields = this.template.fields;
|
||||
|
||||
await next();
|
||||
};
|
||||
}
|
||||
|
|
13
src/service/vk/handlers/StubHandler.ts
Normal file
13
src/service/vk/handlers/StubHandler.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { VkEventHandler } from "./VkEventHandler";
|
||||
import { NextMiddleware } from "middleware-io";
|
||||
import logger from "../../logger";
|
||||
|
||||
/**
|
||||
* StubHandler is used to stub event calls
|
||||
*/
|
||||
export class StubHandler extends VkEventHandler {
|
||||
public execute = async (context: any, next: NextMiddleware) => {
|
||||
logger.debug(`received unhandled message of type "${context.type}"`);
|
||||
await next();
|
||||
};
|
||||
}
|
|
@ -1,14 +1,17 @@
|
|||
import { NextMiddleware } from "middleware-io";
|
||||
import { ConfigGroup, GroupInstance } from "../types";
|
||||
import { ConfigGroup, GroupInstance, VkEvent } from "../types";
|
||||
import { VkService } from "../index";
|
||||
import { TelegramService } from "../../telegram";
|
||||
import { Template } from "../../template";
|
||||
|
||||
export abstract class VkEventHandler {
|
||||
export class VkEventHandler {
|
||||
public constructor(
|
||||
protected type: VkEvent,
|
||||
protected group: ConfigGroup,
|
||||
protected instance: GroupInstance,
|
||||
protected vk: VkService,
|
||||
protected telegram: TelegramService
|
||||
protected telegram: TelegramService,
|
||||
protected template: Template<any>
|
||||
) {}
|
||||
|
||||
public execute: (
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
import { VkEvent } from "../types";
|
||||
import { VkEventHandler } from "./VkEventHandler";
|
||||
import { MessageNewHandler } from "./MessageNewHandler";
|
||||
import { StubHandler } from "./StubHandler";
|
||||
|
||||
type DerivedHandler = typeof VkEventHandler;
|
||||
interface Handler extends DerivedHandler {}
|
||||
|
||||
export const vkEventToHandler: Record<VkEvent, Handler> = {
|
||||
[VkEvent.GroupJoin]: MessageNewHandler,
|
||||
[VkEvent.GroupLeave]: MessageNewHandler,
|
||||
[VkEvent.GroupJoin]: StubHandler,
|
||||
[VkEvent.GroupLeave]: StubHandler,
|
||||
[VkEvent.MessageNew]: MessageNewHandler,
|
||||
[VkEvent.PostSuggestion]: MessageNewHandler,
|
||||
[VkEvent.WallPostNew]: MessageNewHandler,
|
||||
[VkEvent.PostSuggestion]: StubHandler,
|
||||
[VkEvent.WallPostNew]: StubHandler,
|
||||
};
|
||||
|
|
|
@ -7,13 +7,19 @@ import { NextFunction } from "connect";
|
|||
import { VkEventHandler } from "./handlers/VkEventHandler";
|
||||
import { vkEventToHandler } from "./handlers";
|
||||
import { TelegramService } from "../telegram";
|
||||
import { Template } from "../template";
|
||||
import { TemplateConfig } from "../../config/types";
|
||||
|
||||
export class VkService {
|
||||
public endpoint: string = "/";
|
||||
private readonly instances: Record<string, GroupInstance>;
|
||||
private readonly groups: Record<number, ConfigGroup>;
|
||||
|
||||
constructor(private config: VkConfig, private telegram: TelegramService) {
|
||||
constructor(
|
||||
private config: VkConfig,
|
||||
private telegram: TelegramService,
|
||||
private templates: TemplateConfig
|
||||
) {
|
||||
if (!config.groups.length) {
|
||||
throw new Error("No vk groups to handle. Specify them in config");
|
||||
}
|
||||
|
@ -101,11 +107,15 @@ export class VkService {
|
|||
return flatten(
|
||||
group.channels.map((chan) =>
|
||||
chan.events.reduce((acc, event) => {
|
||||
const handler = new (vkEventToHandler as any)[event](
|
||||
const template = new Template(this.templates[event]);
|
||||
|
||||
const handler = new vkEventToHandler[event](
|
||||
event,
|
||||
group,
|
||||
instance,
|
||||
this,
|
||||
this.telegram
|
||||
this.telegram,
|
||||
template
|
||||
);
|
||||
return { ...acc, [event]: handler };
|
||||
}, {} as Record<VkEvent, VkEventHandler>[])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue