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

added http and telegram api services

This commit is contained in:
Fedor Katurov 2021-04-26 12:11:41 +07:00
parent 5453e884c6
commit 9433cc327a
18 changed files with 687 additions and 70 deletions

View file

@ -0,0 +1,5 @@
import { object, string } from "yup";
export const loggerConfigSchema = object().shape({
level: string().optional().oneOf(["debug", "info", "warn", "error"]),
});

View file

@ -0,0 +1,19 @@
import morgan, { StreamOptions } from "morgan";
import logger from "./index";
const stream: StreamOptions = {
write: (message) => logger.http(message),
};
const skip = () => {
const env = process.env.NODE_ENV || "development";
return env !== "development";
};
// Build the morgan middleware
const loggerHttpMiddleware = morgan(
":method :url :status :res[content-length] - :response-time ms",
{ stream, skip }
);
export default loggerHttpMiddleware;

View file

@ -1,7 +1,13 @@
import { createLogger, format, transports } from 'winston';
import { createLogger, format, transports } from "winston";
import prepareConfig from "../../config";
const config = prepareConfig();
const logger = createLogger({
transports: new transports.Console({ format: format.simple() })
})
transports: new transports.Console({
format: format.simple(),
level: config.logger.level || "info",
}),
});
export default logger
export default logger;

11
src/service/logger/tg.ts Normal file
View file

@ -0,0 +1,11 @@
import { MiddlewareFn } from "telegraf";
import logger from "./index";
const loggerTgMiddleware: MiddlewareFn<any> = async (ctx, next) => {
logger.debug(
`received tg message from @${ctx.message.from.username}: ${ctx.message.text}`
);
await next().catch(logger.warn);
};
export default loggerTgMiddleware;

View file

@ -0,0 +1,3 @@
export interface LoggerConfig {
level?: "debug" | "info" | "warn" | "error";
}

View file

@ -1,17 +1,16 @@
import { TelegramConfig } from './types';
import { Telegraf } from 'telegraf';
import logger from '../logger';
import { TelegramConfig } from "./types";
import { Telegraf } from "telegraf";
import logger from "../logger";
import { Response } from "express";
import { Update } from "typegram";
import loggerTgMiddleware from "../logger/tg";
// import SocksProxyAgent from 'socks-proxy-agent';
export class TelegramService {
bot: Telegraf
public readonly bot: Telegraf;
constructor(private props: TelegramConfig) {
if (!props.key) {
throw new Error('Telegram api key not found. Get it from bot father')
}
// const agent = (CONFIG.PROXY && new SocksProxyAgent(CONFIG.PROXY)) || null;
const options = {
channelMode: true,
@ -22,18 +21,28 @@ export class TelegramService {
};
this.bot = new Telegraf(props.key, options);
this.bot.use(loggerTgMiddleware);
process.once('SIGINT', () => this.bot.stop('SIGINT'))
process.once('SIGTERM', () => this.bot.stop('SIGTERM'))
logger.info('Telegram service started')
process.once("SIGINT", () => this.bot.stop("SIGINT"));
process.once("SIGTERM", () => this.bot.stop("SIGTERM"));
}
start() {
if (!this.bot) {
throw new Error('Not initialized')
}
/**
* Connects to telegram
*/
public async start() {
await this.bot.telegram.deleteWebhook().then(
() => this.bot.launch(),
() => this.bot.launch()
);
return this.bot.launch()
logger.info("telegram service started");
}
/**
* Handles webhook updates
*/
public async handleUpdate(req: Update, res: Response) {
return this.bot.handleUpdate(req, res);
}
}