@effect-ak/tg-bot-client 0.6.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +230 -0
- package/dist/index.d.cts +95 -0
- package/dist/index.d.ts +58 -26
- package/dist/index.js +180 -1
- package/package.json +18 -52
- package/src/client-file.ts +70 -0
- package/src/client.ts +46 -0
- package/src/config.ts +13 -0
- package/src/const.ts +20 -0
- package/src/errors.ts +14 -0
- package/src/execute.ts +84 -0
- package/src/guards.ts +27 -0
- package/src/index.ts +7 -0
- package/test/execute.test.ts +102 -0
- package/test/file.test.ts +23 -0
- package/test/fixture.ts +35 -0
- package/tsconfig.json +12 -0
- package/tsup.config.json +10 -0
- package/dist/bot-bundle.js +0 -22
- package/dist/bot-bundle.mjs +0 -22
- package/dist/bot.d.ts +0 -111
- package/dist/bot.js +0 -1
- package/dist/bot.mjs +0 -1
- package/dist/config-DAcDINJv.d.ts +0 -3142
- package/dist/index.mjs +0 -1
- package/dist/webapp.d.ts +0 -392
- package/dist/webapp.js +0 -1
- package/dist/webapp.mjs +0 -0
- package/readme.md +0 -236
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
ClientFileService: () => ClientFileService,
|
|
34
|
+
MESSAGE_EFFECTS: () => MESSAGE_EFFECTS,
|
|
35
|
+
TG_BOT_API_URL: () => TG_BOT_API_URL,
|
|
36
|
+
TgBotApiBaseUrl: () => TgBotApiBaseUrl,
|
|
37
|
+
TgBotApiToken: () => TgBotApiToken,
|
|
38
|
+
TgBotClientError: () => TgBotClientError,
|
|
39
|
+
executeTgBotMethod: () => executeTgBotMethod,
|
|
40
|
+
isFileContent: () => isFileContent,
|
|
41
|
+
isMessageEffect: () => isMessageEffect,
|
|
42
|
+
isTgBotApiResponse: () => isTgBotApiResponse,
|
|
43
|
+
isTgBotApiUpdate: () => isTgBotApiUpdate,
|
|
44
|
+
makePayload: () => makePayload,
|
|
45
|
+
makeTgBotClient: () => makeTgBotClient,
|
|
46
|
+
messageEffectIdCodes: () => messageEffectIdCodes
|
|
47
|
+
});
|
|
48
|
+
module.exports = __toCommonJS(index_exports);
|
|
49
|
+
|
|
50
|
+
// src/execute.ts
|
|
51
|
+
var String = __toESM(require("effect/String"), 1);
|
|
52
|
+
var Micro = __toESM(require("effect/Micro"), 1);
|
|
53
|
+
|
|
54
|
+
// src/errors.ts
|
|
55
|
+
var Data = __toESM(require("effect/Data"), 1);
|
|
56
|
+
var TgBotClientError = class extends Data.TaggedError("TgBotClientError") {
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/guards.ts
|
|
60
|
+
var isFileContent = (input) => typeof input == "object" && input != null && "file_content" in input && input.file_content instanceof Uint8Array && "file_name" in input && typeof input.file_name == "string";
|
|
61
|
+
var isTgBotApiResponse = (input) => typeof input == "object" && input != null && "ok" in input && typeof input.ok == "boolean";
|
|
62
|
+
var isTgBotApiUpdate = (input) => typeof input == "object" && input != null && "update_id" in input && typeof input.update_id == "number";
|
|
63
|
+
|
|
64
|
+
// src/config.ts
|
|
65
|
+
var Context = __toESM(require("effect/Context"), 1);
|
|
66
|
+
|
|
67
|
+
// src/const.ts
|
|
68
|
+
var TG_BOT_API_URL = "https://api.telegram.org";
|
|
69
|
+
var MESSAGE_EFFECTS = {
|
|
70
|
+
"\u{1F525}": "5104841245755180586",
|
|
71
|
+
"\u{1F44D}": "5107584321108051014",
|
|
72
|
+
"\u{1F44E}": "5104858069142078462",
|
|
73
|
+
"\u2764\uFE0F": "5159385139981059251",
|
|
74
|
+
"\u{1F389}": "5046509860389126442",
|
|
75
|
+
"\u{1F4A9}": "5046589136895476101"
|
|
76
|
+
};
|
|
77
|
+
var messageEffectIdCodes = Object.keys(
|
|
78
|
+
MESSAGE_EFFECTS
|
|
79
|
+
);
|
|
80
|
+
var isMessageEffect = (input) => {
|
|
81
|
+
return typeof input === "string" && input in MESSAGE_EFFECTS;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// src/config.ts
|
|
85
|
+
var TgBotApiBaseUrl = class extends Context.Reference()(
|
|
86
|
+
"TgBotApiBaseUrl",
|
|
87
|
+
{ defaultValue: () => TG_BOT_API_URL }
|
|
88
|
+
) {
|
|
89
|
+
};
|
|
90
|
+
var TgBotApiToken = class extends Context.Tag("TgBotApiToken")() {
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// src/execute.ts
|
|
94
|
+
var executeTgBotMethod = (method, input) => Micro.gen(function* () {
|
|
95
|
+
const botToken = yield* Micro.service(TgBotApiToken);
|
|
96
|
+
const baseUrl = yield* Micro.service(TgBotApiBaseUrl);
|
|
97
|
+
const httpResponse = yield* Micro.tryPromise({
|
|
98
|
+
try: () => fetch(`${baseUrl}/bot${botToken}/${String.snakeToCamel(method)}`, {
|
|
99
|
+
body: makePayload(input) ?? null,
|
|
100
|
+
method: "POST"
|
|
101
|
+
}),
|
|
102
|
+
catch: (cause) => new TgBotClientError({
|
|
103
|
+
cause: { _tag: "ClientInternalError", cause }
|
|
104
|
+
})
|
|
105
|
+
});
|
|
106
|
+
const response = yield* Micro.tryPromise({
|
|
107
|
+
try: () => httpResponse.json(),
|
|
108
|
+
catch: () => new TgBotClientError({
|
|
109
|
+
cause: { _tag: "NotJsonResponse", response: httpResponse }
|
|
110
|
+
})
|
|
111
|
+
});
|
|
112
|
+
if (!isTgBotApiResponse(response)) {
|
|
113
|
+
return yield* Micro.fail(
|
|
114
|
+
new TgBotClientError({
|
|
115
|
+
cause: { _tag: "UnexpectedResponse", response }
|
|
116
|
+
})
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (!httpResponse.ok) {
|
|
120
|
+
return yield* Micro.fail(
|
|
121
|
+
new TgBotClientError({
|
|
122
|
+
cause: {
|
|
123
|
+
_tag: "NotOkResponse",
|
|
124
|
+
...response.error_code ? { errorCode: response.error_code } : void 0,
|
|
125
|
+
...response.description ? { details: response.description } : void 0
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return response.result;
|
|
131
|
+
});
|
|
132
|
+
var makePayload = (body) => {
|
|
133
|
+
const entries = Object.entries(body);
|
|
134
|
+
if (entries.length == 0) return void 0;
|
|
135
|
+
const result = new FormData();
|
|
136
|
+
for (const [key, value] of entries) {
|
|
137
|
+
if (!value) continue;
|
|
138
|
+
if (typeof value != "object") {
|
|
139
|
+
result.append(key, `${value}`);
|
|
140
|
+
} else if (isFileContent(value)) {
|
|
141
|
+
result.append(key, new Blob([value.file_content]), value.file_name);
|
|
142
|
+
} else {
|
|
143
|
+
result.append(key, JSON.stringify(value));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// src/client-file.ts
|
|
150
|
+
var Micro2 = __toESM(require("effect/Micro"), 1);
|
|
151
|
+
var Context2 = __toESM(require("effect/Context"), 1);
|
|
152
|
+
var ClientFileService = class _ClientFileService extends Context2.Tag("ClientFileService")() {
|
|
153
|
+
static live = () => {
|
|
154
|
+
return _ClientFileService.context({
|
|
155
|
+
getFile
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
var getFile = ({ fileId, type }) => getFileBytes(fileId).pipe(
|
|
160
|
+
Micro2.andThen(
|
|
161
|
+
({ content, file_name }) => new File([content], file_name, {
|
|
162
|
+
...type ? { type } : void 0
|
|
163
|
+
})
|
|
164
|
+
)
|
|
165
|
+
);
|
|
166
|
+
var getFileBytes = (fileId) => Micro2.gen(function* () {
|
|
167
|
+
const response = yield* executeTgBotMethod("get_file", { file_id: fileId });
|
|
168
|
+
const file_path = response.file_path;
|
|
169
|
+
if (!file_path || file_path.length == 0) {
|
|
170
|
+
return yield* Micro2.fail(
|
|
171
|
+
new TgBotClientError({
|
|
172
|
+
cause: {
|
|
173
|
+
_tag: "UnableToGetFile",
|
|
174
|
+
cause: "File path not defined"
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
const file_name = file_path.replaceAll("/", "-");
|
|
180
|
+
const baseUrl = yield* Micro2.service(TgBotApiBaseUrl);
|
|
181
|
+
const botToken = yield* Micro2.service(TgBotApiToken);
|
|
182
|
+
const url = `${baseUrl}/file/bot${botToken}/${file_path}`;
|
|
183
|
+
const content = yield* Micro2.tryPromise({
|
|
184
|
+
try: () => fetch(url).then((_) => _.arrayBuffer()),
|
|
185
|
+
catch: (cause) => new TgBotClientError({
|
|
186
|
+
cause: { _tag: "UnableToGetFile", cause }
|
|
187
|
+
})
|
|
188
|
+
});
|
|
189
|
+
return {
|
|
190
|
+
content,
|
|
191
|
+
file_name
|
|
192
|
+
};
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// src/client.ts
|
|
196
|
+
var Micro3 = __toESM(require("effect/Micro"), 1);
|
|
197
|
+
var Context3 = __toESM(require("effect/Context"), 1);
|
|
198
|
+
function makeTgBotClient(config) {
|
|
199
|
+
return createEffect(config).pipe(Micro3.runSync);
|
|
200
|
+
}
|
|
201
|
+
var createEffect = ({ bot_token }) => Micro3.gen(function* () {
|
|
202
|
+
const file = yield* Micro3.service(ClientFileService);
|
|
203
|
+
const context = Context3.make(TgBotApiToken, bot_token);
|
|
204
|
+
const execute = (method, input) => executeTgBotMethod(method, input).pipe(
|
|
205
|
+
Micro3.provideContext(context),
|
|
206
|
+
Micro3.runPromise
|
|
207
|
+
);
|
|
208
|
+
const getFile2 = (input) => file.getFile(input).pipe(Micro3.provideContext(context), Micro3.runPromise);
|
|
209
|
+
return {
|
|
210
|
+
execute,
|
|
211
|
+
getFile: getFile2
|
|
212
|
+
};
|
|
213
|
+
}).pipe(Micro3.provideContext(ClientFileService.live()));
|
|
214
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
215
|
+
0 && (module.exports = {
|
|
216
|
+
ClientFileService,
|
|
217
|
+
MESSAGE_EFFECTS,
|
|
218
|
+
TG_BOT_API_URL,
|
|
219
|
+
TgBotApiBaseUrl,
|
|
220
|
+
TgBotApiToken,
|
|
221
|
+
TgBotClientError,
|
|
222
|
+
executeTgBotMethod,
|
|
223
|
+
isFileContent,
|
|
224
|
+
isMessageEffect,
|
|
225
|
+
isTgBotApiResponse,
|
|
226
|
+
isTgBotApiUpdate,
|
|
227
|
+
makePayload,
|
|
228
|
+
makeTgBotClient,
|
|
229
|
+
messageEffectIdCodes
|
|
230
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import * as Micro from 'effect/Micro';
|
|
2
|
+
import { Api, Update } from '@effect-ak/tg-bot-api';
|
|
3
|
+
import * as effect_Cause from 'effect/Cause';
|
|
4
|
+
import * as effect_Types from 'effect/Types';
|
|
5
|
+
import * as Data from 'effect/Data';
|
|
6
|
+
import * as Context from 'effect/Context';
|
|
7
|
+
|
|
8
|
+
type ErrorReason = Data.TaggedEnum<{
|
|
9
|
+
NotOkResponse: {
|
|
10
|
+
errorCode?: number;
|
|
11
|
+
details?: string;
|
|
12
|
+
};
|
|
13
|
+
UnexpectedResponse: {
|
|
14
|
+
response: unknown;
|
|
15
|
+
};
|
|
16
|
+
ClientInternalError: {
|
|
17
|
+
cause: unknown;
|
|
18
|
+
};
|
|
19
|
+
UnableToGetFile: {
|
|
20
|
+
cause: unknown;
|
|
21
|
+
};
|
|
22
|
+
BotHandlerError: {
|
|
23
|
+
cause: unknown;
|
|
24
|
+
};
|
|
25
|
+
NotJsonResponse: {
|
|
26
|
+
response: unknown;
|
|
27
|
+
};
|
|
28
|
+
}>;
|
|
29
|
+
declare const TgBotClientError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
|
|
30
|
+
readonly _tag: "TgBotClientError";
|
|
31
|
+
} & Readonly<A>;
|
|
32
|
+
declare class TgBotClientError extends TgBotClientError_base<{
|
|
33
|
+
cause: ErrorReason;
|
|
34
|
+
}> {
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare const TgBotApiBaseUrl_base: Context.ReferenceClass<TgBotApiBaseUrl, "TgBotApiBaseUrl", string>;
|
|
38
|
+
declare class TgBotApiBaseUrl extends TgBotApiBaseUrl_base {
|
|
39
|
+
}
|
|
40
|
+
declare const TgBotApiToken_base: Context.TagClass<TgBotApiToken, "TgBotApiToken", string>;
|
|
41
|
+
declare class TgBotApiToken extends TgBotApiToken_base {
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare const executeTgBotMethod: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Micro.Micro<ReturnType<Api[M]>, TgBotClientError, TgBotApiToken>;
|
|
45
|
+
declare const makePayload: (body: object) => FormData | undefined;
|
|
46
|
+
|
|
47
|
+
declare const ClientFileService_base: Context.TagClass<ClientFileService, "ClientFileService", {
|
|
48
|
+
getFile: (input: GetFile) => ReturnType<typeof getFile>;
|
|
49
|
+
}>;
|
|
50
|
+
declare class ClientFileService extends ClientFileService_base {
|
|
51
|
+
static live: () => Context.Context<ClientFileService>;
|
|
52
|
+
}
|
|
53
|
+
interface GetFile {
|
|
54
|
+
fileId: string;
|
|
55
|
+
type?: string;
|
|
56
|
+
}
|
|
57
|
+
declare const getFile: ({ fileId, type }: GetFile) => Micro.Micro<File, TgBotClientError, TgBotApiToken>;
|
|
58
|
+
|
|
59
|
+
interface TgBotClient {
|
|
60
|
+
readonly execute: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Promise<ReturnType<Api[M]>>;
|
|
61
|
+
readonly getFile: (input: GetFile) => Promise<File>;
|
|
62
|
+
}
|
|
63
|
+
interface MakeTgClient {
|
|
64
|
+
bot_token: string;
|
|
65
|
+
}
|
|
66
|
+
declare function makeTgBotClient(config: MakeTgClient): TgBotClient;
|
|
67
|
+
|
|
68
|
+
interface FileContent {
|
|
69
|
+
file_content: Uint8Array<ArrayBuffer>;
|
|
70
|
+
file_name: string;
|
|
71
|
+
}
|
|
72
|
+
declare const isFileContent: (input: unknown) => input is FileContent;
|
|
73
|
+
interface TgBotApiResponseSchema {
|
|
74
|
+
ok: boolean;
|
|
75
|
+
error_code?: number;
|
|
76
|
+
description?: string;
|
|
77
|
+
result?: unknown;
|
|
78
|
+
}
|
|
79
|
+
declare const isTgBotApiResponse: (input: unknown) => input is TgBotApiResponseSchema;
|
|
80
|
+
declare const isTgBotApiUpdate: (input: unknown) => input is Update;
|
|
81
|
+
|
|
82
|
+
declare const TG_BOT_API_URL = "https://api.telegram.org";
|
|
83
|
+
declare const MESSAGE_EFFECTS: {
|
|
84
|
+
readonly "\uD83D\uDD25": "5104841245755180586";
|
|
85
|
+
readonly "\uD83D\uDC4D": "5107584321108051014";
|
|
86
|
+
readonly "\uD83D\uDC4E": "5104858069142078462";
|
|
87
|
+
readonly "\u2764\uFE0F": "5159385139981059251";
|
|
88
|
+
readonly "\uD83C\uDF89": "5046509860389126442";
|
|
89
|
+
readonly "\uD83D\uDCA9": "5046589136895476101";
|
|
90
|
+
};
|
|
91
|
+
type MessageEffect = keyof typeof MESSAGE_EFFECTS;
|
|
92
|
+
declare const messageEffectIdCodes: MessageEffect[];
|
|
93
|
+
declare const isMessageEffect: (input: unknown) => input is MessageEffect;
|
|
94
|
+
|
|
95
|
+
export { ClientFileService, type FileContent, type GetFile, MESSAGE_EFFECTS, type MessageEffect, TG_BOT_API_URL, TgBotApiBaseUrl, type TgBotApiResponseSchema, TgBotApiToken, type TgBotClient, TgBotClientError, executeTgBotMethod, isFileContent, isMessageEffect, isTgBotApiResponse, isTgBotApiUpdate, makePayload, makeTgBotClient, messageEffectIdCodes };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,49 +1,81 @@
|
|
|
1
1
|
import * as Micro from 'effect/Micro';
|
|
2
|
-
import {
|
|
3
|
-
export { bI as AcceptedGiftTypes, c as AddStickerToSetInput, bJ as AffiliateInfo, bH as AllowedUpdateName, bK as Animation, d as AnswerCallbackQueryInput, e as AnswerInlineQueryInput, f as AnswerPreCheckoutQueryInput, g as AnswerShippingQueryInput, h as AnswerWebAppQueryInput, i as ApproveChatJoinRequestInput, bL as Audio, bM as BackgroundFill, bN as BackgroundFillFreeformGradient, bO as BackgroundFillGradient, bP as BackgroundFillSolid, bQ as BackgroundType, bR as BackgroundTypeChatTheme, bS as BackgroundTypeFill, bT as BackgroundTypePattern, bU as BackgroundTypeWallpaper, B as BanChatMemberInput, j as BanChatSenderChatInput, bV as Birthdate, bW as BotCommand, bX as BotCommandScope, bY as BotCommandScopeAllChatAdministrators, bZ as BotCommandScopeAllGroupChats, b_ as BotCommandScopeAllPrivateChats, b$ as BotCommandScopeChat, c0 as BotCommandScopeChatAdministrators, c1 as BotCommandScopeChatMember, c2 as BotCommandScopeDefault, c3 as BotDescription, c4 as BotName, c5 as BotShortDescription, c6 as BusinessBotRights, c7 as BusinessConnection, c8 as BusinessIntro, c9 as BusinessLocation, ca as BusinessMessagesDeleted, cb as BusinessOpeningHours, cc as BusinessOpeningHoursInterval, cd as CallbackGame, ce as CallbackQuery, cf as Chat, cg as ChatAdministratorRights, ch as ChatBackground, ci as ChatBoost, cj as ChatBoostAdded, ck as ChatBoostRemoved, cl as ChatBoostSource, cm as ChatBoostSourceGiftCode, cn as ChatBoostSourceGiveaway, co as ChatBoostSourcePremium, cp as ChatBoostUpdated, cq as ChatFullInfo, cr as ChatInviteLink, cs as ChatJoinRequest, ct as ChatLocation, cu as ChatMember, cv as ChatMemberAdministrator, cw as ChatMemberBanned, cx as ChatMemberLeft, cy as ChatMemberMember, cz as ChatMemberOwner, cA as ChatMemberRestricted, cB as ChatMemberUpdated, cC as ChatPermissions, cD as ChatPhoto, cE as ChatShared, cF as Checklist, cG as ChecklistTask, cH as ChecklistTasksAdded, cI as ChecklistTasksDone, cJ as ChosenInlineResult, k as CloseForumTopicInput, l as CloseGeneralForumTopicInput, C as CloseInput, cK as Contact, m as ConvertGiftToStarsInput, n as CopyMessageInput, o as CopyMessagesInput, cL as CopyTextButton, p as CreateChatInviteLinkInput, q as CreateChatSubscriptionInviteLinkInput, r as CreateForumTopicInput, s as CreateInvoiceLinkInput, t as CreateNewStickerSetInput, D as DeclineChatJoinRequestInput, u as DeleteBusinessMessagesInput, v as DeleteChatPhotoInput, w as DeleteChatStickerSetInput, x as DeleteForumTopicInput, y as DeleteMessageInput, z as DeleteMessagesInput, E as DeleteMyCommandsInput, F as DeleteStickerFromSetInput, G as DeleteStickerSetInput, H as DeleteStoryInput, I as DeleteWebhookInput, cM as Dice, cN as DirectMessagePriceChanged, cO as Document, J as EditChatInviteLinkInput, K as EditChatSubscriptionInviteLinkInput, L as EditForumTopicInput, M as EditGeneralForumTopicInput, N as EditMessageCaptionInput, O as EditMessageChecklistInput, P as EditMessageLiveLocationInput, Q as EditMessageMediaInput, R as EditMessageReplyMarkupInput, S as EditMessageTextInput, V as EditStoryInput, W as EditUserStarSubscriptionInput, cP as EncryptedCredentials, cQ as EncryptedPassportElement, X as ExportChatInviteLinkInput, cR as ExternalReplyInfo, cS as File, cT as ForceReply, cU as ForumTopic, cV as ForumTopicClosed, cW as ForumTopicCreated, cX as ForumTopicEdited, cY as ForumTopicReopened, Y as ForwardMessageInput, Z as ForwardMessagesInput, cZ as Game, c_ as GameHighScore, c$ as GeneralForumTopicHidden, d0 as GeneralForumTopicUnhidden, _ as GetAvailableGiftsInput, $ as GetBusinessAccountGiftsInput, a0 as GetBusinessAccountStarBalanceInput, a1 as GetBusinessConnectionInput, a3 as GetChatAdministratorsInput, a2 as GetChatInput, a5 as GetChatMemberCountInput, a4 as GetChatMemberInput, a6 as GetChatMenuButtonInput, a7 as GetCustomEmojiStickersInput, a8 as GetFileInput, a9 as GetForumTopicIconStickersInput, aa as GetGameHighScoresInput, ab as GetMeInput, ac as GetMyCommandsInput, ad as GetMyDefaultAdministratorRightsInput, ae as GetMyDescriptionInput, af as GetMyNameInput, ag as GetMyShortDescriptionInput, ah as GetMyStarBalanceInput, ai as GetStarTransactionsInput, aj as GetStickerSetInput, ak as GetUpdatesInput, al as GetUserChatBoostsInput, am as GetUserProfilePhotosInput, an as GetWebhookInfoInput, d1 as Gift, d2 as GiftInfo, ao as GiftPremiumSubscriptionInput, d3 as Gifts, d4 as Giveaway, d5 as GiveawayCompleted, d6 as GiveawayCreated, d7 as GiveawayWinners, ap as HideGeneralForumTopicInput, d8 as InaccessibleMessage, d9 as InlineKeyboardButton, da as InlineKeyboardMarkup, db as InlineQuery, dc as InlineQueryResult, dd as InlineQueryResultArticle, de as InlineQueryResultAudio, df as InlineQueryResultCachedAudio, dg as InlineQueryResultCachedDocument, dh as InlineQueryResultCachedGif, di as InlineQueryResultCachedMpeg4Gif, dj as InlineQueryResultCachedPhoto, dk as InlineQueryResultCachedSticker, dl as InlineQueryResultCachedVideo, dm as InlineQueryResultCachedVoice, dn as InlineQueryResultContact, dp as InlineQueryResultDocument, dq as InlineQueryResultGame, dr as InlineQueryResultGif, ds as InlineQueryResultLocation, dt as InlineQueryResultMpeg4Gif, du as InlineQueryResultPhoto, dw as InlineQueryResultVenue, dx as InlineQueryResultVideo, dy as InlineQueryResultVoice, dv as InlineQueryResultsButton, dz as InputChecklist, dA as InputChecklistTask, dB as InputContactMessageContent, dC as InputFile, dD as InputInvoiceMessageContent, dE as InputLocationMessageContent, dF as InputMedia, dG as InputMediaAnimation, dH as InputMediaAudio, dI as InputMediaDocument, dJ as InputMediaPhoto, dK as InputMediaVideo, dL as InputMessageContent, dM as InputPaidMedia, dN as InputPaidMediaPhoto, dO as InputPaidMediaVideo, dP as InputPollOption, dQ as InputProfilePhoto, dR as InputProfilePhotoAnimated, dS as InputProfilePhotoStatic, dT as InputSticker, dU as InputStoryContent, dV as InputStoryContentPhoto, dW as InputStoryContentVideo, dX as InputTextMessageContent, dY as InputVenueMessageContent, dZ as Invoice, d_ as KeyboardButton, d$ as KeyboardButtonPollType, e0 as KeyboardButtonRequestChat, e1 as KeyboardButtonRequestUsers, e2 as LabeledPrice, aq as LeaveChatInput, e3 as LinkPreviewOptions, e4 as Location, e5 as LocationAddress, ar as LogOutInput, e6 as LoginUrl, e7 as MaskPosition, e8 as MaybeInaccessibleMessage, e9 as MenuButton, ea as MenuButtonCommands, eb as MenuButtonDefault, ec as MenuButtonWebApp, ed as Message, ee as MessageAutoDeleteTimerChanged, ef as MessageEntity, eg as MessageId, eh as MessageOrigin, ei as MessageOriginChannel, ej as MessageOriginChat, ek as MessageOriginHiddenUser, el as MessageOriginUser, em as MessageReactionCountUpdated, en as MessageReactionUpdated, eo as OrderInfo, ep as OwnedGift, eq as OwnedGiftRegular, es as OwnedGiftUnique, er as OwnedGifts, et as PaidMedia, eu as PaidMediaInfo, ev as PaidMediaPhoto, ew as PaidMediaPreview, ex as PaidMediaPurchased, ey as PaidMediaVideo, ez as PaidMessagePriceChanged, eA as PassportData, eB as PassportElementError, eC as PassportElementErrorDataField, eD as PassportElementErrorFile, eE as PassportElementErrorFiles, eF as PassportElementErrorFrontSide, eG as PassportElementErrorReverseSide, eH as PassportElementErrorSelfie, eI as PassportElementErrorTranslationFile, eJ as PassportElementErrorTranslationFiles, eK as PassportElementErrorUnspecified, eL as PassportFile, eM as PhotoSize, as as PinChatMessageInput, eN as Poll, eO as PollAnswer, eP as PollOption, at as PostStoryInput, eQ as PreCheckoutQuery, eR as PreparedInlineMessage, au as PromoteChatMemberInput, eS as ProximityAlertTriggered, eT as ReactionCount, eU as ReactionType, eV as ReactionTypeCustomEmoji, eW as ReactionTypeEmoji, eX as ReactionTypePaid, av as ReadBusinessMessageInput, aw as RefundStarPaymentInput, eY as RefundedPayment, ax as RemoveBusinessAccountProfilePhotoInput, ay as RemoveChatVerificationInput, az as RemoveUserVerificationInput, aA as ReopenForumTopicInput, aB as ReopenGeneralForumTopicInput, aC as ReplaceStickerInSetInput, eZ as ReplyKeyboardMarkup, e_ as ReplyKeyboardRemove, e$ as ReplyParameters, f0 as ResponseParameters, aD as RestrictChatMemberInput, f1 as RevenueWithdrawalState, f2 as RevenueWithdrawalStateFailed, f3 as RevenueWithdrawalStatePending, f4 as RevenueWithdrawalStateSucceeded, aE as RevokeChatInviteLinkInput, aF as SavePreparedInlineMessageInput, aG as SendAnimationInput, aH as SendAudioInput, aI as SendChatActionInput, aJ as SendChecklistInput, aK as SendContactInput, aL as SendDiceInput, aM as SendDocumentInput, aN as SendGameInput, aO as SendGiftInput, aP as SendInvoiceInput, aQ as SendLocationInput, aR as SendMediaGroupInput, aS as SendMessageInput, aT as SendPaidMediaInput, aU as SendPhotoInput, aV as SendPollInput, aW as SendStickerInput, aX as SendVenueInput, aY as SendVideoInput, aZ as SendVideoNoteInput, a_ as SendVoiceInput, f5 as SentWebAppMessage, a$ as SetBusinessAccountBioInput, b0 as SetBusinessAccountGiftSettingsInput, b1 as SetBusinessAccountNameInput, b2 as SetBusinessAccountProfilePhotoInput, b3 as SetBusinessAccountUsernameInput, b4 as SetChatAdministratorCustomTitleInput, b5 as SetChatDescriptionInput, b6 as SetChatMenuButtonInput, b7 as SetChatPermissionsInput, b8 as SetChatPhotoInput, b9 as SetChatStickerSetInput, ba as SetChatTitleInput, bb as SetCustomEmojiStickerSetThumbnailInput, bc as SetGameScoreInput, bd as SetMessageReactionInput, be as SetMyCommandsInput, bf as SetMyDefaultAdministratorRightsInput, bg as SetMyDescriptionInput, bh as SetMyNameInput, bi as SetMyShortDescriptionInput, bj as SetPassportDataErrorsInput, bk as SetStickerEmojiListInput, bl as SetStickerKeywordsInput, bm as SetStickerMaskPositionInput, bn as SetStickerPositionInSetInput, bo as SetStickerSetThumbnailInput, bp as SetStickerSetTitleInput, bq as SetUserEmojiStatusInput, br as SetWebhookInput, f6 as SharedUser, f7 as ShippingAddress, f8 as ShippingOption, f9 as ShippingQuery, fa as StarAmount, fb as StarTransaction, fc as StarTransactions, fd as Sticker, fe as StickerSet, bs as StopMessageLiveLocationInput, bt as StopPollInput, ff as Story, fg as StoryArea, fh as StoryAreaPosition, fi as StoryAreaType, fj as StoryAreaTypeLink, fk as StoryAreaTypeLocation, fl as StoryAreaTypeSuggestedReaction, fm as StoryAreaTypeUniqueGift, fn as StoryAreaTypeWeather, fo as SuccessfulPayment, fp as SwitchInlineQueryChosenChat, fq as TextQuote, b as TgBotApiBaseUrl, fr as TransactionPartner, fs as TransactionPartnerAffiliateProgram, ft as TransactionPartnerChat, fu as TransactionPartnerFragment, fv as TransactionPartnerOther, fw as TransactionPartnerTelegramAds, fx as TransactionPartnerTelegramApi, fy as TransactionPartnerUser, bu as TransferBusinessAccountStarsInput, bv as TransferGiftInput, bw as UnbanChatMemberInput, bx as UnbanChatSenderChatInput, by as UnhideGeneralForumTopicInput, fz as UniqueGift, fA as UniqueGiftBackdrop, fB as UniqueGiftBackdropColors, fC as UniqueGiftInfo, fD as UniqueGiftModel, fE as UniqueGiftSymbol, bz as UnpinAllChatMessagesInput, bA as UnpinAllForumTopicMessagesInput, bB as UnpinAllGeneralForumTopicMessagesInput, bC as UnpinChatMessageInput, bD as UpgradeGiftInput, bE as UploadStickerFileInput, fF as User, fG as UserChatBoosts, fH as UserProfilePhotos, fI as UsersShared, fJ as Venue, bF as VerifyChatInput, bG as VerifyUserInput, fK as Video, fL as VideoChatEnded, fM as VideoChatParticipantsInvited, fN as VideoChatScheduled, fO as VideoChatStarted, fP as VideoNote, fQ as Voice, fR as WebAppData, fS as WebAppInfo, fT as WebhookInfo, fU as WriteAccessAllowed } from './config-DAcDINJv.js';
|
|
4
|
-
import 'effect/
|
|
5
|
-
import 'effect/
|
|
6
|
-
import 'effect/
|
|
7
|
-
|
|
2
|
+
import { Api, Update } from '@effect-ak/tg-bot-api';
|
|
3
|
+
import * as effect_Cause from 'effect/Cause';
|
|
4
|
+
import * as effect_Types from 'effect/Types';
|
|
5
|
+
import * as Data from 'effect/Data';
|
|
6
|
+
import * as Context from 'effect/Context';
|
|
7
|
+
|
|
8
|
+
type ErrorReason = Data.TaggedEnum<{
|
|
9
|
+
NotOkResponse: {
|
|
10
|
+
errorCode?: number;
|
|
11
|
+
details?: string;
|
|
12
|
+
};
|
|
13
|
+
UnexpectedResponse: {
|
|
14
|
+
response: unknown;
|
|
15
|
+
};
|
|
16
|
+
ClientInternalError: {
|
|
17
|
+
cause: unknown;
|
|
18
|
+
};
|
|
19
|
+
UnableToGetFile: {
|
|
20
|
+
cause: unknown;
|
|
21
|
+
};
|
|
22
|
+
BotHandlerError: {
|
|
23
|
+
cause: unknown;
|
|
24
|
+
};
|
|
25
|
+
NotJsonResponse: {
|
|
26
|
+
response: unknown;
|
|
27
|
+
};
|
|
28
|
+
}>;
|
|
29
|
+
declare const TgBotClientError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
|
|
30
|
+
readonly _tag: "TgBotClientError";
|
|
31
|
+
} & Readonly<A>;
|
|
32
|
+
declare class TgBotClientError extends TgBotClientError_base<{
|
|
33
|
+
cause: ErrorReason;
|
|
34
|
+
}> {
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare const TgBotApiBaseUrl_base: Context.ReferenceClass<TgBotApiBaseUrl, "TgBotApiBaseUrl", string>;
|
|
38
|
+
declare class TgBotApiBaseUrl extends TgBotApiBaseUrl_base {
|
|
39
|
+
}
|
|
40
|
+
declare const TgBotApiToken_base: Context.TagClass<TgBotApiToken, "TgBotApiToken", string>;
|
|
41
|
+
declare class TgBotApiToken extends TgBotApiToken_base {
|
|
42
|
+
}
|
|
8
43
|
|
|
9
44
|
declare const executeTgBotMethod: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Micro.Micro<ReturnType<Api[M]>, TgBotClientError, TgBotApiToken>;
|
|
45
|
+
declare const makePayload: (body: object) => FormData | undefined;
|
|
10
46
|
|
|
11
|
-
|
|
47
|
+
declare const ClientFileService_base: Context.TagClass<ClientFileService, "ClientFileService", {
|
|
48
|
+
getFile: (input: GetFile) => ReturnType<typeof getFile>;
|
|
49
|
+
}>;
|
|
50
|
+
declare class ClientFileService extends ClientFileService_base {
|
|
51
|
+
static live: () => Context.Context<ClientFileService>;
|
|
52
|
+
}
|
|
53
|
+
interface GetFile {
|
|
12
54
|
fileId: string;
|
|
13
55
|
type?: string;
|
|
14
|
-
}
|
|
56
|
+
}
|
|
15
57
|
declare const getFile: ({ fileId, type }: GetFile) => Micro.Micro<File, TgBotClientError, TgBotApiToken>;
|
|
16
|
-
declare const getFileAsBase64String: ({ fileId, type, }: GetFile & {
|
|
17
|
-
type: string;
|
|
18
|
-
}) => Micro.Micro<{
|
|
19
|
-
encoded: string;
|
|
20
|
-
file_name: string;
|
|
21
|
-
}, TgBotClientError, TgBotApiToken>;
|
|
22
|
-
declare const getFileBytes: (fileId: string) => Micro.Micro<{
|
|
23
|
-
content: ArrayBuffer;
|
|
24
|
-
file_name: string;
|
|
25
|
-
}, TgBotClientError, TgBotApiToken>;
|
|
26
58
|
|
|
27
59
|
interface TgBotClient {
|
|
28
60
|
readonly execute: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Promise<ReturnType<Api[M]>>;
|
|
29
61
|
readonly getFile: (input: GetFile) => Promise<File>;
|
|
30
62
|
}
|
|
31
|
-
|
|
63
|
+
interface MakeTgClient {
|
|
32
64
|
bot_token: string;
|
|
33
|
-
}
|
|
65
|
+
}
|
|
34
66
|
declare function makeTgBotClient(config: MakeTgClient): TgBotClient;
|
|
35
67
|
|
|
36
|
-
|
|
37
|
-
file_content: Uint8Array
|
|
68
|
+
interface FileContent {
|
|
69
|
+
file_content: Uint8Array<ArrayBuffer>;
|
|
38
70
|
file_name: string;
|
|
39
|
-
}
|
|
71
|
+
}
|
|
40
72
|
declare const isFileContent: (input: unknown) => input is FileContent;
|
|
41
|
-
|
|
73
|
+
interface TgBotApiResponseSchema {
|
|
42
74
|
ok: boolean;
|
|
43
75
|
error_code?: number;
|
|
44
76
|
description?: string;
|
|
45
77
|
result?: unknown;
|
|
46
|
-
}
|
|
78
|
+
}
|
|
47
79
|
declare const isTgBotApiResponse: (input: unknown) => input is TgBotApiResponseSchema;
|
|
48
80
|
declare const isTgBotApiUpdate: (input: unknown) => input is Update;
|
|
49
81
|
|
|
@@ -60,4 +92,4 @@ type MessageEffect = keyof typeof MESSAGE_EFFECTS;
|
|
|
60
92
|
declare const messageEffectIdCodes: MessageEffect[];
|
|
61
93
|
declare const isMessageEffect: (input: unknown) => input is MessageEffect;
|
|
62
94
|
|
|
63
|
-
export {
|
|
95
|
+
export { ClientFileService, type FileContent, type GetFile, MESSAGE_EFFECTS, type MessageEffect, TG_BOT_API_URL, TgBotApiBaseUrl, type TgBotApiResponseSchema, TgBotApiToken, type TgBotClient, TgBotClientError, executeTgBotMethod, isFileContent, isMessageEffect, isTgBotApiResponse, isTgBotApiUpdate, makePayload, makeTgBotClient, messageEffectIdCodes };
|
package/dist/index.js
CHANGED
|
@@ -1 +1,180 @@
|
|
|
1
|
-
|
|
1
|
+
// src/execute.ts
|
|
2
|
+
import * as String from "effect/String";
|
|
3
|
+
import * as Micro from "effect/Micro";
|
|
4
|
+
|
|
5
|
+
// src/errors.ts
|
|
6
|
+
import * as Data from "effect/Data";
|
|
7
|
+
var TgBotClientError = class extends Data.TaggedError("TgBotClientError") {
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// src/guards.ts
|
|
11
|
+
var isFileContent = (input) => typeof input == "object" && input != null && "file_content" in input && input.file_content instanceof Uint8Array && "file_name" in input && typeof input.file_name == "string";
|
|
12
|
+
var isTgBotApiResponse = (input) => typeof input == "object" && input != null && "ok" in input && typeof input.ok == "boolean";
|
|
13
|
+
var isTgBotApiUpdate = (input) => typeof input == "object" && input != null && "update_id" in input && typeof input.update_id == "number";
|
|
14
|
+
|
|
15
|
+
// src/config.ts
|
|
16
|
+
import * as Context from "effect/Context";
|
|
17
|
+
|
|
18
|
+
// src/const.ts
|
|
19
|
+
var TG_BOT_API_URL = "https://api.telegram.org";
|
|
20
|
+
var MESSAGE_EFFECTS = {
|
|
21
|
+
"\u{1F525}": "5104841245755180586",
|
|
22
|
+
"\u{1F44D}": "5107584321108051014",
|
|
23
|
+
"\u{1F44E}": "5104858069142078462",
|
|
24
|
+
"\u2764\uFE0F": "5159385139981059251",
|
|
25
|
+
"\u{1F389}": "5046509860389126442",
|
|
26
|
+
"\u{1F4A9}": "5046589136895476101"
|
|
27
|
+
};
|
|
28
|
+
var messageEffectIdCodes = Object.keys(
|
|
29
|
+
MESSAGE_EFFECTS
|
|
30
|
+
);
|
|
31
|
+
var isMessageEffect = (input) => {
|
|
32
|
+
return typeof input === "string" && input in MESSAGE_EFFECTS;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/config.ts
|
|
36
|
+
var TgBotApiBaseUrl = class extends Context.Reference()(
|
|
37
|
+
"TgBotApiBaseUrl",
|
|
38
|
+
{ defaultValue: () => TG_BOT_API_URL }
|
|
39
|
+
) {
|
|
40
|
+
};
|
|
41
|
+
var TgBotApiToken = class extends Context.Tag("TgBotApiToken")() {
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/execute.ts
|
|
45
|
+
var executeTgBotMethod = (method, input) => Micro.gen(function* () {
|
|
46
|
+
const botToken = yield* Micro.service(TgBotApiToken);
|
|
47
|
+
const baseUrl = yield* Micro.service(TgBotApiBaseUrl);
|
|
48
|
+
const httpResponse = yield* Micro.tryPromise({
|
|
49
|
+
try: () => fetch(`${baseUrl}/bot${botToken}/${String.snakeToCamel(method)}`, {
|
|
50
|
+
body: makePayload(input) ?? null,
|
|
51
|
+
method: "POST"
|
|
52
|
+
}),
|
|
53
|
+
catch: (cause) => new TgBotClientError({
|
|
54
|
+
cause: { _tag: "ClientInternalError", cause }
|
|
55
|
+
})
|
|
56
|
+
});
|
|
57
|
+
const response = yield* Micro.tryPromise({
|
|
58
|
+
try: () => httpResponse.json(),
|
|
59
|
+
catch: () => new TgBotClientError({
|
|
60
|
+
cause: { _tag: "NotJsonResponse", response: httpResponse }
|
|
61
|
+
})
|
|
62
|
+
});
|
|
63
|
+
if (!isTgBotApiResponse(response)) {
|
|
64
|
+
return yield* Micro.fail(
|
|
65
|
+
new TgBotClientError({
|
|
66
|
+
cause: { _tag: "UnexpectedResponse", response }
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (!httpResponse.ok) {
|
|
71
|
+
return yield* Micro.fail(
|
|
72
|
+
new TgBotClientError({
|
|
73
|
+
cause: {
|
|
74
|
+
_tag: "NotOkResponse",
|
|
75
|
+
...response.error_code ? { errorCode: response.error_code } : void 0,
|
|
76
|
+
...response.description ? { details: response.description } : void 0
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return response.result;
|
|
82
|
+
});
|
|
83
|
+
var makePayload = (body) => {
|
|
84
|
+
const entries = Object.entries(body);
|
|
85
|
+
if (entries.length == 0) return void 0;
|
|
86
|
+
const result = new FormData();
|
|
87
|
+
for (const [key, value] of entries) {
|
|
88
|
+
if (!value) continue;
|
|
89
|
+
if (typeof value != "object") {
|
|
90
|
+
result.append(key, `${value}`);
|
|
91
|
+
} else if (isFileContent(value)) {
|
|
92
|
+
result.append(key, new Blob([value.file_content]), value.file_name);
|
|
93
|
+
} else {
|
|
94
|
+
result.append(key, JSON.stringify(value));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/client-file.ts
|
|
101
|
+
import * as Micro2 from "effect/Micro";
|
|
102
|
+
import * as Context2 from "effect/Context";
|
|
103
|
+
var ClientFileService = class _ClientFileService extends Context2.Tag("ClientFileService")() {
|
|
104
|
+
static live = () => {
|
|
105
|
+
return _ClientFileService.context({
|
|
106
|
+
getFile
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
var getFile = ({ fileId, type }) => getFileBytes(fileId).pipe(
|
|
111
|
+
Micro2.andThen(
|
|
112
|
+
({ content, file_name }) => new File([content], file_name, {
|
|
113
|
+
...type ? { type } : void 0
|
|
114
|
+
})
|
|
115
|
+
)
|
|
116
|
+
);
|
|
117
|
+
var getFileBytes = (fileId) => Micro2.gen(function* () {
|
|
118
|
+
const response = yield* executeTgBotMethod("get_file", { file_id: fileId });
|
|
119
|
+
const file_path = response.file_path;
|
|
120
|
+
if (!file_path || file_path.length == 0) {
|
|
121
|
+
return yield* Micro2.fail(
|
|
122
|
+
new TgBotClientError({
|
|
123
|
+
cause: {
|
|
124
|
+
_tag: "UnableToGetFile",
|
|
125
|
+
cause: "File path not defined"
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
const file_name = file_path.replaceAll("/", "-");
|
|
131
|
+
const baseUrl = yield* Micro2.service(TgBotApiBaseUrl);
|
|
132
|
+
const botToken = yield* Micro2.service(TgBotApiToken);
|
|
133
|
+
const url = `${baseUrl}/file/bot${botToken}/${file_path}`;
|
|
134
|
+
const content = yield* Micro2.tryPromise({
|
|
135
|
+
try: () => fetch(url).then((_) => _.arrayBuffer()),
|
|
136
|
+
catch: (cause) => new TgBotClientError({
|
|
137
|
+
cause: { _tag: "UnableToGetFile", cause }
|
|
138
|
+
})
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
content,
|
|
142
|
+
file_name
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// src/client.ts
|
|
147
|
+
import * as Micro3 from "effect/Micro";
|
|
148
|
+
import * as Context3 from "effect/Context";
|
|
149
|
+
function makeTgBotClient(config) {
|
|
150
|
+
return createEffect(config).pipe(Micro3.runSync);
|
|
151
|
+
}
|
|
152
|
+
var createEffect = ({ bot_token }) => Micro3.gen(function* () {
|
|
153
|
+
const file = yield* Micro3.service(ClientFileService);
|
|
154
|
+
const context = Context3.make(TgBotApiToken, bot_token);
|
|
155
|
+
const execute = (method, input) => executeTgBotMethod(method, input).pipe(
|
|
156
|
+
Micro3.provideContext(context),
|
|
157
|
+
Micro3.runPromise
|
|
158
|
+
);
|
|
159
|
+
const getFile2 = (input) => file.getFile(input).pipe(Micro3.provideContext(context), Micro3.runPromise);
|
|
160
|
+
return {
|
|
161
|
+
execute,
|
|
162
|
+
getFile: getFile2
|
|
163
|
+
};
|
|
164
|
+
}).pipe(Micro3.provideContext(ClientFileService.live()));
|
|
165
|
+
export {
|
|
166
|
+
ClientFileService,
|
|
167
|
+
MESSAGE_EFFECTS,
|
|
168
|
+
TG_BOT_API_URL,
|
|
169
|
+
TgBotApiBaseUrl,
|
|
170
|
+
TgBotApiToken,
|
|
171
|
+
TgBotClientError,
|
|
172
|
+
executeTgBotMethod,
|
|
173
|
+
isFileContent,
|
|
174
|
+
isMessageEffect,
|
|
175
|
+
isTgBotApiResponse,
|
|
176
|
+
isTgBotApiUpdate,
|
|
177
|
+
makePayload,
|
|
178
|
+
makeTgBotClient,
|
|
179
|
+
messageEffectIdCodes
|
|
180
|
+
};
|