mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 23:16:41 +07:00
#2 added PostNewHandler.ts
This commit is contained in:
parent
61f0440cb6
commit
8c17836868
10 changed files with 183 additions and 15 deletions
|
@ -84,6 +84,7 @@ export class TelegramService {
|
|||
message: string,
|
||||
extra?: ExtraReplyMessage
|
||||
) => {
|
||||
logger.debug(`sending message "${message}" to chan "${channel}"`);
|
||||
await this.bot.telegram.sendMessage(channel, message, extra);
|
||||
return;
|
||||
};
|
||||
|
|
|
@ -8,6 +8,10 @@ import toVFile from "to-vfile";
|
|||
import path from "path";
|
||||
import hb from "handlebars";
|
||||
|
||||
const removeFrontmatter = () => (tree) => {
|
||||
tree.children = tree.children.filter((item) => item.type !== "yaml");
|
||||
};
|
||||
|
||||
export class Template<
|
||||
F extends Record<string, any>,
|
||||
V extends Record<string, any>
|
||||
|
@ -22,19 +26,17 @@ export class Template<
|
|||
}
|
||||
|
||||
const processor = unified()
|
||||
.use(parser)
|
||||
.use(compiler)
|
||||
.use(frontmatter)
|
||||
.use(extract, { yaml: parse });
|
||||
.use(extract, { yaml: parse })
|
||||
.use(removeFrontmatter)
|
||||
.use(parser);
|
||||
|
||||
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();
|
||||
this.template = result.contents.toString().trim();
|
||||
} catch (e) {
|
||||
throw new Error(`Template: ${e.toString()}`);
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import { ConfigGroup } from "../types";
|
|||
import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
|
||||
|
||||
interface Fields {
|
||||
buttons?: string[];
|
||||
buttons?: "link"[];
|
||||
link_text?: string;
|
||||
}
|
||||
|
||||
|
@ -46,9 +46,7 @@ export class MessageNewHandler extends VkEventHandler<Fields, Values> {
|
|||
|
||||
this.appendButtons(extras, user.id);
|
||||
|
||||
await this.telegram
|
||||
.sendMessageToChan(this.channel, parsed, extras)
|
||||
.catch(next);
|
||||
await this.telegram.sendMessageToChan(this.channel, parsed, extras);
|
||||
|
||||
await next();
|
||||
};
|
||||
|
|
60
src/service/vk/handlers/PostNewHandler.ts
Normal file
60
src/service/vk/handlers/PostNewHandler.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import { VkEventHandler } from "./VkEventHandler";
|
||||
import { WallPostContext } from "vk-io";
|
||||
import { NextMiddleware } from "middleware-io";
|
||||
import { UsersUserFull } from "vk-io/lib/api/schemas/objects";
|
||||
import { ConfigGroup } from "../types";
|
||||
import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
|
||||
|
||||
type UrlPrefix = string;
|
||||
|
||||
interface Fields {
|
||||
image?: boolean;
|
||||
buttons?: ("buttons" | "likes")[];
|
||||
link_text?: string;
|
||||
links: Record<UrlPrefix, string>;
|
||||
}
|
||||
|
||||
interface Values {
|
||||
user?: UsersUserFull;
|
||||
group: ConfigGroup;
|
||||
text: string;
|
||||
}
|
||||
|
||||
export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
||||
public execute = async (context: WallPostContext, next: NextMiddleware) => {
|
||||
if (
|
||||
context.isRepost ||
|
||||
!PostNewHandler.isValidPostType(context?.wall?.postType)
|
||||
) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
const user = context.wall.signerId
|
||||
? await this.getUserByID(String(context.wall.signerId))
|
||||
: undefined;
|
||||
|
||||
const text = context.wall.text.trim();
|
||||
|
||||
const parsed = this.template.theme({
|
||||
user,
|
||||
group: this.group,
|
||||
text,
|
||||
});
|
||||
|
||||
const extras: ExtraReplyMessage = {
|
||||
parse_mode: "Markdown",
|
||||
};
|
||||
|
||||
await this.telegram.sendMessageToChan(this.channel, parsed, extras);
|
||||
|
||||
await next();
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if event of type we can handle
|
||||
*/
|
||||
public static isValidPostType(type: string): boolean {
|
||||
return type === "post";
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ import { StubHandler } from "./StubHandler";
|
|||
import { VkService } from "../index";
|
||||
import { TelegramService } from "../../telegram";
|
||||
import { Template } from "../../template";
|
||||
import { PostNewHandler } from "./PostNewHandler";
|
||||
|
||||
interface Handler {
|
||||
new (
|
||||
|
@ -22,6 +23,5 @@ export const vkEventToHandler: Record<VkEvent, Handler> = {
|
|||
[VkEvent.GroupJoin]: StubHandler,
|
||||
[VkEvent.GroupLeave]: StubHandler,
|
||||
[VkEvent.MessageNew]: MessageNewHandler,
|
||||
[VkEvent.PostSuggestion]: StubHandler,
|
||||
[VkEvent.WallPostNew]: StubHandler,
|
||||
[VkEvent.WallPostNew]: PostNewHandler,
|
||||
};
|
||||
|
|
|
@ -21,7 +21,6 @@ interface GroupChannel {
|
|||
|
||||
export enum VkEvent {
|
||||
WallPostNew = "wall_post_new",
|
||||
PostSuggestion = "post_suggestion",
|
||||
GroupJoin = "group_join",
|
||||
GroupLeave = "group_leave",
|
||||
MessageNew = "message_new",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue