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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue