mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
added per-group and per-channel templates
This commit is contained in:
parent
a3fcb429ce
commit
3f2e07b391
5 changed files with 28 additions and 5 deletions
|
@ -14,7 +14,7 @@ vk:
|
|||
# Default path for POST requests from VK api
|
||||
endpoint: /
|
||||
groups: []
|
||||
templates:
|
||||
templates: # (required)
|
||||
message_new: templates/message_new.md
|
||||
wall_post_new: templates/post_new.md
|
||||
group_join: templates/group_join.md
|
||||
|
@ -26,6 +26,8 @@ templates:
|
|||
# secretKey: 'groupSecretKey'
|
||||
# apiKey: 'callbackApiKey'
|
||||
# post_types: ['post','copy','reply','postpone','suggest']
|
||||
# templates: # group's custom templates (optional)
|
||||
# message_new: templates/custom/message_new.md
|
||||
# channels:
|
||||
# - id: '@pogonia_test_chan'
|
||||
# events:
|
||||
|
@ -34,4 +36,5 @@ templates:
|
|||
# - group_join
|
||||
# - group_leave
|
||||
# - message_new
|
||||
|
||||
# templates: # channel custom templates (optional)
|
||||
# wall_post_new: templates/custom/post_new.md
|
||||
|
|
|
@ -6,8 +6,18 @@ import { telegramConfigSchema } from "../service/telegram/validation";
|
|||
import { loggerConfigSchema } from "../service/logger/config";
|
||||
import { dbConfigValidatior } from "../service/db/postgres/validation";
|
||||
|
||||
const templateConfigSchema = object().shape({
|
||||
export const templateConfigSchema = object().required().shape({
|
||||
message_new: string().required(),
|
||||
wall_post_new: string().required(),
|
||||
group_join: string().required(),
|
||||
group_leave: string().required(),
|
||||
});
|
||||
|
||||
export const templateOptionalSchema = object().shape({
|
||||
message_new: string(),
|
||||
wall_post_new: string(),
|
||||
group_join: string(),
|
||||
group_leave: string(),
|
||||
});
|
||||
|
||||
const configSchema = object<Config>().required().shape({
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ConfigGroup, GroupInstance, VkConfig, VkEvent } from "./types";
|
|||
import { API, Updates, Upload } from "vk-io";
|
||||
import logger from "../logger";
|
||||
import { Request, Response } from "express";
|
||||
import { flatten, has, keys } from "ramda";
|
||||
import { flatten, has, keys, prop } from "ramda";
|
||||
import { NextFunction } from "connect";
|
||||
import { VkEventHandler } from "./handlers/VkEventHandler";
|
||||
import { vkEventToHandler } from "./handlers";
|
||||
|
@ -137,7 +137,11 @@ export class VkService {
|
|||
return flatten(
|
||||
group.channels.map((chan) =>
|
||||
chan.events.reduce((acc, event) => {
|
||||
const template = new Template(this.templates[event]);
|
||||
const template = new Template(
|
||||
prop(event, chan?.templates) ||
|
||||
prop(event, group?.templates) ||
|
||||
prop(event, this.templates)
|
||||
);
|
||||
|
||||
const handler = new vkEventToHandler[event](
|
||||
event,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { API, Upload, Updates } from "vk-io";
|
||||
import { WallPostType } from "vk-io/lib/api/schemas/objects";
|
||||
import { TemplateConfig } from "../../config/types";
|
||||
|
||||
export interface VkConfig extends Record<string, any> {
|
||||
groups: ConfigGroup[];
|
||||
|
@ -13,12 +14,14 @@ export interface ConfigGroup {
|
|||
secretKey: string;
|
||||
apiKey: string;
|
||||
channels: GroupChannel[];
|
||||
templates: Partial<TemplateConfig>;
|
||||
}
|
||||
|
||||
export interface GroupChannel {
|
||||
id: string;
|
||||
events: VkEvent[];
|
||||
post_types: WallPostType[];
|
||||
templates: Partial<TemplateConfig>;
|
||||
}
|
||||
|
||||
export enum VkEvent {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import * as yup from "yup";
|
||||
import { VkConfig, VkEvent } from "./types";
|
||||
import { templateOptionalSchema } from "../../config/validate";
|
||||
|
||||
const vkChannelEventSchema = yup.string().oneOf(Object.values(VkEvent));
|
||||
|
||||
|
@ -12,6 +13,7 @@ const vkChannelSchema = yup
|
|||
.required()
|
||||
.matches(/^@/, ({ path }) => `${path} should start with "@"`),
|
||||
events: yup.array().of(vkChannelEventSchema),
|
||||
templates: templateOptionalSchema,
|
||||
});
|
||||
|
||||
export const vkConfigSchema = yup
|
||||
|
@ -30,6 +32,7 @@ export const vkConfigSchema = yup
|
|||
secretKey: yup.string().required(),
|
||||
apiKey: yup.string().required(),
|
||||
channels: yup.array().of(vkChannelSchema),
|
||||
templates: templateOptionalSchema,
|
||||
})
|
||||
),
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue