Skip to content

Commit

Permalink
fix(api): post file media on tg
Browse files Browse the repository at this point in the history
  • Loading branch information
ascariandrea committed Dec 2, 2023
1 parent 71b75fb commit 94935bf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
11 changes: 10 additions & 1 deletion packages/@liexp/backend/src/providers/tg/tg.provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Stream } from "stream";
import { type Logger } from "@liexp/core/lib/logger";
import { MP4Type } from "@liexp/shared/lib/io/http/Media";
import { MP4Type, PDFType } from "@liexp/shared/lib/io/http/Media";
import * as TE from "fp-ts/TaskEither";
import { pipe } from "fp-ts/function";
import TelegramBot from "node-telegram-bot-api";
Expand All @@ -27,6 +27,7 @@ export interface TGBotProvider {
text: string,
media: readonly TelegramBot.InputMedia[],
) => TE.TaskEither<Error, TelegramBot.Message>;
postFile: (text: string, fileName: string, file: string | Stream | Buffer, contentType?: PDFType) => TE.TaskEither<Error, TelegramBot.Message>;
onMessage: (
f: (message: TelegramBot.Message, metadata: TelegramBot.Metadata) => void,
) => void;
Expand Down Expand Up @@ -154,6 +155,14 @@ export const TGBotProvider = (
),
);
},
postFile(text, filename, file, contentType = PDFType.value) {
return liftTGTE(() =>
api.sendDocument(opts.chat, file, {
caption: text,
parse_mode: "HTML",
}, { filename, contentType }),
);
},
onMessage: (f) => {
api.on("message", f);
},
Expand Down
15 changes: 14 additions & 1 deletion packages/@liexp/shared/src/io/http/SocialPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ export const SocialPostPhoto = t.type(
},
"SocialPostPhoto",
);
export type SocialPostPhoto = t.TypeOf<typeof SocialPostPhoto>;

export const SocialPostDocument = t.type(
{
type: t.literal("document"),
filename: t.string,
media: t.string,
thumbnail: t.string,
},
"SocialPostPhoto",
);
export type SocialPostDocument = t.TypeOf<typeof SocialPostDocument>;

export const SocialPostVideo = t.type(
{
Expand All @@ -30,9 +42,10 @@ export const SocialPostVideo = t.type(
},
"SocialPostVideo",
);
export type SocialPostVideo = t.TypeOf<typeof SocialPostVideo>;

export const SocialPostBodyMultipleMedia = t.array(
t.union([SocialPostPhoto, SocialPostVideo]),
t.union([SocialPostPhoto, SocialPostVideo, SocialPostDocument]),
"SocialPostBodyMultipleMedia",
);
export type SocialPostBodyMultipleMedia = t.TypeOf<
Expand Down
23 changes: 22 additions & 1 deletion services/api/src/flows/social-posts/postToTG.flow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { type Stream } from "stream";
import { fp, pipe } from "@liexp/core/lib/fp";
import { PDFType } from "@liexp/shared/lib/io/http/Media";
import {
SocialPostDocument,
SocialPostPhoto,
SocialPostVideo,
type CreateSocialPost,
Expand Down Expand Up @@ -112,6 +114,16 @@ export const postToTG: TEFlow<[UUID, CreateSocialPost], TelegramBot.Message> =
if (SocialPostPhoto.is(m)) {
return ctx.tg.postPhoto(m.media, mediaText);
}

if (SocialPostDocument.is(m)) {
return ctx.tg.postFile(
mediaText,
m.filename,
m.media,
PDFType.value,
);
}

if (SocialPostVideo.is(m)) {
return pipe(
ctx.http.get<Stream>(m.media, {
Expand All @@ -125,7 +137,16 @@ export const postToTG: TEFlow<[UUID, CreateSocialPost], TelegramBot.Message> =
);
}
}
return ctx.tg.postMediaGroup(mediaText, media);
const allowedMedia = pipe(
media.map((m) => {
if (m.type === 'document') {
return fp.E.left(m);
}
return fp.E.right(m);
}),
fp.A.separate,
);
return ctx.tg.postMediaGroup(mediaText, allowedMedia.right);
}),
fp.TE.chain((message) =>
useReply
Expand Down

0 comments on commit 94935bf

Please sign in to comment.