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

added webhook parser

This commit is contained in:
Fedor Katurov 2021-04-26 12:25:07 +07:00
parent 9433cc327a
commit 87da9078fe
7 changed files with 32 additions and 20 deletions

View file

@ -2,7 +2,10 @@ http:
port: 3002
telegram:
key: ''
webhookUrl: http://localhost:3002/webhook
webhook:
path: /webhook
url: https://something.org:123123/webhook
enabled: false
logger:
level: info
#vk:

View file

@ -6,6 +6,7 @@ import loggerHttpMiddleware from "../../service/logger/http";
import logger from "../../service/logger";
import { TelegramService } from "../../service/telegram";
import http from "http";
import { WebhookConfig } from "../../config/types";
export class HttpApi {
app: Express;
@ -13,7 +14,8 @@ export class HttpApi {
constructor(
private props: HttpConfig,
private telegram: TelegramService,
private vk: VkService
private vk: VkService,
private webhook?: WebhookConfig
) {
this.app = express();
this.app.use(express.json());
@ -35,8 +37,8 @@ export class HttpApi {
this.app.use(bodyParser.json());
this.app.use(express.json());
if (props?.webhook?.enabled && props?.webhook?.url) {
logger.info(`using webhook at ${props.webhook.url}`);
if (this?.webhook?.enabled && this?.webhook?.path) {
logger.info(`using webhook at ${this.webhook.path}`);
this.app.post(props.webhook.url, this.handleWebhook);
}
}

View file

@ -1,7 +1,3 @@
export interface HttpConfig extends Record<string, any> {
port: number;
webhook?: {
url?: string;
enabled?: boolean;
};
}

View file

@ -1,11 +1,5 @@
import { boolean, number, object, string } from "yup";
import { number, object } from "yup";
export const httpConfigSchema = object()
.required()
.shape({
port: number().defined().required().positive(),
webhook: object().optional().shape({
url: string(),
enabled: boolean(),
}),
});
export const httpConfigSchema = object().required().shape({
port: number().defined().required().positive(),
});

View file

@ -3,9 +3,15 @@ import { VkConfig } from "../service/vk/types";
import { HttpConfig } from "../api/http/types";
import { LoggerConfig } from "../service/logger/types";
export interface WebhookConfig {
path?: string;
url?: string;
enabled?: boolean;
}
export interface Config extends Record<string, any> {
http: HttpConfig;
telegram: TelegramConfig;
vk: VkConfig;
logger?: LoggerConfig;
webhook?: WebhookConfig;
}

View file

@ -1,15 +1,21 @@
import { object } from "yup";
import { boolean, 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 webhookValidationSchema = object().optional().shape({
url: string(),
enabled: boolean(),
});
const configSchema = object<Config>().required().shape({
http: httpConfigSchema,
vk: vkConfigSchema,
telegram: telegramConfigSchema,
logger: loggerConfigSchema,
webhook: webhookValidationSchema,
});
export const validateConfig = (config: Config) =>

View file

@ -14,7 +14,12 @@ async function main() {
const telegramApi = new TelegramApi(telegram).listen();
await telegram.start();
const httpApi = new HttpApi(config.http, telegram, vkService).listen();
const httpApi = new HttpApi(
config.http,
telegram,
vkService,
config.webhook
).listen();
} catch (e) {
logger.error(e.message);
}