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:
parent
9433cc327a
commit
87da9078fe
7 changed files with 32 additions and 20 deletions
|
@ -2,7 +2,10 @@ http:
|
||||||
port: 3002
|
port: 3002
|
||||||
telegram:
|
telegram:
|
||||||
key: ''
|
key: ''
|
||||||
webhookUrl: http://localhost:3002/webhook
|
webhook:
|
||||||
|
path: /webhook
|
||||||
|
url: https://something.org:123123/webhook
|
||||||
|
enabled: false
|
||||||
logger:
|
logger:
|
||||||
level: info
|
level: info
|
||||||
#vk:
|
#vk:
|
||||||
|
|
|
@ -6,6 +6,7 @@ import loggerHttpMiddleware from "../../service/logger/http";
|
||||||
import logger from "../../service/logger";
|
import logger from "../../service/logger";
|
||||||
import { TelegramService } from "../../service/telegram";
|
import { TelegramService } from "../../service/telegram";
|
||||||
import http from "http";
|
import http from "http";
|
||||||
|
import { WebhookConfig } from "../../config/types";
|
||||||
|
|
||||||
export class HttpApi {
|
export class HttpApi {
|
||||||
app: Express;
|
app: Express;
|
||||||
|
@ -13,7 +14,8 @@ export class HttpApi {
|
||||||
constructor(
|
constructor(
|
||||||
private props: HttpConfig,
|
private props: HttpConfig,
|
||||||
private telegram: TelegramService,
|
private telegram: TelegramService,
|
||||||
private vk: VkService
|
private vk: VkService,
|
||||||
|
private webhook?: WebhookConfig
|
||||||
) {
|
) {
|
||||||
this.app = express();
|
this.app = express();
|
||||||
this.app.use(express.json());
|
this.app.use(express.json());
|
||||||
|
@ -35,8 +37,8 @@ export class HttpApi {
|
||||||
this.app.use(bodyParser.json());
|
this.app.use(bodyParser.json());
|
||||||
this.app.use(express.json());
|
this.app.use(express.json());
|
||||||
|
|
||||||
if (props?.webhook?.enabled && props?.webhook?.url) {
|
if (this?.webhook?.enabled && this?.webhook?.path) {
|
||||||
logger.info(`using webhook at ${props.webhook.url}`);
|
logger.info(`using webhook at ${this.webhook.path}`);
|
||||||
this.app.post(props.webhook.url, this.handleWebhook);
|
this.app.post(props.webhook.url, this.handleWebhook);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
export interface HttpConfig extends Record<string, any> {
|
export interface HttpConfig extends Record<string, any> {
|
||||||
port: number;
|
port: number;
|
||||||
webhook?: {
|
|
||||||
url?: string;
|
|
||||||
enabled?: boolean;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
import { boolean, number, object, string } from "yup";
|
import { number, object } from "yup";
|
||||||
|
|
||||||
export const httpConfigSchema = object()
|
export const httpConfigSchema = object().required().shape({
|
||||||
.required()
|
port: number().defined().required().positive(),
|
||||||
.shape({
|
});
|
||||||
port: number().defined().required().positive(),
|
|
||||||
webhook: object().optional().shape({
|
|
||||||
url: string(),
|
|
||||||
enabled: boolean(),
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
|
@ -3,9 +3,15 @@ import { VkConfig } from "../service/vk/types";
|
||||||
import { HttpConfig } from "../api/http/types";
|
import { HttpConfig } from "../api/http/types";
|
||||||
import { LoggerConfig } from "../service/logger/types";
|
import { LoggerConfig } from "../service/logger/types";
|
||||||
|
|
||||||
|
export interface WebhookConfig {
|
||||||
|
path?: string;
|
||||||
|
url?: string;
|
||||||
|
enabled?: boolean;
|
||||||
|
}
|
||||||
export interface Config extends Record<string, any> {
|
export interface Config extends Record<string, any> {
|
||||||
http: HttpConfig;
|
http: HttpConfig;
|
||||||
telegram: TelegramConfig;
|
telegram: TelegramConfig;
|
||||||
vk: VkConfig;
|
vk: VkConfig;
|
||||||
logger?: LoggerConfig;
|
logger?: LoggerConfig;
|
||||||
|
webhook?: WebhookConfig;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,21 @@
|
||||||
import { object } from "yup";
|
import { boolean, object, string } from "yup";
|
||||||
import { httpConfigSchema } from "../api/http/validation";
|
import { httpConfigSchema } from "../api/http/validation";
|
||||||
import { Config } from "./types";
|
import { Config } from "./types";
|
||||||
import { vkConfigSchema } from "../service/vk/validation";
|
import { vkConfigSchema } from "../service/vk/validation";
|
||||||
import { telegramConfigSchema } from "../service/telegram/validation";
|
import { telegramConfigSchema } from "../service/telegram/validation";
|
||||||
import { loggerConfigSchema } from "../service/logger/config";
|
import { loggerConfigSchema } from "../service/logger/config";
|
||||||
|
|
||||||
|
const webhookValidationSchema = object().optional().shape({
|
||||||
|
url: string(),
|
||||||
|
enabled: boolean(),
|
||||||
|
});
|
||||||
|
|
||||||
const configSchema = object<Config>().required().shape({
|
const configSchema = object<Config>().required().shape({
|
||||||
http: httpConfigSchema,
|
http: httpConfigSchema,
|
||||||
vk: vkConfigSchema,
|
vk: vkConfigSchema,
|
||||||
telegram: telegramConfigSchema,
|
telegram: telegramConfigSchema,
|
||||||
logger: loggerConfigSchema,
|
logger: loggerConfigSchema,
|
||||||
|
webhook: webhookValidationSchema,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const validateConfig = (config: Config) =>
|
export const validateConfig = (config: Config) =>
|
||||||
|
|
|
@ -14,7 +14,12 @@ async function main() {
|
||||||
const telegramApi = new TelegramApi(telegram).listen();
|
const telegramApi = new TelegramApi(telegram).listen();
|
||||||
await telegram.start();
|
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) {
|
} catch (e) {
|
||||||
logger.error(e.message);
|
logger.error(e.message);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue