1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-24 22:46:41 +07:00
vk-tg-bot/src/api/telegram/index.ts
2021-05-28 15:20:08 +07:00

100 lines
2.4 KiB
TypeScript

import { TelegramService } from "../../service/telegram";
import axios from "axios";
import logger from "../../service/logger";
import { PostgresDB } from "../../service/db/postgres";
import { Readable } from "stream";
export class TelegramApi {
constructor(
private telegram: TelegramService,
private db: PostgresDB,
private config: Record<any, any>
) {}
public listen() {
this.telegram.bot.command("ping", TelegramApi.ping);
this.telegram.bot.command("config", this.dumpConfig);
this.telegram.bot.command("pop", this.pop);
return;
}
/**
* Handles ping command
*/
private static ping(ctx) {
return ctx.replyWithSticker(
"CAACAgIAAxkBAAIB6F82KSeJBEFer895bb7mFI7_GzYoAAISAAOwODIrOXeFNb5v4aEaBA"
);
}
/**
* Pops last recorded request from vk
*/
private pop = async (ctx, next) => {
const username = ctx?.update?.message?.from?.username;
if (!username || !this.telegram.isOwner(`@${username}`)) {
return;
}
const request = await this.db.popRequest();
if (!request) {
await ctx.reply(`sorry, no logged requests yet`);
return next();
}
const { body, createdAt } = request;
const source = JSON.stringify(body, null, 2);
await ctx.replyWithDocument(
{
source: Readable.from(source),
filename: `debug-${createdAt.toISOString()}.txt`,
},
{ caption: `recorded at: ${createdAt.toLocaleString()}` }
);
return next();
};
/**
* Pops last recorded request from vk
*/
private dumpConfig = async (ctx, next) => {
const username = ctx?.update?.message?.from?.username;
if (!username || !this.telegram.isOwner(`@${username}`)) {
return;
}
const source = JSON.stringify(this.config, null, 2);
await ctx.replyWithDocument({
source: Readable.from(source),
filename: `config.txt`,
});
return next();
};
/**
* Probes webhook url and falls back to polling mode on error
*/
public probe = async () => {
if (!this.telegram.isWebhookEnabled) {
return;
}
try {
await axios.get(this.telegram.webhook.url!);
logger.info(
`probing telegram webhook at ${this.telegram.webhook.url}: ok`
);
} catch (e) {
logger.warn(
`probing telegram webhook at ${this.telegram.webhook.url}: FAILED, falling back to polling mode`
);
await this.telegram.bot.launch();
}
};
}