mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 06:56:40 +07:00
#2 added PostNewHandler.ts
This commit is contained in:
parent
61f0440cb6
commit
8c17836868
10 changed files with 183 additions and 15 deletions
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";
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue