This repository has been archived on 2024-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
yandexgpt_tg_bot/message.mjs

29 lines
909 B
JavaScript
Raw Normal View History

2023-07-02 11:41:03 +03:00
import { option, readonlyArray } from "fp-ts";
import { flow, pipe } from "fp-ts/lib/function.js";
const RE_URL =
/(http|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])/;
// getMessageUrl :: Message -> Option<string>
const getMessageUrl = (msg) => option.fromNullable(RE_URL.exec(msg.text)?.[0]);
// getMessageEntities :: Message -> ReadonlyArray<MessageEntity>
const getMessageEntities = (msg) => msg.entities ?? readonlyArray.empty;
// isTextLink :: MessageEntity -> boolean
const isTextLink = (msgEntity) => msgEntity.type === "text_link";
// getTextLink :: Message -> Option<string>
const getTextLink = flow(
getMessageEntities,
readonlyArray.findFirstMap(
flow(option.fromPredicate(isTextLink), option.map((link) => link.url)),
),
);
export const extractMessageLink = (msg) =>
pipe(
getTextLink(msg),
option.orElse(() => getMessageUrl(msg)),
);