mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 23:16:41 +07:00
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/service/vk/handlers/MessageNewHandler.ts
This commit is contained in:
commit
eb696e64d4
15 changed files with 573 additions and 29 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;
|
||||
};
|
||||
}
|
||||
|
|
60
src/service/template/index.ts
Normal file
60
src/service/template/index.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import extract from "remark-extract-frontmatter";
|
||||
import frontmatter from "remark-frontmatter";
|
||||
import compiler from "remark-stringify";
|
||||
import parser from "remark-parse";
|
||||
import unified from "unified";
|
||||
import { parse } from "yaml";
|
||||
import toVFile from "to-vfile";
|
||||
import path from "path";
|
||||
import hb from "handlebars";
|
||||
|
||||
export class Template<
|
||||
F extends Record<string, any>,
|
||||
V extends Record<string, any>
|
||||
> {
|
||||
public fields: F = {} as F;
|
||||
public template: string = "";
|
||||
|
||||
constructor(filename: string) {
|
||||
try {
|
||||
if (!filename) {
|
||||
return;
|
||||
}
|
||||
|
||||
const processor = unified()
|
||||
.use(parser)
|
||||
.use(compiler)
|
||||
.use(frontmatter)
|
||||
.use(extract, { yaml: parse });
|
||||
|
||||
const file = toVFile.readSync(path.join(__dirname, "../../", filename));
|
||||
const result = processor.processSync(file);
|
||||
|
||||
this.fields = result.data as F;
|
||||
this.template = result
|
||||
.toString()
|
||||
.replace(/^---\n(.*)---\n?$/gms, "")
|
||||
.trim();
|
||||
} catch (e) {
|
||||
throw new Error(`Template: ${e.toString()}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Themes the tempalte with values
|
||||
*/
|
||||
public theme = (values: V) => {
|
||||
return hb.compile(this.template)(values);
|
||||
};
|
||||
|
||||
/**
|
||||
* Registers handlebars helpers
|
||||
*/
|
||||
public static registerHelpers() {
|
||||
hb.registerHelper("ifEq", function (arg1, arg2, options) {
|
||||
return arg1 == arg2 ? options.fn(this) : options.inverse(this);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Template.registerHelpers();
|
|
@ -3,8 +3,22 @@ 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";
|
||||
import { ConfigGroup } from "../types";
|
||||
import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
|
||||
|
||||
export class MessageNewHandler extends VkEventHandler {
|
||||
interface Fields {
|
||||
buttons?: string[];
|
||||
link_text?: string;
|
||||
}
|
||||
|
||||
interface Values {
|
||||
user: UsersUserFull;
|
||||
group: ConfigGroup;
|
||||
text: string;
|
||||
}
|
||||
|
||||
export class MessageNewHandler extends VkEventHandler<Fields, Values> {
|
||||
public execute = async (
|
||||
context: MessageContext<ContextDefaultState>,
|
||||
next: NextMiddleware
|
||||
|
@ -14,15 +28,44 @@ export class MessageNewHandler extends VkEventHandler {
|
|||
return;
|
||||
}
|
||||
|
||||
const users = await this.instance.api.users.get({
|
||||
user_ids: [String(context.senderId)],
|
||||
});
|
||||
const from = users[0];
|
||||
const user = await this.getUserByID(String(context.senderId));
|
||||
|
||||
logger.debug(
|
||||
`vk, group ${this.group.name} received message from ${from.first_name} ${from.last_name}: ${context.text}`
|
||||
);
|
||||
|
||||
const parsed = this.template.theme({
|
||||
user,
|
||||
group: this.group,
|
||||
text: context.text,
|
||||
});
|
||||
|
||||
const extras: ExtraReplyMessage = {
|
||||
parse_mode: "Markdown",
|
||||
};
|
||||
|
||||
this.appendButtons(extras, user.id);
|
||||
|
||||
await this.telegram
|
||||
.sendMessageToChan(this.channel, parsed, extras)
|
||||
.catch(next);
|
||||
|
||||
await next();
|
||||
};
|
||||
|
||||
/**
|
||||
* Appending buttons (if needed) by mutating original extras
|
||||
*/
|
||||
private appendButtons = (extras: ExtraReplyMessage, userId: number) => {
|
||||
if (!this.template?.fields?.buttons?.includes("link")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const text = this.template?.fields?.link_text || "View dialog";
|
||||
const url = this.makeDialogUrl(this.group.id, userId);
|
||||
|
||||
extras.reply_markup = {
|
||||
inline_keyboard: [[{ text, url }]],
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
13
src/service/vk/handlers/StubHandler.ts
Normal file
13
src/service/vk/handlers/StubHandler.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { VkEventHandler } from "./VkEventHandler";
|
||||
import { NextMiddleware } from "middleware-io";
|
||||
import logger from "../../logger";
|
||||
|
||||
/**
|
||||
* StubHandler is used to stub event calls
|
||||
*/
|
||||
export class StubHandler extends VkEventHandler {
|
||||
public execute = async (context: any, next: NextMiddleware) => {
|
||||
logger.debug(`received unhandled message of type "${context.type}"`);
|
||||
await next();
|
||||
};
|
||||
}
|
|
@ -1,14 +1,21 @@
|
|||
import { NextMiddleware } from "middleware-io";
|
||||
import { ConfigGroup, GroupInstance } from "../types";
|
||||
import { ConfigGroup, GroupInstance, VkEvent } from "../types";
|
||||
import { VkService } from "../index";
|
||||
import { TelegramService } from "../../telegram";
|
||||
import { Template } from "../../template";
|
||||
|
||||
export abstract 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 telegram: TelegramService,
|
||||
protected template: Template<F, V>
|
||||
) {}
|
||||
|
||||
public execute: (
|
||||
|
@ -18,4 +25,23 @@ export abstract class VkEventHandler {
|
|||
console.log(`vk received unknown event`, ctx);
|
||||
await next();
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches user by id
|
||||
* @param id
|
||||
*/
|
||||
protected getUserByID = async (id: string) => {
|
||||
const users = await this.instance.api.users.get({
|
||||
user_ids: [id],
|
||||
fields: ["sex"],
|
||||
});
|
||||
|
||||
return users[0];
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns url for group dialog
|
||||
*/
|
||||
protected makeDialogUrl = (groupId: number, userId: number): string =>
|
||||
`https://vk.com/gim${groupId}?sel=${userId}`;
|
||||
}
|
||||
|
|
|
@ -1,14 +1,27 @@
|
|||
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]: MessageNewHandler,
|
||||
[VkEvent.GroupLeave]: MessageNewHandler,
|
||||
[VkEvent.GroupJoin]: StubHandler,
|
||||
[VkEvent.GroupLeave]: StubHandler,
|
||||
[VkEvent.MessageNew]: MessageNewHandler,
|
||||
[VkEvent.PostSuggestion]: MessageNewHandler,
|
||||
[VkEvent.WallPostNew]: MessageNewHandler,
|
||||
[VkEvent.PostSuggestion]: StubHandler,
|
||||
[VkEvent.WallPostNew]: 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";
|
||||
|
@ -7,6 +7,8 @@ import { NextFunction } from "connect";
|
|||
import { VkEventHandler } from "./handlers/VkEventHandler";
|
||||
import { vkEventToHandler } from "./handlers";
|
||||
import { TelegramService } from "../telegram";
|
||||
import { Template } from "../template";
|
||||
import { TemplateConfig } from "../../config/types";
|
||||
|
||||
/**
|
||||
* Service to handle VK to Telegram interactions
|
||||
|
@ -16,7 +18,11 @@ export class VkService {
|
|||
private readonly instances: Record<string, GroupInstance>;
|
||||
private readonly groups: Record<number, ConfigGroup>;
|
||||
|
||||
constructor(private config: VkConfig, private telegram: TelegramService) {
|
||||
constructor(
|
||||
private config: VkConfig,
|
||||
private telegram: TelegramService,
|
||||
private templates: TemplateConfig
|
||||
) {
|
||||
if (!config.groups.length) {
|
||||
throw new Error("No vk groups to handle. Specify them in config");
|
||||
}
|
||||
|
@ -107,11 +113,16 @@ export class VkService {
|
|||
return flatten(
|
||||
group.channels.map((chan) =>
|
||||
chan.events.reduce((acc, event) => {
|
||||
const handler = new (vkEventToHandler as any)[event](
|
||||
const template = new Template(this.templates[event]);
|
||||
|
||||
const handler = new vkEventToHandler[event](
|
||||
event,
|
||||
group,
|
||||
chan.id,
|
||||
instance,
|
||||
this,
|
||||
this.telegram
|
||||
this.telegram,
|
||||
template
|
||||
);
|
||||
return { ...acc, [event]: handler };
|
||||
}, {} as Record<VkEvent, VkEventHandler>[])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue