mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 15:06:41 +07:00
#1 added handlebars template engine
This commit is contained in:
parent
95ed64c640
commit
eb6a645ad3
10 changed files with 112 additions and 17 deletions
|
@ -2,9 +2,7 @@ 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}`
|
||||
);
|
||||
logger.debug(`received tg message`, ctx);
|
||||
await next().catch(logger.warn);
|
||||
};
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import logger from "../logger";
|
|||
import { Response } from "express";
|
||||
import { Update } from "typegram";
|
||||
import loggerTgMiddleware from "../logger/tg";
|
||||
import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
|
||||
|
||||
// import SocksProxyAgent from 'socks-proxy-agent';
|
||||
|
||||
|
@ -74,4 +75,16 @@ export class TelegramService {
|
|||
// TODO: test this.webhook.url with axios instead of 'true'
|
||||
return isWebhookEnabled && true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sends simple message to channel
|
||||
*/
|
||||
public sendMessageToChan = async (
|
||||
channel: string,
|
||||
message: string,
|
||||
extra?: ExtraReplyMessage
|
||||
) => {
|
||||
await this.bot.telegram.sendMessage(channel, message, extra);
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -6,9 +6,13 @@ import unified from "unified";
|
|||
import { parse } from "yaml";
|
||||
import toVFile from "to-vfile";
|
||||
import path from "path";
|
||||
import hb from "handlebars";
|
||||
|
||||
export class Template<T extends Record<string, any>> {
|
||||
public fields: T = {} as T;
|
||||
export class Template<
|
||||
F extends Record<string, any>,
|
||||
V extends Record<string, any>
|
||||
> {
|
||||
public fields: F = {} as F;
|
||||
public template: string = "";
|
||||
|
||||
constructor(filename: string) {
|
||||
|
@ -26,7 +30,7 @@ export class Template<T extends Record<string, any>> {
|
|||
const file = toVFile.readSync(path.join(__dirname, "../../", filename));
|
||||
const result = processor.processSync(file);
|
||||
|
||||
this.fields = result.data as T;
|
||||
this.fields = result.data as F;
|
||||
this.template = result
|
||||
.toString()
|
||||
.replace(/^---\n(.*)---\n?$/gms, "")
|
||||
|
@ -35,4 +39,8 @@ export class Template<T extends Record<string, any>> {
|
|||
throw new Error(`Template: ${e.toString()}`);
|
||||
}
|
||||
}
|
||||
|
||||
theme = (values: V) => {
|
||||
return hb.compile(this.template)(values);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -3,8 +3,18 @@ import { MessageContext } from "vk-io";
|
|||
import { NextMiddleware } from "middleware-io";
|
||||
import logger from "../../logger";
|
||||
import { ContextDefaultState } from "vk-io/lib/structures/contexts/context";
|
||||
import { UsersUserFull } from "vk-io/lib/api/schemas/objects";
|
||||
|
||||
export class MessageNewHandler extends VkEventHandler {
|
||||
interface Fields {
|
||||
buttons: string[];
|
||||
}
|
||||
|
||||
interface Values {
|
||||
user: UsersUserFull;
|
||||
text: string;
|
||||
}
|
||||
|
||||
export class MessageNewHandler extends VkEventHandler<Fields, Values> {
|
||||
public execute = async (
|
||||
context: MessageContext<ContextDefaultState>,
|
||||
next: NextMiddleware
|
||||
|
@ -23,8 +33,12 @@ export class MessageNewHandler extends VkEventHandler {
|
|||
`received message from ${from.first_name} ${from.last_name}: ${context.text}`
|
||||
);
|
||||
|
||||
const template = this.template.template;
|
||||
const fields = this.template.fields;
|
||||
const parsed = this.template.theme({
|
||||
user: from,
|
||||
text: context.text,
|
||||
});
|
||||
|
||||
await this.telegram.sendMessageToChan(this.channel, parsed).catch(next);
|
||||
|
||||
await next();
|
||||
};
|
||||
|
|
|
@ -4,14 +4,18 @@ import { VkService } from "../index";
|
|||
import { TelegramService } from "../../telegram";
|
||||
import { Template } from "../../template";
|
||||
|
||||
export class VkEventHandler {
|
||||
export class VkEventHandler<
|
||||
F extends Record<string, any> = any,
|
||||
V extends Record<string, any> = any
|
||||
> {
|
||||
public constructor(
|
||||
protected type: VkEvent,
|
||||
protected group: ConfigGroup,
|
||||
protected channel: string,
|
||||
protected instance: GroupInstance,
|
||||
protected vk: VkService,
|
||||
protected telegram: TelegramService,
|
||||
protected template: Template<any>
|
||||
protected template: Template<F, V>
|
||||
) {}
|
||||
|
||||
public execute: (
|
||||
|
|
|
@ -1,10 +1,22 @@
|
|||
import { VkEvent } from "../types";
|
||||
import { ConfigGroup, GroupInstance, VkEvent } from "../types";
|
||||
import { VkEventHandler } from "./VkEventHandler";
|
||||
import { MessageNewHandler } from "./MessageNewHandler";
|
||||
import { StubHandler } from "./StubHandler";
|
||||
import { VkService } from "../index";
|
||||
import { TelegramService } from "../../telegram";
|
||||
import { Template } from "../../template";
|
||||
|
||||
type DerivedHandler = typeof VkEventHandler;
|
||||
interface Handler extends DerivedHandler {}
|
||||
interface Handler {
|
||||
new (
|
||||
type: VkEvent,
|
||||
group: ConfigGroup,
|
||||
channel: string,
|
||||
instance: GroupInstance,
|
||||
vk: VkService,
|
||||
telegram: TelegramService,
|
||||
template: Template<any, any>
|
||||
): VkEventHandler;
|
||||
}
|
||||
|
||||
export const vkEventToHandler: Record<VkEvent, Handler> = {
|
||||
[VkEvent.GroupJoin]: StubHandler,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { ConfigGroup, GroupInstance, VkConfig, VkEvent } from "./types";
|
||||
import { API, Upload, Updates } from "vk-io";
|
||||
import { API, Updates, Upload } from "vk-io";
|
||||
import logger from "../logger";
|
||||
import { Request, Response } from "express";
|
||||
import { flatten, has, keys } from "ramda";
|
||||
|
@ -112,6 +112,7 @@ export class VkService {
|
|||
const handler = new vkEventToHandler[event](
|
||||
event,
|
||||
group,
|
||||
chan.id,
|
||||
instance,
|
||||
this,
|
||||
this.telegram,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue