Skip to content

Commit 2bb2d69

Browse files
committed
image-caption
1 parent 894c3c1 commit 2bb2d69

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/telegram.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,31 @@ async (
125125
});
126126
};
127127

128-
type TelegramImageType = "jpeg" | "png";
129-
130-
export const telegramSendPhoto = (botToken: string) =>
131-
async (
132-
userId: number,
133-
stream: NodeJS.ReadableStream,
134-
imageType: TelegramImageType,
135-
imageName: string,
136-
) => {
137-
const body = new FormData();
138-
body.append("chat_id", String(userId));
139-
body.append(
140-
"photo",
141-
new Blob(await streamToChunks(stream), { type: `image/${imageType}` }),
142-
imageName,
143-
);
144-
await fetch(`${tokenToTelegramURL(botToken)}sendPhoto`, {
145-
method: "POST",
146-
body,
147-
});
128+
type SendPhotoParams = {
129+
chatId: number;
130+
stream: NodeJS.ReadableStream;
131+
fileType: "jpeg" | "png";
132+
filename: string;
133+
caption?: string;
148134
};
149135

136+
export const telegramSendPhoto =
137+
(botToken: string) =>
138+
async ({ chatId, stream, fileType, filename, caption }: SendPhotoParams) => {
139+
const body = new FormData();
140+
body.append("chat_id", String(chatId));
141+
body.append(
142+
"photo",
143+
new Blob(await streamToChunks(stream), { type: `image/${fileType}` }),
144+
filename,
145+
);
146+
if (caption) body.append("caption", caption);
147+
await fetch(`${tokenToTelegramURL(botToken)}sendPhoto`, {
148+
method: "POST",
149+
body,
150+
});
151+
};
152+
150153
export const sendTelegramMessage = (token: string) =>
151154
pipe(
152155
retry(2, 500, (chat_id: number, text: string) =>

0 commit comments

Comments
 (0)