@im-more/dsh-telegram 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/cordis.patch.yml +10 -0
- package/lib/index.js +2067 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +70 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +231 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +34 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +41 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +39 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +382 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +33 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +89 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +19 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +34 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +10 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +8 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/media.d.ts +38 -0
- package/lib/types/media.d.ts.map +1 -0
- package/lib/types/media.js +65 -0
- package/lib/types/media.js.map +1 -0
- package/lib/types/modes/html.d.ts +16 -0
- package/lib/types/modes/html.d.ts.map +1 -0
- package/lib/types/modes/html.js +155 -0
- package/lib/types/modes/html.js.map +1 -0
- package/lib/types/modes/plain.d.ts +14 -0
- package/lib/types/modes/plain.d.ts.map +1 -0
- package/lib/types/modes/plain.js +56 -0
- package/lib/types/modes/plain.js.map +1 -0
- package/lib/types/modes/shared.d.ts +140 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +405 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/polling.d.ts +26 -0
- package/lib/types/polling.d.ts.map +1 -0
- package/lib/types/polling.js +149 -0
- package/lib/types/polling.js.map +1 -0
- package/lib/types/request.d.ts +67 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +158 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/secret.d.ts +27 -0
- package/lib/types/secret.d.ts.map +1 -0
- package/lib/types/secret.js +39 -0
- package/lib/types/secret.js.map +1 -0
- package/lib/types/send.d.ts +48 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +326 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/types/game.d.ts +102 -0
- package/lib/types/types/game.d.ts.map +1 -0
- package/lib/types/types/game.js +5 -0
- package/lib/types/types/game.js.map +1 -0
- package/lib/types/types/index.d.ts +2751 -0
- package/lib/types/types/index.d.ts.map +1 -0
- package/lib/types/types/index.js +88 -0
- package/lib/types/types/index.js.map +1 -0
- package/lib/types/types/inline.d.ts +796 -0
- package/lib/types/types/inline.d.ts.map +1 -0
- package/lib/types/types/inline.js +4 -0
- package/lib/types/types/inline.js.map +1 -0
- package/lib/types/types/internal.d.ts +32 -0
- package/lib/types/types/internal.d.ts.map +1 -0
- package/lib/types/types/internal.js +30 -0
- package/lib/types/types/internal.js.map +1 -0
- package/lib/types/types/passport.d.ts +229 -0
- package/lib/types/types/passport.d.ts.map +1 -0
- package/lib/types/types/passport.js +3 -0
- package/lib/types/types/passport.js.map +1 -0
- package/lib/types/types/payment.d.ts +285 -0
- package/lib/types/types/payment.d.ts.map +1 -0
- package/lib/types/types/payment.js +6 -0
- package/lib/types/types/payment.js.map +1 -0
- package/lib/types/types/sticker.d.ts +221 -0
- package/lib/types/types/sticker.d.ts.map +1 -0
- package/lib/types/types/sticker.js +11 -0
- package/lib/types/types/sticker.js.map +1 -0
- package/lib/types/types/update.d.ts +97 -0
- package/lib/types/types/update.d.ts.map +1 -0
- package/lib/types/types/update.js +6 -0
- package/lib/types/types/update.js.map +1 -0
- package/lib/types/webhook.d.ts +29 -0
- package/lib/types/webhook.d.ts.map +1 -0
- package/lib/types/webhook.js +129 -0
- package/lib/types/webhook.js.map +1 -0
- package/package.json +61 -3
- package/src/bot.ts +349 -0
- package/src/config.ts +73 -0
- package/src/decode.ts +472 -0
- package/src/errors.ts +103 -0
- package/src/identity.ts +41 -0
- package/src/index.ts +14 -0
- package/src/media.ts +79 -0
- package/src/modes/html.ts +193 -0
- package/src/modes/plain.ts +75 -0
- package/src/modes/shared.ts +505 -0
- package/src/polling.ts +176 -0
- package/src/request.ts +224 -0
- package/src/secret.ts +46 -0
- package/src/send.ts +454 -0
- package/src/types/.eslintrc.yml +2 -0
- package/src/types/game.ts +112 -0
- package/src/types/index.ts +3028 -0
- package/src/types/inline.ts +857 -0
- package/src/types/internal.ts +64 -0
- package/src/types/passport.ts +256 -0
- package/src/types/payment.ts +304 -0
- package/src/types/sticker.ts +244 -0
- package/src/types/update.ts +107 -0
- package/src/webhook.ts +168 -0
- package/README.md +0 -3
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telegram Bot API transport: one POST path for every method, envelope and
|
|
3
|
+
* error handling, multipart bodies, and file download.
|
|
4
|
+
*
|
|
5
|
+
* The typed method surface lives on `Internal`; this module owns the shared
|
|
6
|
+
* wire mechanics so the generated client and the upload paths cannot diverge.
|
|
7
|
+
*/
|
|
8
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
9
|
+
import type { Message } from './types/index.ts';
|
|
10
|
+
/** One Bot API method request body: JSON-shaped data or a multipart form. */
|
|
11
|
+
export type TelegramRequestBody = object | FormData;
|
|
12
|
+
/** Endpoints and dependencies shared by every Bot API request. */
|
|
13
|
+
export interface TelegramRequest {
|
|
14
|
+
readonly ctx: Context;
|
|
15
|
+
readonly token: string;
|
|
16
|
+
readonly origin: string;
|
|
17
|
+
readonly filesBase: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Build the request endpoints for one bot token.
|
|
21
|
+
*
|
|
22
|
+
* @param ctx - context whose `http` service performs requests.
|
|
23
|
+
* @param token - bot token, appended to the API origin.
|
|
24
|
+
* @param apiBaseUrl - Bot API base URL.
|
|
25
|
+
* @param filesBaseUrl - file download base URL.
|
|
26
|
+
* @returns the shared request endpoints.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createTelegramRequest(ctx: Context, token: string, apiBaseUrl: string, filesBaseUrl: string): TelegramRequest;
|
|
29
|
+
/**
|
|
30
|
+
* Invoke one Bot API method through the shared envelope and error path.
|
|
31
|
+
*
|
|
32
|
+
* @param request - endpoints for this bot.
|
|
33
|
+
* @param method - Bot API method name.
|
|
34
|
+
* @param payload - JSON body or multipart form.
|
|
35
|
+
* @param signal - cancellation.
|
|
36
|
+
* @returns the method's `result` field.
|
|
37
|
+
*/
|
|
38
|
+
export declare function invoke<T>(request: TelegramRequest, method: string, payload?: TelegramRequestBody, signal?: AbortSignal): Promise<T>;
|
|
39
|
+
/**
|
|
40
|
+
* Download one file the platform hosts.
|
|
41
|
+
*
|
|
42
|
+
* @param request - endpoints for this bot.
|
|
43
|
+
* @param filePath - `file_path` of a `getFile` answer.
|
|
44
|
+
* @param signal - cancellation.
|
|
45
|
+
* @returns the response whose body is the file.
|
|
46
|
+
*/
|
|
47
|
+
export declare function downloadFile(request: TelegramRequest, filePath: string, signal?: AbortSignal): Promise<Response>;
|
|
48
|
+
/**
|
|
49
|
+
* Send one multipart media form (`sendPhoto`, `sendVideo`, …).
|
|
50
|
+
*
|
|
51
|
+
* @param request - endpoints for this bot.
|
|
52
|
+
* @param method - Bot API send method.
|
|
53
|
+
* @param form - multipart body.
|
|
54
|
+
* @param signal - cancellation.
|
|
55
|
+
* @returns the sent message.
|
|
56
|
+
*/
|
|
57
|
+
export declare function sendMedia(request: TelegramRequest, method: string, form: FormData, signal?: AbortSignal): Promise<Message>;
|
|
58
|
+
/**
|
|
59
|
+
* Send one `sendMediaGroup` multipart form.
|
|
60
|
+
*
|
|
61
|
+
* @param request - endpoints for this bot.
|
|
62
|
+
* @param form - multipart body.
|
|
63
|
+
* @param signal - cancellation.
|
|
64
|
+
* @returns the sent messages.
|
|
65
|
+
*/
|
|
66
|
+
export declare function sendMediaGroup(request: TelegramRequest, form: FormData, signal?: AbortSignal): Promise<Message[]>;
|
|
67
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAGlD,OAAO,KAAK,EAAE,OAAO,EAAoB,MAAM,kBAAkB,CAAA;AAEjE,6EAA6E;AAC7E,MAAM,MAAM,mBAAmB,GAAG,MAAM,GAAG,QAAQ,CAAA;AAEnD,kEAAkE;AAClE,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAA;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;CAC3B;AAED;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,OAAO,EACZ,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GACnB,eAAe,CAQjB;AAED;;;;;;;;GAQG;AACH,wBAAsB,MAAM,CAAC,CAAC,EAC5B,OAAO,EAAE,eAAe,EACxB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,mBAAmB,EAC7B,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,CAAC,CAAC,CAuCZ;AAED;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,QAAQ,CAAC,CAMnB;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CACvB,OAAO,EAAE,eAAe,EACxB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,QAAQ,EACd,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,OAAO,CAAC,CAElB;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,QAAQ,EACd,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,OAAO,EAAE,CAAC,CAEpB"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telegram Bot API transport: one POST path for every method, envelope and
|
|
3
|
+
* error handling, multipart bodies, and file download.
|
|
4
|
+
*
|
|
5
|
+
* The typed method surface lives on `Internal`; this module owns the shared
|
|
6
|
+
* wire mechanics so the generated client and the upload paths cannot diverge.
|
|
7
|
+
*/
|
|
8
|
+
import { TelegramApiError } from "./errors.js";
|
|
9
|
+
/**
|
|
10
|
+
* Build the request endpoints for one bot token.
|
|
11
|
+
*
|
|
12
|
+
* @param ctx - context whose `http` service performs requests.
|
|
13
|
+
* @param token - bot token, appended to the API origin.
|
|
14
|
+
* @param apiBaseUrl - Bot API base URL.
|
|
15
|
+
* @param filesBaseUrl - file download base URL.
|
|
16
|
+
* @returns the shared request endpoints.
|
|
17
|
+
*/
|
|
18
|
+
export function createTelegramRequest(ctx, token, apiBaseUrl, filesBaseUrl) {
|
|
19
|
+
const base = apiBaseUrl.replace(/\/+$/, '');
|
|
20
|
+
return {
|
|
21
|
+
ctx,
|
|
22
|
+
token,
|
|
23
|
+
origin: `${base}/bot${token}`,
|
|
24
|
+
filesBase: filesBaseUrl.replace(/\/+$/, ''),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Invoke one Bot API method through the shared envelope and error path.
|
|
29
|
+
*
|
|
30
|
+
* @param request - endpoints for this bot.
|
|
31
|
+
* @param method - Bot API method name.
|
|
32
|
+
* @param payload - JSON body or multipart form.
|
|
33
|
+
* @param signal - cancellation.
|
|
34
|
+
* @returns the method's `result` field.
|
|
35
|
+
*/
|
|
36
|
+
export async function invoke(request, method, payload, signal) {
|
|
37
|
+
let response;
|
|
38
|
+
try {
|
|
39
|
+
response = (await request.ctx.http(`${request.origin}/${method}`, {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
data: payload ?? {},
|
|
42
|
+
...(signal === undefined ? {} : { signal }),
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
catch (cause) {
|
|
46
|
+
if (isCancellation(cause, signal))
|
|
47
|
+
throw cancellation(cause, signal);
|
|
48
|
+
const failed = responseOf(cause);
|
|
49
|
+
if (failed !== undefined) {
|
|
50
|
+
const failedBody = (await failed.json().catch(() => undefined));
|
|
51
|
+
if (failedBody !== undefined && typeof failedBody === 'object')
|
|
52
|
+
throw failure(method, failed.status, failedBody);
|
|
53
|
+
}
|
|
54
|
+
throw new TelegramApiError(`telegram ${method} request failed`, {
|
|
55
|
+
method,
|
|
56
|
+
cause,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
const body = (await response.json().catch(() => undefined));
|
|
60
|
+
if (body === undefined || typeof body !== 'object') {
|
|
61
|
+
// Reading the body fails when the answer is cut short by cancellation; that is still a cancellation.
|
|
62
|
+
if (signal?.aborted === true)
|
|
63
|
+
throw signal.reason;
|
|
64
|
+
throw new TelegramApiError(`telegram ${method} answered ${response.status} without a JSON envelope`, {
|
|
65
|
+
method,
|
|
66
|
+
status: response.status,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (!body.ok)
|
|
70
|
+
throw failure(method, response.status, body);
|
|
71
|
+
return body.result;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Download one file the platform hosts.
|
|
75
|
+
*
|
|
76
|
+
* @param request - endpoints for this bot.
|
|
77
|
+
* @param filePath - `file_path` of a `getFile` answer.
|
|
78
|
+
* @param signal - cancellation.
|
|
79
|
+
* @returns the response whose body is the file.
|
|
80
|
+
*/
|
|
81
|
+
export async function downloadFile(request, filePath, signal) {
|
|
82
|
+
const url = `${request.filesBase}/file/bot${request.token}/${filePath.replace(/^\/+/, '')}`;
|
|
83
|
+
return (await request.ctx.http(url, signal === undefined ? {} : { signal }));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Send one multipart media form (`sendPhoto`, `sendVideo`, …).
|
|
87
|
+
*
|
|
88
|
+
* @param request - endpoints for this bot.
|
|
89
|
+
* @param method - Bot API send method.
|
|
90
|
+
* @param form - multipart body.
|
|
91
|
+
* @param signal - cancellation.
|
|
92
|
+
* @returns the sent message.
|
|
93
|
+
*/
|
|
94
|
+
export function sendMedia(request, method, form, signal) {
|
|
95
|
+
return invoke(request, method, form, signal);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Send one `sendMediaGroup` multipart form.
|
|
99
|
+
*
|
|
100
|
+
* @param request - endpoints for this bot.
|
|
101
|
+
* @param form - multipart body.
|
|
102
|
+
* @param signal - cancellation.
|
|
103
|
+
* @returns the sent messages.
|
|
104
|
+
*/
|
|
105
|
+
export function sendMediaGroup(request, form, signal) {
|
|
106
|
+
return invoke(request, 'sendMediaGroup', form, signal);
|
|
107
|
+
}
|
|
108
|
+
function failure(method, status, body) {
|
|
109
|
+
const retryAfter = body.parameters?.retry_after;
|
|
110
|
+
return new TelegramApiError(`telegram ${method} answered ${body.error_code ?? status}${body.description === undefined ? '' : `: ${body.description}`}`, {
|
|
111
|
+
method,
|
|
112
|
+
status,
|
|
113
|
+
...(body.error_code === undefined ? {} : { errorCode: body.error_code }),
|
|
114
|
+
...(body.description === undefined
|
|
115
|
+
? {}
|
|
116
|
+
: { description: body.description }),
|
|
117
|
+
...(retryAfter === undefined ? {} : { retryAfterMs: retryAfter * 1000 }),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Whether a failed request was cancelled, either because its signal is aborted or because the failure it produced is
|
|
122
|
+
* an abort.
|
|
123
|
+
*
|
|
124
|
+
* @param cause - the value `ctx.http` rejected with.
|
|
125
|
+
* @param signal - the cancellation signal handed to the request.
|
|
126
|
+
* @returns true when the request was cancelled rather than rejected by the transport.
|
|
127
|
+
*/
|
|
128
|
+
function isCancellation(cause, signal) {
|
|
129
|
+
if (signal?.aborted === true)
|
|
130
|
+
return true;
|
|
131
|
+
if (isAbortError(cause))
|
|
132
|
+
return true;
|
|
133
|
+
return signal?.reason !== undefined && cause === signal.reason;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The value a cancelled request rejects with: the signal's reason, so the classifier recognises the cancellation
|
|
137
|
+
* instead of a wrapped transport failure.
|
|
138
|
+
*
|
|
139
|
+
* @param cause - the value `ctx.http` rejected with.
|
|
140
|
+
* @param signal - the cancellation signal handed to the request.
|
|
141
|
+
* @returns the abort error to rethrow.
|
|
142
|
+
*/
|
|
143
|
+
function cancellation(cause, signal) {
|
|
144
|
+
return signal?.aborted === true ? signal.reason : cause;
|
|
145
|
+
}
|
|
146
|
+
/** Whether a caught value is an abort error, including the `DOMException` a signal produces. */
|
|
147
|
+
function isAbortError(cause) {
|
|
148
|
+
return ((cause instanceof Error || cause instanceof DOMException) &&
|
|
149
|
+
cause.name === 'AbortError');
|
|
150
|
+
}
|
|
151
|
+
/** The HTTP service's failure carries the response it refused; recover it structurally. */
|
|
152
|
+
function responseOf(cause) {
|
|
153
|
+
if (typeof cause !== 'object' || cause === null || !('response' in cause))
|
|
154
|
+
return undefined;
|
|
155
|
+
const response = cause.response;
|
|
156
|
+
return response instanceof Response ? response : undefined;
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AA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AA;IACzC,IAAI,YAAY,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IACpC,OAAO,MAAM,EAAE,MAAM,KAAK,SAAS,IAAI,KAAK,KAAK,MAAM,CAAC,MAAM,CAAA;AAChE,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,YAAY,CACnB,KAAc,EACd,MAA+B;IAE/B,OAAO,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAA;AACzD,CAAC;AAED,gGAAgG;AAChG,SAAS,YAAY,CAAC,KAAc;IAClC,OAAO,CACL,CAAC,KAAK,YAAY,KAAK,IAAI,KAAK,YAAY,YAAY,CAAC;QACzD,KAAK,CAAC,IAAI,KAAK,YAAY,CAC5B,CAAA;AACH,CAAC;AAED,2FAA2F;AAC3F,SAAS,UAAU,CAAC,KAAc;IAChC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,CAAC,UAAU,IAAI,KAAK,CAAC;QACvE,OAAO,SAAS,CAAA;IAClB,MAAM,QAAQ,GAAI,KAAgC,CAAC,QAAQ,CAAA;IAC3D,OAAO,QAAQ,YAAY,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAA;AAC5D,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Domain-separation label for the derived webhook secret token. Changing it
|
|
3
|
+
* invalidates every already-configured webhook, so treat it as a wire constant.
|
|
4
|
+
*/
|
|
5
|
+
export declare const WEBHOOK_SECRET_LABEL = "dsh-im-more/telegram/webhook/secret-token/v1";
|
|
6
|
+
/**
|
|
7
|
+
* Derive the `secret_token` Telegram echoes in
|
|
8
|
+
* `X-Telegram-Bot-Api-Secret-Token`.
|
|
9
|
+
*
|
|
10
|
+
* The value is an HMAC of a fixed label keyed by the Bot API token: it is
|
|
11
|
+
* stable for one bot, never exposes the token, and stays inside Telegram's
|
|
12
|
+
* 1–256 character `A-Z a-z 0-9 _ -` alphabet because base64url uses that set.
|
|
13
|
+
*
|
|
14
|
+
* @param botToken - the Bot API token (`<bot_id>:<secret>`).
|
|
15
|
+
* @returns a 43-character base64url secret token.
|
|
16
|
+
*/
|
|
17
|
+
export declare function deriveWebhookSecret(botToken: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* Compare a received secret-token header with the derived value in constant
|
|
20
|
+
* time.
|
|
21
|
+
*
|
|
22
|
+
* @param expected - the derived secret token.
|
|
23
|
+
* @param received - the header value, if the request carried one.
|
|
24
|
+
* @returns whether the header matches.
|
|
25
|
+
*/
|
|
26
|
+
export declare function matchesWebhookSecret(expected: string, received: string | undefined): boolean;
|
|
27
|
+
//# sourceMappingURL=secret.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secret.d.ts","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,eAAO,MAAM,oBAAoB,iDACe,CAAA;AAEhD;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAI5D;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GAAG,SAAS,GAC3B,OAAO,CAQT"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Domain-separation label for the derived webhook secret token. Changing it
|
|
4
|
+
* invalidates every already-configured webhook, so treat it as a wire constant.
|
|
5
|
+
*/
|
|
6
|
+
export const WEBHOOK_SECRET_LABEL = 'dsh-im-more/telegram/webhook/secret-token/v1';
|
|
7
|
+
/**
|
|
8
|
+
* Derive the `secret_token` Telegram echoes in
|
|
9
|
+
* `X-Telegram-Bot-Api-Secret-Token`.
|
|
10
|
+
*
|
|
11
|
+
* The value is an HMAC of a fixed label keyed by the Bot API token: it is
|
|
12
|
+
* stable for one bot, never exposes the token, and stays inside Telegram's
|
|
13
|
+
* 1–256 character `A-Z a-z 0-9 _ -` alphabet because base64url uses that set.
|
|
14
|
+
*
|
|
15
|
+
* @param botToken - the Bot API token (`<bot_id>:<secret>`).
|
|
16
|
+
* @returns a 43-character base64url secret token.
|
|
17
|
+
*/
|
|
18
|
+
export function deriveWebhookSecret(botToken) {
|
|
19
|
+
return createHmac('sha256', botToken)
|
|
20
|
+
.update(WEBHOOK_SECRET_LABEL)
|
|
21
|
+
.digest('base64url');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Compare a received secret-token header with the derived value in constant
|
|
25
|
+
* time.
|
|
26
|
+
*
|
|
27
|
+
* @param expected - the derived secret token.
|
|
28
|
+
* @param received - the header value, if the request carried one.
|
|
29
|
+
* @returns whether the header matches.
|
|
30
|
+
*/
|
|
31
|
+
export function matchesWebhookSecret(expected, received) {
|
|
32
|
+
if (received === undefined)
|
|
33
|
+
return false;
|
|
34
|
+
const expectedBytes = Buffer.from(expected);
|
|
35
|
+
const receivedBytes = Buffer.from(received);
|
|
36
|
+
return (expectedBytes.length === receivedBytes.length &&
|
|
37
|
+
timingSafeEqual(expectedBytes, receivedBytes));
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=secret.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secret.js","sourceRoot":"","sources":["../../src/secret.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAEzD;;;GAGG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAC/B,8CAA8C,CAAA;AAEhD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,mBAAmB,CAAC,QAAgB;IAClD,OAAO,UAAU,CAAC,QAAQ,EAAE,QAAQ,CAAC;SAClC,MAAM,CAAC,oBAAoB,CAAC;SAC5B,MAAM,CAAC,WAAW,CAAC,CAAA;AACxB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAClC,QAAgB,EAChB,QAA4B;IAE5B,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO,KAAK,CAAA;IACxC,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC3C,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC3C,OAAO,CACL,aAAa,CAAC,MAAM,KAAK,aAAa,CAAC,MAAM;QAC7C,eAAe,CAAC,aAAa,EAAE,aAAa,CAAC,CAC9C,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type MessageHandle, type SendContext, type SentPart, type Target } from '@deepseek-ai/cordis-plugin-im-core';
|
|
2
|
+
import type { Config } from './config.ts';
|
|
3
|
+
import type { MediaStaging } from './media.ts';
|
|
4
|
+
import type { TelegramPayload } from './modes/shared.ts';
|
|
5
|
+
import type { Message } from './types/index.ts';
|
|
6
|
+
import type { Internal } from './types/internal.ts';
|
|
7
|
+
/** Everything `sendPayload` / `editPayload` / `deletePart` need from the adapter. */
|
|
8
|
+
export interface TelegramSendDeps {
|
|
9
|
+
readonly internal: Internal;
|
|
10
|
+
readonly config: Config;
|
|
11
|
+
readonly staging: MediaStaging;
|
|
12
|
+
readonly sendMedia: (method: string, form: FormData, signal: AbortSignal | undefined) => Promise<Message>;
|
|
13
|
+
readonly sendMediaGroup: (form: FormData, signal: AbortSignal | undefined) => Promise<Message[]>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Send one serialized payload.
|
|
17
|
+
*
|
|
18
|
+
* @param deps - adapter services.
|
|
19
|
+
* @param payload - the serialized draft.
|
|
20
|
+
* @param target - the chat (and thread) the payload goes to.
|
|
21
|
+
* @param ctx - core send context (signal, part index).
|
|
22
|
+
* @returns the published part.
|
|
23
|
+
*/
|
|
24
|
+
export declare function sendPayload(deps: TelegramSendDeps, payload: TelegramPayload, target: Target, ctx: SendContext): Promise<SentPart>;
|
|
25
|
+
/**
|
|
26
|
+
* Edit one published message in place.
|
|
27
|
+
*
|
|
28
|
+
* Text messages use `editMessageText`; media messages edit their caption. An
|
|
29
|
+
* edit cannot replace the media, and a caption edit without a caption or a
|
|
30
|
+
* keyboard would publish nothing, so both are refused for the caller to
|
|
31
|
+
* withdraw and resend.
|
|
32
|
+
*
|
|
33
|
+
* @param deps - adapter services.
|
|
34
|
+
* @param part - the published part.
|
|
35
|
+
* @param payload - the freshly serialized draft.
|
|
36
|
+
* @param handle - the message handle (carries the target chat).
|
|
37
|
+
* @param signal - cancellation.
|
|
38
|
+
*/
|
|
39
|
+
export declare function editPayload(deps: TelegramSendDeps, part: SentPart, payload: TelegramPayload, handle: MessageHandle, signal?: AbortSignal): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Delete one published message.
|
|
42
|
+
*
|
|
43
|
+
* @param deps - adapter services.
|
|
44
|
+
* @param part - the published part.
|
|
45
|
+
* @param handle - the message handle (carries the target chat).
|
|
46
|
+
*/
|
|
47
|
+
export declare function deletePart(deps: TelegramSendDeps, part: SentPart, handle: MessageHandle): Promise<void>;
|
|
48
|
+
//# sourceMappingURL=send.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../../src/send.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,MAAM,EACZ,MAAM,oCAAoC,CAAA;AAC3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,KAAK,EAAwB,OAAO,EAAE,MAAM,k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