message: use longer link to describe an article
This commit is contained in:
parent
b74e24da32
commit
12ca31135f
2 changed files with 26 additions and 8 deletions
23
main.mjs
23
main.mjs
|
@ -5,6 +5,7 @@ import {
|
||||||
readonlyArray,
|
readonlyArray,
|
||||||
semigroup,
|
semigroup,
|
||||||
string,
|
string,
|
||||||
|
taskEither,
|
||||||
} from "fp-ts";
|
} from "fp-ts";
|
||||||
import { flow, pipe } from "fp-ts/lib/function.js";
|
import { flow, pipe } from "fp-ts/lib/function.js";
|
||||||
import { describeArticle } from "./api.mjs";
|
import { describeArticle } from "./api.mjs";
|
||||||
|
@ -35,13 +36,23 @@ bot.on("channel_post", (msg) => {
|
||||||
string.trimLeft,
|
string.trimLeft,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
rte.match(
|
rte.flatMapTaskEither(
|
||||||
(err) => console.log({ err }),
|
|
||||||
(res) =>
|
(res) =>
|
||||||
bot.editMessageText(msg.text + "\n\nYandexGPT:\n\n" + res, {
|
taskEither.tryCatch(
|
||||||
chat_id: msg.chat.id,
|
() =>
|
||||||
message_id: msg.message_id,
|
bot.editMessageText(
|
||||||
}),
|
msg.text + "\n\nYandexGpt:\n\n" + res,
|
||||||
|
{
|
||||||
|
chat_id: (msg.forward_from_chat ?? msg.chat).id,
|
||||||
|
message_id: msg.forward_from_message_id ?? msg.message_id,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
String,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
rte.match(
|
||||||
|
(err) => console.log({ err, msg }),
|
||||||
|
(res) => console.log("done", msg.message_id),
|
||||||
),
|
),
|
||||||
)(config)();
|
)(config)();
|
||||||
});
|
});
|
||||||
|
|
11
message.mjs
11
message.mjs
|
@ -1,4 +1,4 @@
|
||||||
import { option, readonlyArray } from "fp-ts";
|
import { number, option, ord, readonlyArray } from "fp-ts";
|
||||||
import { flow, pipe } from "fp-ts/lib/function.js";
|
import { flow, pipe } from "fp-ts/lib/function.js";
|
||||||
|
|
||||||
const RE_URL =
|
const RE_URL =
|
||||||
|
@ -13,12 +13,19 @@ const getMessageEntities = (msg) => msg.entities ?? readonlyArray.empty;
|
||||||
// isTextLink :: MessageEntity -> boolean
|
// isTextLink :: MessageEntity -> boolean
|
||||||
const isTextLink = (msgEntity) => msgEntity.type === "text_link";
|
const isTextLink = (msgEntity) => msgEntity.type === "text_link";
|
||||||
|
|
||||||
|
const ordStringLength = pipe(
|
||||||
|
number.Ord,
|
||||||
|
ord.contramap((s) => s.length),
|
||||||
|
);
|
||||||
|
|
||||||
// getTextLink :: Message -> Option<string>
|
// getTextLink :: Message -> Option<string>
|
||||||
const getTextLink = flow(
|
const getTextLink = flow(
|
||||||
getMessageEntities,
|
getMessageEntities,
|
||||||
readonlyArray.findFirstMap(
|
readonlyArray.filterMap(
|
||||||
flow(option.fromPredicate(isTextLink), option.map((link) => link.url)),
|
flow(option.fromPredicate(isTextLink), option.map((link) => link.url)),
|
||||||
),
|
),
|
||||||
|
readonlyArray.sort(ordStringLength),
|
||||||
|
readonlyArray.last,
|
||||||
);
|
);
|
||||||
|
|
||||||
export const extractMessageLink = (msg) =>
|
export const extractMessageLink = (msg) =>
|
||||||
|
|
Reference in a new issue