1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-25 23:16:41 +07:00

added vk events handler stub

This commit is contained in:
Fedor Katurov 2021-04-28 09:55:02 +07:00
parent dfae42c197
commit 3f908da91e
11 changed files with 125 additions and 67 deletions

View file

@ -1,17 +1,17 @@
import { TelegramConfig } from "./types";
import { TelegramConfig, WebhookConfig } from "./types";
import { Telegraf } from "telegraf";
import logger from "../logger";
import { Response } from "express";
import { Update } from "typegram";
import loggerTgMiddleware from "../logger/tg";
import { WebhookConfig } from "../../config/types";
// import SocksProxyAgent from 'socks-proxy-agent';
export class TelegramService {
public readonly bot: Telegraf;
public readonly webhook: WebhookConfig = {};
constructor(private props: TelegramConfig, private webhook: WebhookConfig) {
constructor(private props: TelegramConfig) {
// const agent = (CONFIG.PROXY && new SocksProxyAgent(CONFIG.PROXY)) || null;
const options: Partial<Telegraf.Options<any>> = {
telegram: {
@ -21,6 +21,8 @@ export class TelegramService {
},
};
this.webhook = props.webhook;
this.bot = new Telegraf(props.key, options);
this.bot.use(loggerTgMiddleware);

View file

@ -1,3 +1,9 @@
export interface WebhookConfig {
url?: string;
enabled?: boolean;
}
export interface TelegramConfig {
key: string;
webhook: WebhookConfig;
}

View file

@ -1,5 +1,12 @@
import * as yup from "yup";
import { boolean, object, string } from "yup";
const webhookValidationSchema = object().optional().shape({
url: string(),
enabled: boolean(),
});
export const telegramConfigSchema = yup.object().required().shape({
key: yup.string().required(),
webhook: webhookValidationSchema,
});

View file

@ -1,9 +1,20 @@
import { VkConfig } from './types';
import { VkConfig } from "./types";
export class VkService {
public endpoint: string = "/";
constructor(private config: VkConfig) {
if (!config.groups.length) {
throw new Error('No vk groups to handle. Specify them in config')
throw new Error("No vk groups to handle. Specify them in config");
}
this.endpoint = config.endpoint;
}
/**
* Handles incoming VK events
*/
public handle = async (event: any) => {
// TODO: handle events
};
}

View file

@ -1,26 +1,27 @@
export interface VkConfig extends Record<string, any> {
groups: ConfigGroup[]
groups: ConfigGroup[];
endpoint?: string;
}
interface ConfigGroup {
id: number
name: string
testResponse: string
secretKey: string
apiKey: string
channels: GroupChannel[]
id: number;
name: string;
testResponse: string;
secretKey: string;
apiKey: string;
channels: GroupChannel[];
}
interface GroupChannel {
id: string,
events: VkEvent[]
id: string;
events: VkEvent[];
}
export enum VkEvent {
Confirmation = 'confirmation',
WallPostNew = 'wall_post_new',
PostSuggestion = 'post_suggestion',
GroupJoin = 'group_join',
GroupLeave = 'group_leave',
MessageNew = 'message_new',
Confirmation = "confirmation",
WallPostNew = "wall_post_new",
PostSuggestion = "post_suggestion",
GroupJoin = "group_join",
GroupLeave = "group_leave",
MessageNew = "message_new",
}

View file

@ -1,20 +1,35 @@
import * as yup from 'yup'
import { VkConfig, VkEvent } from './types';
import * as yup from "yup";
import { VkConfig, VkEvent } from "./types";
const vkChannelEventSchema = yup.string().oneOf(Object.values(VkEvent))
const vkChannelEventSchema = yup.string().oneOf(Object.values(VkEvent));
const vkChannelSchema = yup.object().required().shape({
id: yup.string().required().matches(/^@/, ({ path }) => `${path} should start with "@"`),
events: yup.array().of(vkChannelEventSchema)
})
const vkChannelSchema = yup
.object()
.required()
.shape({
id: yup
.string()
.required()
.matches(/^@/, ({ path }) => `${path} should start with "@"`),
events: yup.array().of(vkChannelEventSchema),
});
export const vkConfigSchema = yup.object<VkConfig>().required().shape({
groups: yup.array().required().of(yup.object().shape({
id: yup.number().positive(),
name: yup.string().required(),
testResponse: yup.string().required(),
secretKey: yup.string().required(),
apiKey: yup.string().required(),
channels: yup.array().of(vkChannelSchema),
}))
})
export const vkConfigSchema = yup
.object<VkConfig>()
.required()
.shape({
endpoint: yup.string().optional(),
groups: yup
.array()
.required()
.of(
yup.object().shape({
id: yup.number().positive(),
name: yup.string().required(),
testResponse: yup.string().required(),
secretKey: yup.string().required(),
apiKey: yup.string().required(),
channels: yup.array().of(vkChannelSchema),
})
),
});