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

added help command

This commit is contained in:
Fedor Katurov 2023-12-30 18:09:02 +07:00
parent 6d3f511807
commit 621b03fb70
12 changed files with 91 additions and 9 deletions

View file

@ -5,6 +5,7 @@ import { Response } from "express";
import { InputMediaPhoto, Update } from "typegram";
import loggerTgMiddleware from "../logger/tg";
import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
import { Template } from "../template";
// import SocksProxyAgent from 'socks-proxy-agent';
@ -14,6 +15,9 @@ export class TelegramService {
public readonly bot: Telegraf;
public readonly webhook: WebhookConfig = {};
protected helpTemplate: Template<{}, {}> | undefined;
protected adminHelpTemplate: Template<{}, {}> | undefined;
constructor(private props: TelegramConfig) {
// const agent = (CONFIG.PROXY && new SocksProxyAgent(CONFIG.PROXY)) || null;
const options: Partial<Telegraf.Options<any>> = {
@ -29,6 +33,14 @@ export class TelegramService {
this.bot = new Telegraf(props.key, options);
this.bot.use(loggerTgMiddleware);
this.helpTemplate = props.templates?.help
? new Template(props.templates?.help)
: undefined;
this.adminHelpTemplate = props.templates?.help_admin
? new Template(props.templates?.help_admin)
: undefined;
process.once("SIGINT", () => this.stop("SIGINT"));
process.once("SIGTERM", () => this.stop("SIGTERM"));
}
@ -40,6 +52,18 @@ export class TelegramService {
* Connects to telegram
*/
public async start() {
if (!this.helpTemplate) {
console.warn(
"No help template specified, check templates.help in config"
);
}
if (!this.adminHelpTemplate) {
console.warn(
"No admin help template specified, check templates.help_admin in config"
);
}
if (this.isWebhookEnabled) {
await this.bot.telegram
.deleteWebhook()
@ -196,4 +220,16 @@ export class TelegramService {
);
}
});
public getHelpMessage = (forOwner?: boolean) => {
const template = forOwner
? this.adminHelpTemplate ?? this.helpTemplate
: this.helpTemplate;
if (!template) {
return;
}
return template?.theme({});
};
}

View file

@ -7,4 +7,8 @@ export interface TelegramConfig {
key: string;
owners?: string[];
webhook: WebhookConfig;
templates?: {
help?: string;
help_admin?: string;
};
}

View file

@ -16,10 +16,10 @@ const removeFrontmatter = () => (tree) => {
};
export class Template<
F extends Record<string, any>,
V extends Record<string, any>
Fields extends Record<string, any>,
Values extends Record<string, any>
> {
public fields: F = {} as F;
public fields: Fields = {} as Fields;
public template: string = "";
private readonly file: VFileCompatible = "";
@ -44,7 +44,7 @@ export class Template<
this.file = toVFile.readSync(path.join(__dirname, dir, filename));
const result = processor.processSync(this.file);
this.fields = result.data as F;
this.fields = result.data as Fields;
} catch (e) {
throw new Error(`Template: ${e?.toString()}`);
}
@ -54,7 +54,7 @@ export class Template<
* Themes the template with values, removes markdown from template.
* NOTE: text, that we'll insert into template, won't be used here
*/
public theme = (values: V, markdown?: boolean) => {
public theme = (values: Values, markdown?: boolean) => {
const processor = unified()
.use(stringify)
.use(frontmatter)