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
|
# Default path for POST requests from VK api
|
||||||
endpoint: /
|
endpoint: /
|
||||||
groups: []
|
groups: []
|
||||||
templates:
|
templates: # (required)
|
||||||
message_new: templates/message_new.md
|
message_new: templates/message_new.md
|
||||||
wall_post_new: templates/post_new.md
|
wall_post_new: templates/post_new.md
|
||||||
group_join: templates/group_join.md
|
group_join: templates/group_join.md
|
||||||
|
@ -26,6 +26,8 @@ templates:
|
||||||
# secretKey: 'groupSecretKey'
|
# secretKey: 'groupSecretKey'
|
||||||
# apiKey: 'callbackApiKey'
|
# apiKey: 'callbackApiKey'
|
||||||
# post_types: ['post','copy','reply','postpone','suggest']
|
# post_types: ['post','copy','reply','postpone','suggest']
|
||||||
|
# templates: # group's custom templates (optional)
|
||||||
|
# message_new: templates/custom/message_new.md
|
||||||
# channels:
|
# channels:
|
||||||
# - id: '@pogonia_test_chan'
|
# - id: '@pogonia_test_chan'
|
||||||
# events:
|
# events:
|
||||||
|
@ -34,4 +36,5 @@ templates:
|
||||||
# - group_join
|
# - group_join
|
||||||
# - group_leave
|
# - group_leave
|
||||||
# - message_new
|
# - 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 { loggerConfigSchema } from "../service/logger/config";
|
||||||
import { dbConfigValidatior } from "../service/db/postgres/validation";
|
import { dbConfigValidatior } from "../service/db/postgres/validation";
|
||||||
|
|
||||||
const templateConfigSchema = object().shape({
|
export const templateConfigSchema = object().required().shape({
|
||||||
message_new: string().required(),
|
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({
|
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 { API, Updates, Upload } from "vk-io";
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { flatten, has, keys } from "ramda";
|
import { flatten, has, keys, prop } from "ramda";
|
||||||
import { NextFunction } from "connect";
|
import { NextFunction } from "connect";
|
||||||
import { VkEventHandler } from "./handlers/VkEventHandler";
|
import { VkEventHandler } from "./handlers/VkEventHandler";
|
||||||
import { vkEventToHandler } from "./handlers";
|
import { vkEventToHandler } from "./handlers";
|
||||||
|
@ -137,7 +137,11 @@ export class VkService {
|
||||||
return flatten(
|
return flatten(
|
||||||
group.channels.map((chan) =>
|
group.channels.map((chan) =>
|
||||||
chan.events.reduce((acc, event) => {
|
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](
|
const handler = new vkEventToHandler[event](
|
||||||
event,
|
event,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { API, Upload, Updates } from "vk-io";
|
import { API, Upload, Updates } from "vk-io";
|
||||||
import { WallPostType } from "vk-io/lib/api/schemas/objects";
|
import { WallPostType } from "vk-io/lib/api/schemas/objects";
|
||||||
|
import { TemplateConfig } from "../../config/types";
|
||||||
|
|
||||||
export interface VkConfig extends Record<string, any> {
|
export interface VkConfig extends Record<string, any> {
|
||||||
groups: ConfigGroup[];
|
groups: ConfigGroup[];
|
||||||
|
@ -13,12 +14,14 @@ export interface ConfigGroup {
|
||||||
secretKey: string;
|
secretKey: string;
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
channels: GroupChannel[];
|
channels: GroupChannel[];
|
||||||
|
templates: Partial<TemplateConfig>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GroupChannel {
|
export interface GroupChannel {
|
||||||
id: string;
|
id: string;
|
||||||
events: VkEvent[];
|
events: VkEvent[];
|
||||||
post_types: WallPostType[];
|
post_types: WallPostType[];
|
||||||
|
templates: Partial<TemplateConfig>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum VkEvent {
|
export enum VkEvent {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import * as yup from "yup";
|
import * as yup from "yup";
|
||||||
import { VkConfig, VkEvent } from "./types";
|
import { VkConfig, VkEvent } from "./types";
|
||||||
|
import { templateOptionalSchema } from "../../config/validate";
|
||||||
|
|
||||||
const vkChannelEventSchema = yup.string().oneOf(Object.values(VkEvent));
|
const vkChannelEventSchema = yup.string().oneOf(Object.values(VkEvent));
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ const vkChannelSchema = yup
|
||||||
.required()
|
.required()
|
||||||
.matches(/^@/, ({ path }) => `${path} should start with "@"`),
|
.matches(/^@/, ({ path }) => `${path} should start with "@"`),
|
||||||
events: yup.array().of(vkChannelEventSchema),
|
events: yup.array().of(vkChannelEventSchema),
|
||||||
|
templates: templateOptionalSchema,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const vkConfigSchema = yup
|
export const vkConfigSchema = yup
|
||||||
|
@ -30,6 +32,7 @@ export const vkConfigSchema = yup
|
||||||
secretKey: yup.string().required(),
|
secretKey: yup.string().required(),
|
||||||
apiKey: yup.string().required(),
|
apiKey: yup.string().required(),
|
||||||
channels: yup.array().of(vkChannelSchema),
|
channels: yup.array().of(vkChannelSchema),
|
||||||
|
templates: templateOptionalSchema,
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue