Skip to content

Commit

Permalink
fix(api): reply to media message for long text TG messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ascariandrea committed Oct 30, 2023
1 parent b2e6e11 commit 79f5160
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
9 changes: 5 additions & 4 deletions packages/@liexp/backend/src/providers/tg/tg.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export interface TGBotProvider {
upsertPinnedMessage: (
text: string,
) => TE.TaskEither<Error, TelegramBot.Message>;
post: (text: string) => TE.TaskEither<Error, any>;
post: (text: string, replyToMessageId?: number) => TE.TaskEither<Error, TelegramBot.Message>;
postPhoto: (
imageUrl: string | Stream,
caption: string,
) => TE.TaskEither<Error, any>;
) => TE.TaskEither<Error, TelegramBot.Message>;
postVideo: (
videoUrl: string | Stream,
caption: string,
opts?: TelegramBot.SendVideoOptions,
) => TE.TaskEither<Error, any>;
) => TE.TaskEither<Error, TelegramBot.Message>;
postMediaGroup: (
text: string,
media: readonly TelegramBot.InputMedia[],
Expand Down Expand Up @@ -99,9 +99,10 @@ export const TGBotProvider = (
}),
);
},
post: (text) => {
post: (text, replyToMessageId) => {
return liftTGTE(() =>
api.sendMessage(opts.chat, text, {
reply_to_message_id: replyToMessageId,
parse_mode: "HTML",
disable_web_page_preview: false,
}),
Expand Down
37 changes: 31 additions & 6 deletions services/api/src/flows/events/postToTG.flow.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { type Stream } from "stream";
import { fp, pipe } from "@liexp/core/lib/fp";
import {
type SocialPostBodyMultipleMedia,
SocialPostPhoto,
SocialPostVideo,
type CreateSocialPost,
type SocialPostBodyMultipleMedia,
} from "@liexp/shared/lib/io/http/SocialPost";
import * as t from "io-ts";
import { type UUID } from "io-ts-types/lib/UUID";
import { type EventV2Entity } from "@entities/Event.v2.entity";
import type TelegramBot from "node-telegram-bot-api";
import { type Flow, type TEFlow } from "@flows/flow.types";
import { ServerError } from "@io/ControllerError";

Expand Down Expand Up @@ -64,7 +64,25 @@ const writeText: Flow<[CreateSocialPost], string> = (ctx) => (body) => {
].join("\n");
};

export const postToTG: TEFlow<[UUID, CreateSocialPost], EventV2Entity> =
const getMessageTexts = (
post: CreateSocialPost,
text: string
): { mediaText: string; messageText: string; useReply: boolean } => {
if (text.length > 300) {
return {
mediaText: post.title,
messageText: text,
useReply: true,
};
}
return {
mediaText: text,
messageText: text,
useReply: false,
};
};

export const postToTG: TEFlow<[UUID, CreateSocialPost], TelegramBot.Message> =
(ctx) => (id, body) => {
return pipe(
writeText(ctx)(body),
Expand All @@ -75,30 +93,37 @@ export const postToTG: TEFlow<[UUID, CreateSocialPost], EventV2Entity> =
? [{ type: "photo", media: body.media }]
: body.media;

const { mediaText, messageText, useReply } = getMessageTexts(body, text);

return pipe(
media,
fp.TE.right,
fp.TE.chain((media) => {
if (media.length === 1) {
const m = media[0];
if (SocialPostPhoto.is(m)) {
return ctx.tg.postPhoto(m.media, text);
return ctx.tg.postPhoto(m.media, mediaText);
}
if (SocialPostVideo.is(m)) {
return pipe(
ctx.http.get<Stream>(m.media, {
responseType: "stream",
}),
fp.TE.chain((stream) =>
ctx.tg.postVideo(stream, text, {
ctx.tg.postVideo(stream, mediaText, {
duration: m.duration,
}),
),
);
}
}
return ctx.tg.postMediaGroup(text, media);
return ctx.tg.postMediaGroup(mediaText, media);
}),
fp.TE.chain((message) =>
useReply
? ctx.tg.post(messageText, message.message_id)
: fp.TE.right(message),
),
);
}),
fp.TE.mapLeft((e) => ServerError([e.message])),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ export const MakeCreateSocialPostRoute: Route = (r, ctx) => {
? postToTG(ctx)(id, { ...p.content, platforms })
: TE.right(undefined),
}),
ctx.logger.info.logInTaskEither(
`Posting ${id} with caption %O`,
),
TE.chain((result) =>
ctx.db.save(SocialPostEntity, [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const MakePublishSocialPostRoute: Route = (r, ctx) => {
},
}),
TE.chain((p) => postToTG(ctx)(id, { ...p.content })),
ctx.logger.info.logInTaskEither(`Posting ${id} with caption %O`),
ctx.logger.info.logInTaskEither((r) => [`Posting ${id} with caption %O`, r.text]),
TE.chain((result) =>
ctx.db.save(SocialPostEntity, [
{
Expand Down

0 comments on commit 79f5160

Please sign in to comment.