From a59aae9c6e2e58ee62dc43fab152aed8b940cb00 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Thu, 29 Apr 2021 17:43:47 +0700 Subject: [PATCH] #2 added buttons --- src/service/template/index.ts | 2 +- src/service/vk/handlers/PostNewHandler.ts | 79 ++++++++++++++++++++++- src/utils/extract.ts | 17 +++++ templates/post_new.md | 3 + 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 src/utils/extract.ts diff --git a/src/service/template/index.ts b/src/service/template/index.ts index 2c2339d..244f5f9 100644 --- a/src/service/template/index.ts +++ b/src/service/template/index.ts @@ -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"; diff --git a/src/service/vk/handlers/PostNewHandler.ts b/src/service/vk/handlers/PostNewHandler.ts index 05a9802..e179d8b 100644 --- a/src/service/vk/handlers/PostNewHandler.ts +++ b/src/service/vk/handlers/PostNewHandler.ts @@ -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; + likes?: string[]; } interface Values { @@ -43,9 +51,11 @@ export class PostNewHandler extends VkEventHandler { }); 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 { 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 = { + links: this.generateLinks, + likes: this.generateLikes, + }; } diff --git a/src/utils/extract.ts b/src/utils/extract.ts new file mode 100644 index 0000000..a8044fa --- /dev/null +++ b/src/utils/extract.ts @@ -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); +}; diff --git a/templates/post_new.md b/templates/post_new.md index 29791b9..a4df2df 100644 --- a/templates/post_new.md +++ b/templates/post_new.md @@ -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