mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 15:06:41 +07:00
added templaters
This commit is contained in:
parent
1754092f7c
commit
d5228ef146
13 changed files with 406 additions and 17 deletions
|
@ -1,11 +1,14 @@
|
|||
import { TelegramConfig } from "../service/telegram/types";
|
||||
import { VkConfig } from "../service/vk/types";
|
||||
import { VkConfig, VkEvent } from "../service/vk/types";
|
||||
import { HttpConfig } from "../api/http/types";
|
||||
import { LoggerConfig } from "../service/logger/types";
|
||||
|
||||
export type TemplateConfig = Record<VkEvent, string>;
|
||||
|
||||
export interface Config extends Record<string, any> {
|
||||
http: HttpConfig;
|
||||
telegram: TelegramConfig;
|
||||
vk: VkConfig;
|
||||
logger?: LoggerConfig;
|
||||
templates?: TemplateConfig;
|
||||
}
|
||||
|
|
|
@ -1,15 +1,20 @@
|
|||
import { boolean, object, string } from "yup";
|
||||
import { object, string } from "yup";
|
||||
import { httpConfigSchema } from "../api/http/validation";
|
||||
import { Config } from "./types";
|
||||
import { vkConfigSchema } from "../service/vk/validation";
|
||||
import { telegramConfigSchema } from "../service/telegram/validation";
|
||||
import { loggerConfigSchema } from "../service/logger/config";
|
||||
|
||||
const templateConfigSchema = object().shape({
|
||||
message_new: string().required(),
|
||||
});
|
||||
|
||||
const configSchema = object<Config>().required().shape({
|
||||
http: httpConfigSchema,
|
||||
vk: vkConfigSchema,
|
||||
telegram: telegramConfigSchema,
|
||||
logger: loggerConfigSchema,
|
||||
templates: templateConfigSchema,
|
||||
});
|
||||
|
||||
export const validateConfig = (config: Config) =>
|
||||
|
|
|
@ -9,7 +9,7 @@ async function main() {
|
|||
try {
|
||||
const config = prepareConfig();
|
||||
const telegram = new TelegramService(config.telegram);
|
||||
const vkService = new VkService(config.vk, telegram);
|
||||
const vkService = new VkService(config.vk, telegram, config.templates);
|
||||
|
||||
const telegramApi = new TelegramApi(telegram).listen();
|
||||
await telegram.start();
|
||||
|
|
39
src/service/template/index.ts
Normal file
39
src/service/template/index.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import extract from "remark-extract-frontmatter";
|
||||
import frontmatter from "remark-frontmatter";
|
||||
import compiler from "remark-stringify";
|
||||
import parser from "remark-parse";
|
||||
import unified, { Processor } from "unified";
|
||||
import { parse } from "yaml";
|
||||
import toVFile from "to-vfile";
|
||||
import path from "path";
|
||||
|
||||
export class Template<T extends Record<string, any>> {
|
||||
private processor: Processor;
|
||||
public fields: T = {} as T;
|
||||
public template: string = "";
|
||||
|
||||
constructor(filename: string) {
|
||||
try {
|
||||
if (!filename) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.processor = unified()
|
||||
.use(parser)
|
||||
.use(compiler)
|
||||
.use(frontmatter)
|
||||
.use(extract, { yaml: parse });
|
||||
|
||||
const file = toVFile.readSync(path.join(__dirname, "../../", filename));
|
||||
const result = this.processor.processSync(file);
|
||||
|
||||
this.fields = result.data as T;
|
||||
this.template = result
|
||||
.toString()
|
||||
.replace(/^---\n(.*)---\n?$/gms, "")
|
||||
.trim();
|
||||
} catch (e) {
|
||||
throw new Error(`Template: ${e.toString()}`);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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