@aigne/afs-telegram 1.11.0-beta.12
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/LICENSE.md +26 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.cjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.mjs +10 -0
- package/dist/client.cjs +106 -0
- package/dist/client.d.cts +93 -0
- package/dist/client.d.cts.map +1 -0
- package/dist/client.d.mts +93 -0
- package/dist/client.d.mts.map +1 -0
- package/dist/client.mjs +106 -0
- package/dist/client.mjs.map +1 -0
- package/dist/index.cjs +432 -0
- package/dist/index.d.cts +113 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +113 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +433 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +56 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Proprietary License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2025 ArcBlock, Inc. All Rights Reserved.
|
|
4
|
+
|
|
5
|
+
This software and associated documentation files (the "Software") are proprietary
|
|
6
|
+
and confidential. Unauthorized copying, modification, distribution, or use of
|
|
7
|
+
this Software, via any medium, is strictly prohibited.
|
|
8
|
+
|
|
9
|
+
The Software is provided for internal use only within ArcBlock, Inc. and its
|
|
10
|
+
authorized affiliates.
|
|
11
|
+
|
|
12
|
+
## No License Granted
|
|
13
|
+
|
|
14
|
+
No license, express or implied, is granted to any party for any purpose.
|
|
15
|
+
All rights are reserved by ArcBlock, Inc.
|
|
16
|
+
|
|
17
|
+
## Public Artifact Distribution
|
|
18
|
+
|
|
19
|
+
Portions of this Software may be released publicly under separate open-source
|
|
20
|
+
licenses (such as MIT License) through designated public repositories. Such
|
|
21
|
+
public releases are governed by their respective licenses and do not affect
|
|
22
|
+
the proprietary nature of this repository.
|
|
23
|
+
|
|
24
|
+
## Contact
|
|
25
|
+
|
|
26
|
+
For licensing inquiries, contact: legal@arcblock.io
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/decorate.js
|
|
3
|
+
function __decorate(decorators, target, key, desc) {
|
|
4
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
6
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
7
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
exports.__decorate = __decorate;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/decorate.js
|
|
2
|
+
function __decorate(decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
export { __decorate };
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/client.ts
|
|
3
|
+
/**
|
|
4
|
+
* TelegramClient — pure Telegram Bot API wrapper.
|
|
5
|
+
*
|
|
6
|
+
* Zero external dependencies (uses native fetch).
|
|
7
|
+
* No AFS or application-specific concepts.
|
|
8
|
+
*/
|
|
9
|
+
const DEFAULT_API_BASE = "https://api.telegram.org";
|
|
10
|
+
const DEFAULT_TIMEOUT = 3e4;
|
|
11
|
+
const DEFAULT_POLL_TIMEOUT = 30;
|
|
12
|
+
var TelegramClient = class {
|
|
13
|
+
_token;
|
|
14
|
+
_apiBase;
|
|
15
|
+
_pollTimeout;
|
|
16
|
+
_timeout;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
if (!options.token) throw new Error("TelegramClient requires a token");
|
|
19
|
+
this._token = options.token;
|
|
20
|
+
this._apiBase = options.apiBase ?? DEFAULT_API_BASE;
|
|
21
|
+
this._pollTimeout = options.pollTimeout ?? DEFAULT_POLL_TIMEOUT;
|
|
22
|
+
this._timeout = options.timeout ?? DEFAULT_TIMEOUT;
|
|
23
|
+
}
|
|
24
|
+
async sendMessage(chatId, text, options) {
|
|
25
|
+
const body = {
|
|
26
|
+
chat_id: chatId,
|
|
27
|
+
text
|
|
28
|
+
};
|
|
29
|
+
if (options?.parseMode) body.parse_mode = options.parseMode;
|
|
30
|
+
if (options?.replyMarkup) body.reply_markup = options.replyMarkup;
|
|
31
|
+
return this._call("sendMessage", body);
|
|
32
|
+
}
|
|
33
|
+
async poll(offset, signal) {
|
|
34
|
+
try {
|
|
35
|
+
const result = await this._call("getUpdates", {
|
|
36
|
+
offset,
|
|
37
|
+
timeout: this._pollTimeout,
|
|
38
|
+
allowed_updates: [
|
|
39
|
+
"message",
|
|
40
|
+
"callback_query",
|
|
41
|
+
"inline_query"
|
|
42
|
+
]
|
|
43
|
+
}, signal);
|
|
44
|
+
return Array.isArray(result) ? result : [];
|
|
45
|
+
} catch {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async setCommands(commands) {
|
|
50
|
+
await this._call("setMyCommands", { commands });
|
|
51
|
+
}
|
|
52
|
+
async sendChatAction(chatId, action) {
|
|
53
|
+
await this._call("sendChatAction", {
|
|
54
|
+
chat_id: chatId,
|
|
55
|
+
action
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
async answerCallbackQuery(callbackQueryId, options) {
|
|
59
|
+
const body = { callback_query_id: callbackQueryId };
|
|
60
|
+
if (options?.text) body.text = options.text;
|
|
61
|
+
await this._call("answerCallbackQuery", body);
|
|
62
|
+
}
|
|
63
|
+
async answerInlineQuery(inlineQueryId, results, options) {
|
|
64
|
+
const body = {
|
|
65
|
+
inline_query_id: inlineQueryId,
|
|
66
|
+
results
|
|
67
|
+
};
|
|
68
|
+
if (options?.cacheTime !== void 0) body.cache_time = options.cacheTime;
|
|
69
|
+
await this._call("answerInlineQuery", body);
|
|
70
|
+
}
|
|
71
|
+
async setChatMenuButton(chatId, webAppUrl) {
|
|
72
|
+
await this._call("setChatMenuButton", {
|
|
73
|
+
chat_id: chatId,
|
|
74
|
+
menu_button: {
|
|
75
|
+
type: "web_app",
|
|
76
|
+
text: "Dashboard",
|
|
77
|
+
web_app: { url: webAppUrl }
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async getMe() {
|
|
82
|
+
return this._call("getMe", {});
|
|
83
|
+
}
|
|
84
|
+
static escapeHtml(text) {
|
|
85
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
86
|
+
}
|
|
87
|
+
_apiUrl(method) {
|
|
88
|
+
return `${this._apiBase}/bot${this._token}/${method}`;
|
|
89
|
+
}
|
|
90
|
+
async _call(method, body, signal) {
|
|
91
|
+
const data = await (await fetch(this._apiUrl(method), {
|
|
92
|
+
method: "POST",
|
|
93
|
+
headers: { "Content-Type": "application/json" },
|
|
94
|
+
body: JSON.stringify(body),
|
|
95
|
+
signal: signal ?? AbortSignal.timeout(this._timeout)
|
|
96
|
+
})).json();
|
|
97
|
+
if (!data.ok) {
|
|
98
|
+
const desc = (data.description || "Unknown error").replace(this._token, "***");
|
|
99
|
+
throw new Error(`Telegram API error: ${desc}`);
|
|
100
|
+
}
|
|
101
|
+
return data.result;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
exports.TelegramClient = TelegramClient;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
//#region src/client.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* TelegramClient — pure Telegram Bot API wrapper.
|
|
4
|
+
*
|
|
5
|
+
* Zero external dependencies (uses native fetch).
|
|
6
|
+
* No AFS or application-specific concepts.
|
|
7
|
+
*/
|
|
8
|
+
interface TelegramClientOptions {
|
|
9
|
+
token: string;
|
|
10
|
+
apiBase?: string;
|
|
11
|
+
pollTimeout?: number;
|
|
12
|
+
timeout?: number;
|
|
13
|
+
}
|
|
14
|
+
interface SendMessageOptions {
|
|
15
|
+
parseMode?: "HTML" | "Markdown" | "MarkdownV2";
|
|
16
|
+
replyMarkup?: unknown;
|
|
17
|
+
}
|
|
18
|
+
interface TelegramUpdate {
|
|
19
|
+
update_id: number;
|
|
20
|
+
message?: TelegramMessage;
|
|
21
|
+
callback_query?: TelegramCallbackQuery;
|
|
22
|
+
inline_query?: TelegramInlineQuery;
|
|
23
|
+
}
|
|
24
|
+
interface TelegramMessage {
|
|
25
|
+
message_id: number;
|
|
26
|
+
chat: {
|
|
27
|
+
id: number;
|
|
28
|
+
type?: string;
|
|
29
|
+
};
|
|
30
|
+
from?: {
|
|
31
|
+
id: number;
|
|
32
|
+
first_name?: string;
|
|
33
|
+
username?: string;
|
|
34
|
+
is_bot?: boolean;
|
|
35
|
+
};
|
|
36
|
+
text?: string;
|
|
37
|
+
date: number;
|
|
38
|
+
}
|
|
39
|
+
interface TelegramCallbackQuery {
|
|
40
|
+
id: string;
|
|
41
|
+
from: {
|
|
42
|
+
id: number;
|
|
43
|
+
first_name?: string;
|
|
44
|
+
username?: string;
|
|
45
|
+
};
|
|
46
|
+
message?: TelegramMessage;
|
|
47
|
+
data?: string;
|
|
48
|
+
}
|
|
49
|
+
interface TelegramInlineQuery {
|
|
50
|
+
id: string;
|
|
51
|
+
from: {
|
|
52
|
+
id: number;
|
|
53
|
+
first_name?: string;
|
|
54
|
+
username?: string;
|
|
55
|
+
};
|
|
56
|
+
query: string;
|
|
57
|
+
}
|
|
58
|
+
interface BotCommand {
|
|
59
|
+
command: string;
|
|
60
|
+
description: string;
|
|
61
|
+
}
|
|
62
|
+
declare class TelegramClient {
|
|
63
|
+
private readonly _token;
|
|
64
|
+
private readonly _apiBase;
|
|
65
|
+
private readonly _pollTimeout;
|
|
66
|
+
private readonly _timeout;
|
|
67
|
+
constructor(options: TelegramClientOptions);
|
|
68
|
+
sendMessage(chatId: string, text: string, options?: SendMessageOptions): Promise<{
|
|
69
|
+
message_id: number;
|
|
70
|
+
}>;
|
|
71
|
+
poll(offset: number, signal?: AbortSignal): Promise<TelegramUpdate[]>;
|
|
72
|
+
setCommands(commands: BotCommand[]): Promise<void>;
|
|
73
|
+
sendChatAction(chatId: string, action: string): Promise<void>;
|
|
74
|
+
answerCallbackQuery(callbackQueryId: string, options?: {
|
|
75
|
+
text?: string;
|
|
76
|
+
}): Promise<void>;
|
|
77
|
+
answerInlineQuery(inlineQueryId: string, results: unknown[], options?: {
|
|
78
|
+
cacheTime?: number;
|
|
79
|
+
}): Promise<void>;
|
|
80
|
+
setChatMenuButton(chatId: string, webAppUrl: string): Promise<void>;
|
|
81
|
+
getMe(): Promise<{
|
|
82
|
+
id: number;
|
|
83
|
+
is_bot: boolean;
|
|
84
|
+
first_name: string;
|
|
85
|
+
username?: string;
|
|
86
|
+
}>;
|
|
87
|
+
static escapeHtml(text: string): string;
|
|
88
|
+
private _apiUrl;
|
|
89
|
+
private _call;
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
export { TelegramClient, TelegramUpdate };
|
|
93
|
+
//# sourceMappingURL=client.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.cts","names":[],"sources":["../src/client.ts"],"mappings":";;AAWA;;;;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,WAAoB,OAAA;EAWtD,KAAA,CAAA,GAAS,OAAA;IAAU,EAAA;IAAY,MAAA;IAAiB,UAAA;IAAoB,QAAA;EAAA;EAAA,OAMnE,UAAA,CAAW,IAAA;EAAA,QAMV,OAAA;EAAA,QAIM,KAAA;AAAA"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
//#region src/client.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* TelegramClient — pure Telegram Bot API wrapper.
|
|
4
|
+
*
|
|
5
|
+
* Zero external dependencies (uses native fetch).
|
|
6
|
+
* No AFS or application-specific concepts.
|
|
7
|
+
*/
|
|
8
|
+
interface TelegramClientOptions {
|
|
9
|
+
token: string;
|
|
10
|
+
apiBase?: string;
|
|
11
|
+
pollTimeout?: number;
|
|
12
|
+
timeout?: number;
|
|
13
|
+
}
|
|
14
|
+
interface SendMessageOptions {
|
|
15
|
+
parseMode?: "HTML" | "Markdown" | "MarkdownV2";
|
|
16
|
+
replyMarkup?: unknown;
|
|
17
|
+
}
|
|
18
|
+
interface TelegramUpdate {
|
|
19
|
+
update_id: number;
|
|
20
|
+
message?: TelegramMessage;
|
|
21
|
+
callback_query?: TelegramCallbackQuery;
|
|
22
|
+
inline_query?: TelegramInlineQuery;
|
|
23
|
+
}
|
|
24
|
+
interface TelegramMessage {
|
|
25
|
+
message_id: number;
|
|
26
|
+
chat: {
|
|
27
|
+
id: number;
|
|
28
|
+
type?: string;
|
|
29
|
+
};
|
|
30
|
+
from?: {
|
|
31
|
+
id: number;
|
|
32
|
+
first_name?: string;
|
|
33
|
+
username?: string;
|
|
34
|
+
is_bot?: boolean;
|
|
35
|
+
};
|
|
36
|
+
text?: string;
|
|
37
|
+
date: number;
|
|
38
|
+
}
|
|
39
|
+
interface TelegramCallbackQuery {
|
|
40
|
+
id: string;
|
|
41
|
+
from: {
|
|
42
|
+
id: number;
|
|
43
|
+
first_name?: string;
|
|
44
|
+
username?: string;
|
|
45
|
+
};
|
|
46
|
+
message?: TelegramMessage;
|
|
47
|
+
data?: string;
|
|
48
|
+
}
|
|
49
|
+
interface TelegramInlineQuery {
|
|
50
|
+
id: string;
|
|
51
|
+
from: {
|
|
52
|
+
id: number;
|
|
53
|
+
first_name?: string;
|
|
54
|
+
username?: string;
|
|
55
|
+
};
|
|
56
|
+
query: string;
|
|
57
|
+
}
|
|
58
|
+
interface BotCommand {
|
|
59
|
+
command: string;
|
|
60
|
+
description: string;
|
|
61
|
+
}
|
|
62
|
+
declare class TelegramClient {
|
|
63
|
+
private readonly _token;
|
|
64
|
+
private readonly _apiBase;
|
|
65
|
+
private readonly _pollTimeout;
|
|
66
|
+
private readonly _timeout;
|
|
67
|
+
constructor(options: TelegramClientOptions);
|
|
68
|
+
sendMessage(chatId: string, text: string, options?: SendMessageOptions): Promise<{
|
|
69
|
+
message_id: number;
|
|
70
|
+
}>;
|
|
71
|
+
poll(offset: number, signal?: AbortSignal): Promise<TelegramUpdate[]>;
|
|
72
|
+
setCommands(commands: BotCommand[]): Promise<void>;
|
|
73
|
+
sendChatAction(chatId: string, action: string): Promise<void>;
|
|
74
|
+
answerCallbackQuery(callbackQueryId: string, options?: {
|
|
75
|
+
text?: string;
|
|
76
|
+
}): Promise<void>;
|
|
77
|
+
answerInlineQuery(inlineQueryId: string, results: unknown[], options?: {
|
|
78
|
+
cacheTime?: number;
|
|
79
|
+
}): Promise<void>;
|
|
80
|
+
setChatMenuButton(chatId: string, webAppUrl: string): Promise<void>;
|
|
81
|
+
getMe(): Promise<{
|
|
82
|
+
id: number;
|
|
83
|
+
is_bot: boolean;
|
|
84
|
+
first_name: string;
|
|
85
|
+
username?: string;
|
|
86
|
+
}>;
|
|
87
|
+
static escapeHtml(text: string): string;
|
|
88
|
+
private _apiUrl;
|
|
89
|
+
private _call;
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
export { TelegramClient, TelegramUpdate };
|
|
93
|
+
//# sourceMappingURL=client.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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AAA;EAAA,IACX,OAAA;EASG,iBAAA,CAAkB,MAAA,UAAgB,SAAA,WAAoB,OAAA;EAWtD,KAAA,CAAA,GAAS,OAAA;IAAU,EAAA;IAAY,MAAA;IAAiB,UAAA;IAAoB,QAAA;EAAA;EAAA,OAMnE,UAAA,CAAW,IAAA;EAAA,QAMV,OAAA;EAAA,QAIM,KAAA;AAAA"}
|
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
//#region src/client.ts
|
|
2
|
+
/**
|
|
3
|
+
* TelegramClient — pure Telegram Bot API wrapper.
|
|
4
|
+
*
|
|
5
|
+
* Zero external dependencies (uses native fetch).
|
|
6
|
+
* No AFS or application-specific concepts.
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_API_BASE = "https://api.telegram.org";
|
|
9
|
+
const DEFAULT_TIMEOUT = 3e4;
|
|
10
|
+
const DEFAULT_POLL_TIMEOUT = 30;
|
|
11
|
+
var TelegramClient = class {
|
|
12
|
+
_token;
|
|
13
|
+
_apiBase;
|
|
14
|
+
_pollTimeout;
|
|
15
|
+
_timeout;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
if (!options.token) throw new Error("TelegramClient requires a token");
|
|
18
|
+
this._token = options.token;
|
|
19
|
+
this._apiBase = options.apiBase ?? DEFAULT_API_BASE;
|
|
20
|
+
this._pollTimeout = options.pollTimeout ?? DEFAULT_POLL_TIMEOUT;
|
|
21
|
+
this._timeout = options.timeout ?? DEFAULT_TIMEOUT;
|
|
22
|
+
}
|
|
23
|
+
async sendMessage(chatId, text, options) {
|
|
24
|
+
const body = {
|
|
25
|
+
chat_id: chatId,
|
|
26
|
+
text
|
|
27
|
+
};
|
|
28
|
+
if (options?.parseMode) body.parse_mode = options.parseMode;
|
|
29
|
+
if (options?.replyMarkup) body.reply_markup = options.replyMarkup;
|
|
30
|
+
return this._call("sendMessage", body);
|
|
31
|
+
}
|
|
32
|
+
async poll(offset, signal) {
|
|
33
|
+
try {
|
|
34
|
+
const result = await this._call("getUpdates", {
|
|
35
|
+
offset,
|
|
36
|
+
timeout: this._pollTimeout,
|
|
37
|
+
allowed_updates: [
|
|
38
|
+
"message",
|
|
39
|
+
"callback_query",
|
|
40
|
+
"inline_query"
|
|
41
|
+
]
|
|
42
|
+
}, signal);
|
|
43
|
+
return Array.isArray(result) ? result : [];
|
|
44
|
+
} catch {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async setCommands(commands) {
|
|
49
|
+
await this._call("setMyCommands", { commands });
|
|
50
|
+
}
|
|
51
|
+
async sendChatAction(chatId, action) {
|
|
52
|
+
await this._call("sendChatAction", {
|
|
53
|
+
chat_id: chatId,
|
|
54
|
+
action
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async answerCallbackQuery(callbackQueryId, options) {
|
|
58
|
+
const body = { callback_query_id: callbackQueryId };
|
|
59
|
+
if (options?.text) body.text = options.text;
|
|
60
|
+
await this._call("answerCallbackQuery", body);
|
|
61
|
+
}
|
|
62
|
+
async answerInlineQuery(inlineQueryId, results, options) {
|
|
63
|
+
const body = {
|
|
64
|
+
inline_query_id: inlineQueryId,
|
|
65
|
+
results
|
|
66
|
+
};
|
|
67
|
+
if (options?.cacheTime !== void 0) body.cache_time = options.cacheTime;
|
|
68
|
+
await this._call("answerInlineQuery", body);
|
|
69
|
+
}
|
|
70
|
+
async setChatMenuButton(chatId, webAppUrl) {
|
|
71
|
+
await this._call("setChatMenuButton", {
|
|
72
|
+
chat_id: chatId,
|
|
73
|
+
menu_button: {
|
|
74
|
+
type: "web_app",
|
|
75
|
+
text: "Dashboard",
|
|
76
|
+
web_app: { url: webAppUrl }
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async getMe() {
|
|
81
|
+
return this._call("getMe", {});
|
|
82
|
+
}
|
|
83
|
+
static escapeHtml(text) {
|
|
84
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
85
|
+
}
|
|
86
|
+
_apiUrl(method) {
|
|
87
|
+
return `${this._apiBase}/bot${this._token}/${method}`;
|
|
88
|
+
}
|
|
89
|
+
async _call(method, body, signal) {
|
|
90
|
+
const data = await (await fetch(this._apiUrl(method), {
|
|
91
|
+
method: "POST",
|
|
92
|
+
headers: { "Content-Type": "application/json" },
|
|
93
|
+
body: JSON.stringify(body),
|
|
94
|
+
signal: signal ?? AbortSignal.timeout(this._timeout)
|
|
95
|
+
})).json();
|
|
96
|
+
if (!data.ok) {
|
|
97
|
+
const desc = (data.description || "Unknown error").replace(this._token, "***");
|
|
98
|
+
throw new Error(`Telegram API error: ${desc}`);
|
|
99
|
+
}
|
|
100
|
+
return data.result;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
//#endregion
|
|
105
|
+
export { TelegramClient };
|
|
106
|
+
//# sourceMappingURL=client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.mjs","names":[],"sources":["../src/client.ts"],"sourcesContent":["/**\n * TelegramClient — pure Telegram Bot API wrapper.\n *\n * Zero external dependencies (uses native fetch).\n * No AFS or application-specific concepts.\n */\n\nconst DEFAULT_API_BASE = \"https://api.telegram.org\";\nconst DEFAULT_TIMEOUT = 30_000;\nconst DEFAULT_POLL_TIMEOUT = 30; // seconds (Telegram long-poll)\n\nexport interface TelegramClientOptions {\n token: string;\n apiBase?: string;\n pollTimeout?: number; // seconds\n timeout?: number; // ms for non-poll requests\n}\n\nexport interface SendMessageOptions {\n parseMode?: \"HTML\" | \"Markdown\" | \"MarkdownV2\";\n replyMarkup?: unknown;\n}\n\nexport interface TelegramUpdate {\n update_id: number;\n message?: TelegramMessage;\n callback_query?: TelegramCallbackQuery;\n inline_query?: TelegramInlineQuery;\n}\n\nexport interface TelegramMessage {\n message_id: number;\n chat: { id: number; type?: string };\n from?: { id: number; first_name?: string; username?: string; is_bot?: boolean };\n text?: string;\n date: number;\n}\n\nexport interface TelegramCallbackQuery {\n id: string;\n from: { id: number; first_name?: string; username?: string };\n message?: TelegramMessage;\n data?: string;\n}\n\nexport interface TelegramInlineQuery {\n id: string;\n from: { id: number; first_name?: string; username?: string };\n query: string;\n}\n\nexport interface BotCommand {\n command: string;\n description: string;\n}\n\nexport class TelegramClient {\n private readonly _token: string;\n private readonly _apiBase: string;\n private readonly _pollTimeout: number;\n private readonly _timeout: number;\n\n constructor(options: TelegramClientOptions) {\n if (!options.token) throw new Error(\"TelegramClient requires a token\");\n this._token = options.token;\n this._apiBase = options.apiBase ?? DEFAULT_API_BASE;\n this._pollTimeout = options.pollTimeout ?? DEFAULT_POLL_TIMEOUT;\n this._timeout = options.timeout ?? DEFAULT_TIMEOUT;\n }\n\n // ─── API Methods ─────────────────────────────────────────\n\n async sendMessage(\n chatId: string,\n text: string,\n options?: SendMessageOptions,\n ): Promise<{ message_id: number }> {\n const body: Record<string, unknown> = { chat_id: chatId, text };\n if (options?.parseMode) body.parse_mode = options.parseMode;\n if (options?.replyMarkup) body.reply_markup = options.replyMarkup;\n return this._call(\"sendMessage\", body);\n }\n\n async poll(offset: number, signal?: AbortSignal): Promise<TelegramUpdate[]> {\n try {\n const result = await this._call(\n \"getUpdates\",\n {\n offset,\n timeout: this._pollTimeout,\n allowed_updates: [\"message\", \"callback_query\", \"inline_query\"],\n },\n signal,\n );\n return Array.isArray(result) ? result : [];\n } catch {\n return [];\n }\n }\n\n async setCommands(commands: BotCommand[]): Promise<void> {\n await this._call(\"setMyCommands\", { commands });\n }\n\n async sendChatAction(chatId: string, action: string): Promise<void> {\n await this._call(\"sendChatAction\", { chat_id: chatId, action });\n }\n\n async answerCallbackQuery(callbackQueryId: string, options?: { text?: string }): Promise<void> {\n const body: Record<string, unknown> = { callback_query_id: callbackQueryId };\n if (options?.text) body.text = options.text;\n await this._call(\"answerCallbackQuery\", body);\n }\n\n async answerInlineQuery(\n inlineQueryId: string,\n results: unknown[],\n options?: { cacheTime?: number },\n ): Promise<void> {\n const body: Record<string, unknown> = {\n inline_query_id: inlineQueryId,\n results,\n };\n if (options?.cacheTime !== undefined) body.cache_time = options.cacheTime;\n await this._call(\"answerInlineQuery\", body);\n }\n\n async setChatMenuButton(chatId: string, webAppUrl: string): Promise<void> {\n await this._call(\"setChatMenuButton\", {\n chat_id: chatId,\n menu_button: {\n type: \"web_app\",\n text: \"Dashboard\",\n web_app: { url: webAppUrl },\n },\n });\n }\n\n async getMe(): Promise<{ id: number; is_bot: boolean; first_name: string; username?: string }> {\n return this._call(\"getMe\", {});\n }\n\n // ─── Helpers ─────────────────────────────────────────────\n\n static escapeHtml(text: string): string {\n return text.replace(/&/g, \"&\").replace(/</g, \"<\").replace(/>/g, \">\");\n }\n\n // ─── Internal ────────────────────────────────────────────\n\n private _apiUrl(method: string): string {\n return `${this._apiBase}/bot${this._token}/${method}`;\n }\n\n private async _call(\n method: string,\n body: Record<string, unknown>,\n signal?: AbortSignal,\n ): Promise<any> {\n const response = await fetch(this._apiUrl(method), {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify(body),\n signal: signal ?? AbortSignal.timeout(this._timeout),\n });\n\n const data = (await response.json()) as { ok: boolean; result?: any; description?: string };\n if (!data.ok) {\n // Never leak token in error messages\n const desc = (data.description || \"Unknown error\").replace(this._token, \"***\");\n throw new Error(`Telegram API error: ${desc}`);\n }\n return data.result;\n }\n}\n"],"mappings":";;;;;;;AAOA,MAAM,mBAAmB;AACzB,MAAM,kBAAkB;AACxB,MAAM,uBAAuB;AA+C7B,IAAa,iBAAb,MAA4B;CAC1B,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,SAAgC;AAC1C,MAAI,CAAC,QAAQ,MAAO,OAAM,IAAI,MAAM,kCAAkC;AACtE,OAAK,SAAS,QAAQ;AACtB,OAAK,WAAW,QAAQ,WAAW;AACnC,OAAK,eAAe,QAAQ,eAAe;AAC3C,OAAK,WAAW,QAAQ,WAAW;;CAKrC,MAA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