mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
#2 added buttons
This commit is contained in:
parent
8c17836868
commit
a59aae9c6e
4 changed files with 98 additions and 3 deletions
|
@ -1,6 +1,6 @@
|
|||
import extract from "remark-extract-frontmatter";
|
||||
import frontmatter from "remark-frontmatter";
|
||||
import compiler from "remark-stringify";
|
||||
import compiler from "retext-stringify";
|
||||
import parser from "remark-parse";
|
||||
import unified from "unified";
|
||||
import { parse } from "yaml";
|
||||
|
|
|
@ -4,14 +4,22 @@ 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";
|
||||
import { InlineKeyboardButton } from "typegram";
|
||||
import { keys } from "ramda";
|
||||
import { extractURLs } from "../../../utils/extract";
|
||||
|
||||
type Button = "links" | "likes";
|
||||
type UrlPrefix = string;
|
||||
type ExtraGenerator = (text: string) => InlineKeyboardButton[];
|
||||
|
||||
const defaultLikes = ["👎", "👍"];
|
||||
|
||||
interface Fields {
|
||||
image?: boolean;
|
||||
buttons?: ("buttons" | "likes")[];
|
||||
buttons?: Button[];
|
||||
link_text?: string;
|
||||
links: Record<UrlPrefix, string>;
|
||||
likes?: string[];
|
||||
}
|
||||
|
||||
interface Values {
|
||||
|
@ -43,9 +51,11 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
});
|
||||
|
||||
const extras: ExtraReplyMessage = {
|
||||
parse_mode: "Markdown",
|
||||
disable_web_page_preview: true,
|
||||
};
|
||||
|
||||
this.appendExtras(extras, text);
|
||||
|
||||
await this.telegram.sendMessageToChan(this.channel, parsed, extras);
|
||||
|
||||
await next();
|
||||
|
@ -57,4 +67,69 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
public static isValidPostType(type: string): boolean {
|
||||
return type === "post";
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates extras
|
||||
*/
|
||||
private appendExtras = (extras: ExtraReplyMessage, text: string) => {
|
||||
const { buttons } = this.template.fields;
|
||||
if (!buttons?.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const keyboard = buttons
|
||||
.map((button) => this.extrasGenerators[button](text))
|
||||
.filter((el) => el && el.length);
|
||||
|
||||
if (!keyboard.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
extras.reply_markup = {
|
||||
inline_keyboard: keyboard,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates link buttons for post
|
||||
*/
|
||||
private generateLinks: ExtraGenerator = (text) => {
|
||||
const links = this.template.fields.links;
|
||||
|
||||
if (!links) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const urls = extractURLs(text);
|
||||
|
||||
if (!urls) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return urls
|
||||
.map((url) => {
|
||||
const label = keys(links).find((link) =>
|
||||
url.toString().startsWith(link)
|
||||
);
|
||||
|
||||
return label ? { text: links[label], url: url.toString() } : undefined;
|
||||
})
|
||||
.filter((el) => el);
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates like button
|
||||
*/
|
||||
private generateLikes: ExtraGenerator = () => {
|
||||
const likes = this.template.fields.likes || defaultLikes;
|
||||
return likes.map((like, i) => ({
|
||||
text: like,
|
||||
callback_data: `/like ${like}`,
|
||||
}));
|
||||
};
|
||||
|
||||
private extrasGenerators: Record<Button, ExtraGenerator> = {
|
||||
links: this.generateLinks,
|
||||
likes: this.generateLikes,
|
||||
};
|
||||
}
|
||||
|
|
17
src/utils/extract.ts
Normal file
17
src/utils/extract.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { URL } from "url";
|
||||
|
||||
const urlRe = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gim;
|
||||
|
||||
export const extractURLs = (text: string): URL[] => {
|
||||
const matches = text.match(urlRe) || [];
|
||||
|
||||
return matches
|
||||
.map((m) => {
|
||||
try {
|
||||
return new URL(m);
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
})
|
||||
.filter((el) => el);
|
||||
};
|
|
@ -5,6 +5,9 @@
|
|||
links:
|
||||
https://map.vault48.org/: Посмотреть карту
|
||||
http://map.vault48.org/: Посмотреть карту
|
||||
https://vk.com/album-: Альбом поката
|
||||
http://vk.com/album-: Альбом поката
|
||||
likes: ['😱','🤔','😃']
|
||||
---
|
||||
{{!--
|
||||
use handlebars template here
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue