-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetTgFileData.ts
More file actions
58 lines (57 loc) · 1.68 KB
/
getTgFileData.ts
File metadata and controls
58 lines (57 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import type { TgApiOptions } from "./TgApiOptions.ts";
import { TG_API_URL } from "./TG_API_URL.ts";
import type { TgBotConfig } from "./TgBotConfig.ts";
import { TgError } from "./TgError.ts";
/**
* Fetches the contents of a file that you get from {@link TgApi.getFile}.
*
* This is just a simple wrapper over {@link fetch}.
*
* @throws {TgError} if the response is not OK.
*
* @example Downloading bot's profile photo.
*
* ```ts
* import { callTgApi, getTgFileData } from "./mod.ts";
*
* const botToken = "YOUR_TOKEN";
*
* const botUser = await callTgApi({ botToken }, "getMe", undefined);
*
* const botPhoto = await callTgApi(
* { botToken },
* "getUserProfilePhotos",
* { user_id: botUser.id }
* );
* const botPhotoFileId = botPhoto.photos[0]?.[0]?.file_id;
* if (botPhotoFileId == null) throw new Error("No profile photo");
*
* const botPhotoFile = await callTgApi(
* { botToken },
* "getFile",
* { file_id: botPhotoFileId }
* );
* if (botPhotoFile.file_path == null) throw new Error("Photo file unavailable");
*
* const botPhotoBlob = await getTgFileData({ botToken }, botPhotoFile.file_path);
* console.log(botPhotoBlob.size);
* ```
*/
export async function getTgFileData(
config: TgBotConfig,
filePath: string,
options?: TgApiOptions,
): Promise<Blob> {
const url = new URL(
`./file/bot${config.botToken}/${filePath}`,
config.apiUrl ?? TG_API_URL,
);
const localFetch = config.fetch ?? globalThis.fetch;
const response = await localFetch(url.href, {
method: "GET",
signal: options?.signal ?? null,
});
if (!response.ok) throw new TgError(response.statusText, response.status);
const blob = await response.blob();
return blob;
}