mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 06:56:40 +07:00
added pop action for telegram
This commit is contained in:
parent
c1fb45742e
commit
c709bbf94c
9 changed files with 90 additions and 8 deletions
|
@ -1,12 +1,15 @@
|
|||
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) {}
|
||||
constructor(private telegram: TelegramService, private db: PostgresDB) {}
|
||||
|
||||
public listen() {
|
||||
this.telegram.bot.command("ping", TelegramApi.ping);
|
||||
this.telegram.bot.command("pop", this.pop);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -19,6 +22,30 @@ export class TelegramApi {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 { body, createdAt } = await this.db.popRequest();
|
||||
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();
|
||||
};
|
||||
|
||||
/**
|
||||
* Probes webhook url and falls back to polling mode on error
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue