mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 15:06:41 +07:00
added http and telegram api services
This commit is contained in:
parent
5453e884c6
commit
9433cc327a
18 changed files with 687 additions and 70 deletions
|
@ -0,0 +1,59 @@
|
|||
import { HttpConfig } from "./types";
|
||||
import { VkService } from "../../service/vk";
|
||||
import express, { Express, Request, Response } from "express";
|
||||
import bodyParser from "body-parser";
|
||||
import loggerHttpMiddleware from "../../service/logger/http";
|
||||
import logger from "../../service/logger";
|
||||
import { TelegramService } from "../../service/telegram";
|
||||
import http from "http";
|
||||
|
||||
export class HttpApi {
|
||||
app: Express;
|
||||
|
||||
constructor(
|
||||
private props: HttpConfig,
|
||||
private telegram: TelegramService,
|
||||
private vk: VkService
|
||||
) {
|
||||
this.app = express();
|
||||
this.app.use(express.json());
|
||||
this.app.use(express.urlencoded({ extended: false }));
|
||||
this.app.use((req, res, next) => {
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header(
|
||||
"Access-Control-Allow-Methods",
|
||||
"GET, PUT, POST, DELETE, PATCH"
|
||||
);
|
||||
res.header(
|
||||
"Access-Control-Allow-Headers",
|
||||
"Origin, X-Requested-With, Content-Type, Accept"
|
||||
);
|
||||
next();
|
||||
});
|
||||
this.app.use(loggerHttpMiddleware);
|
||||
|
||||
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}`);
|
||||
this.app.post(props.webhook.url, this.handleWebhook);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts http server
|
||||
*/
|
||||
public async listen() {
|
||||
const httpServer = http.createServer(this.app);
|
||||
httpServer.listen(this.props.port);
|
||||
logger.info(`http api listening at ${this.props.port}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles telegram webhooks
|
||||
*/
|
||||
private async handleWebhook(req: Request, res: Response) {
|
||||
return this.telegram.handleUpdate(req.body, res);
|
||||
}
|
||||
}
|
|
@ -1,3 +1,7 @@
|
|||
export interface HttpConfig extends Record<string, any> {
|
||||
port: number
|
||||
port: number;
|
||||
webhook?: {
|
||||
url?: string;
|
||||
enabled?: boolean;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
import { number, object } from 'yup'
|
||||
import { boolean, number, object, string } from "yup";
|
||||
|
||||
export const httpConfigSchema = object().required().shape({
|
||||
port: number().defined().required().positive(),
|
||||
})
|
||||
export const httpConfigSchema = object()
|
||||
.required()
|
||||
.shape({
|
||||
port: number().defined().required().positive(),
|
||||
webhook: object().optional().shape({
|
||||
url: string(),
|
||||
enabled: boolean(),
|
||||
}),
|
||||
});
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
import { TelegramService } from "../../service/telegram";
|
||||
import logger from "../../service/logger";
|
||||
|
||||
export class TelegramApi {
|
||||
constructor(private telegram: TelegramService) {}
|
||||
|
||||
public listen() {
|
||||
this.telegram.bot.command("ping", TelegramApi.ping);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles ping command
|
||||
*/
|
||||
private static ping(ctx) {
|
||||
return ctx.replyWithSticker(
|
||||
"CAACAgIAAxkBAAIB6F82KSeJBEFer895bb7mFI7_GzYoAAISAAOwODIrOXeFNb5v4aEaBA"
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue