@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
package/src/request.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
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
|
+
|
|
9
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
10
|
+
import type {} from '@deepseek-ai/cordis-plugin-http'
|
|
11
|
+
import { TelegramApiError } from './errors.ts'
|
|
12
|
+
import type { Message, TelegramResponse } from './types/index.ts'
|
|
13
|
+
|
|
14
|
+
/** One Bot API method request body: JSON-shaped data or a multipart form. */
|
|
15
|
+
export type TelegramRequestBody = object | FormData
|
|
16
|
+
|
|
17
|
+
/** Endpoints and dependencies shared by every Bot API request. */
|
|
18
|
+
export interface TelegramRequest {
|
|
19
|
+
readonly ctx: Context
|
|
20
|
+
readonly token: string
|
|
21
|
+
readonly origin: string
|
|
22
|
+
readonly filesBase: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Build the request endpoints for one bot token.
|
|
27
|
+
*
|
|
28
|
+
* @param ctx - context whose `http` service performs requests.
|
|
29
|
+
* @param token - bot token, appended to the API origin.
|
|
30
|
+
* @param apiBaseUrl - Bot API base URL.
|
|
31
|
+
* @param filesBaseUrl - file download base URL.
|
|
32
|
+
* @returns the shared request endpoints.
|
|
33
|
+
*/
|
|
34
|
+
export function createTelegramRequest(
|
|
35
|
+
ctx: Context,
|
|
36
|
+
token: string,
|
|
37
|
+
apiBaseUrl: string,
|
|
38
|
+
filesBaseUrl: string,
|
|
39
|
+
): TelegramRequest {
|
|
40
|
+
const base = apiBaseUrl.replace(/\/+$/, '')
|
|
41
|
+
return {
|
|
42
|
+
ctx,
|
|
43
|
+
token,
|
|
44
|
+
origin: `${base}/bot${token}`,
|
|
45
|
+
filesBase: filesBaseUrl.replace(/\/+$/, ''),
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Invoke one Bot API method through the shared envelope and error path.
|
|
51
|
+
*
|
|
52
|
+
* @param request - endpoints for this bot.
|
|
53
|
+
* @param method - Bot API method name.
|
|
54
|
+
* @param payload - JSON body or multipart form.
|
|
55
|
+
* @param signal - cancellation.
|
|
56
|
+
* @returns the method's `result` field.
|
|
57
|
+
*/
|
|
58
|
+
export async function invoke<T>(
|
|
59
|
+
request: TelegramRequest,
|
|
60
|
+
method: string,
|
|
61
|
+
payload?: TelegramRequestBody,
|
|
62
|
+
signal?: AbortSignal,
|
|
63
|
+
): Promise<T> {
|
|
64
|
+
let response: Response
|
|
65
|
+
try {
|
|
66
|
+
response = (await request.ctx.http(`${request.origin}/${method}`, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
data: payload ?? {},
|
|
69
|
+
...(signal === undefined ? {} : { signal }),
|
|
70
|
+
})) as unknown as Response
|
|
71
|
+
} catch (cause) {
|
|
72
|
+
if (isCancellation(cause, signal)) throw cancellation(cause, signal)
|
|
73
|
+
const failed = responseOf(cause)
|
|
74
|
+
if (failed !== undefined) {
|
|
75
|
+
const failedBody = (await failed.json().catch(() => undefined)) as
|
|
76
|
+
| TelegramResponse<T>
|
|
77
|
+
| undefined
|
|
78
|
+
if (failedBody !== undefined && typeof failedBody === 'object')
|
|
79
|
+
throw failure(method, failed.status, failedBody)
|
|
80
|
+
}
|
|
81
|
+
throw new TelegramApiError(`telegram ${method} request failed`, {
|
|
82
|
+
method,
|
|
83
|
+
cause,
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
const body = (await response.json().catch(() => undefined)) as
|
|
87
|
+
| TelegramResponse<T>
|
|
88
|
+
| undefined
|
|
89
|
+
if (body === undefined || typeof body !== 'object') {
|
|
90
|
+
// Reading the body fails when the answer is cut short by cancellation; that is still a cancellation.
|
|
91
|
+
if (signal?.aborted === true) throw signal.reason
|
|
92
|
+
throw new TelegramApiError(
|
|
93
|
+
`telegram ${method} answered ${response.status} without a JSON envelope`,
|
|
94
|
+
{
|
|
95
|
+
method,
|
|
96
|
+
status: response.status,
|
|
97
|
+
},
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
if (!body.ok) throw failure(method, response.status, body)
|
|
101
|
+
return body.result as T
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Download one file the platform hosts.
|
|
106
|
+
*
|
|
107
|
+
* @param request - endpoints for this bot.
|
|
108
|
+
* @param filePath - `file_path` of a `getFile` answer.
|
|
109
|
+
* @param signal - cancellation.
|
|
110
|
+
* @returns the response whose body is the file.
|
|
111
|
+
*/
|
|
112
|
+
export async function downloadFile(
|
|
113
|
+
request: TelegramRequest,
|
|
114
|
+
filePath: string,
|
|
115
|
+
signal?: AbortSignal,
|
|
116
|
+
): Promise<Response> {
|
|
117
|
+
const url = `${request.filesBase}/file/bot${request.token}/${filePath.replace(/^\/+/, '')}`
|
|
118
|
+
return (await request.ctx.http(
|
|
119
|
+
url,
|
|
120
|
+
signal === undefined ? {} : { signal },
|
|
121
|
+
)) as unknown as Response
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Send one multipart media form (`sendPhoto`, `sendVideo`, …).
|
|
126
|
+
*
|
|
127
|
+
* @param request - endpoints for this bot.
|
|
128
|
+
* @param method - Bot API send method.
|
|
129
|
+
* @param form - multipart body.
|
|
130
|
+
* @param signal - cancellation.
|
|
131
|
+
* @returns the sent message.
|
|
132
|
+
*/
|
|
133
|
+
export function sendMedia(
|
|
134
|
+
request: TelegramRequest,
|
|
135
|
+
method: string,
|
|
136
|
+
form: FormData,
|
|
137
|
+
signal?: AbortSignal,
|
|
138
|
+
): Promise<Message> {
|
|
139
|
+
return invoke<Message>(request, method, form, signal)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Send one `sendMediaGroup` multipart form.
|
|
144
|
+
*
|
|
145
|
+
* @param request - endpoints for this bot.
|
|
146
|
+
* @param form - multipart body.
|
|
147
|
+
* @param signal - cancellation.
|
|
148
|
+
* @returns the sent messages.
|
|
149
|
+
*/
|
|
150
|
+
export function sendMediaGroup(
|
|
151
|
+
request: TelegramRequest,
|
|
152
|
+
form: FormData,
|
|
153
|
+
signal?: AbortSignal,
|
|
154
|
+
): Promise<Message[]> {
|
|
155
|
+
return invoke<Message[]>(request, 'sendMediaGroup', form, signal)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function failure<T>(
|
|
159
|
+
method: string,
|
|
160
|
+
status: number,
|
|
161
|
+
body: TelegramResponse<T>,
|
|
162
|
+
): TelegramApiError {
|
|
163
|
+
const retryAfter = body.parameters?.retry_after
|
|
164
|
+
return new TelegramApiError(
|
|
165
|
+
`telegram ${method} answered ${body.error_code ?? status}${body.description === undefined ? '' : `: ${body.description}`}`,
|
|
166
|
+
{
|
|
167
|
+
method,
|
|
168
|
+
status,
|
|
169
|
+
...(body.error_code === undefined ? {} : { errorCode: body.error_code }),
|
|
170
|
+
...(body.description === undefined
|
|
171
|
+
? {}
|
|
172
|
+
: { description: body.description }),
|
|
173
|
+
...(retryAfter === undefined ? {} : { retryAfterMs: retryAfter * 1000 }),
|
|
174
|
+
},
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Whether a failed request was cancelled, either because its signal is aborted or because the failure it produced is
|
|
180
|
+
* an abort.
|
|
181
|
+
*
|
|
182
|
+
* @param cause - the value `ctx.http` rejected with.
|
|
183
|
+
* @param signal - the cancellation signal handed to the request.
|
|
184
|
+
* @returns true when the request was cancelled rather than rejected by the transport.
|
|
185
|
+
*/
|
|
186
|
+
function isCancellation(
|
|
187
|
+
cause: unknown,
|
|
188
|
+
signal: AbortSignal | undefined,
|
|
189
|
+
): boolean {
|
|
190
|
+
if (signal?.aborted === true) return true
|
|
191
|
+
if (isAbortError(cause)) return true
|
|
192
|
+
return signal?.reason !== undefined && cause === signal.reason
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The value a cancelled request rejects with: the signal's reason, so the classifier recognises the cancellation
|
|
197
|
+
* instead of a wrapped transport failure.
|
|
198
|
+
*
|
|
199
|
+
* @param cause - the value `ctx.http` rejected with.
|
|
200
|
+
* @param signal - the cancellation signal handed to the request.
|
|
201
|
+
* @returns the abort error to rethrow.
|
|
202
|
+
*/
|
|
203
|
+
function cancellation(
|
|
204
|
+
cause: unknown,
|
|
205
|
+
signal: AbortSignal | undefined,
|
|
206
|
+
): unknown {
|
|
207
|
+
return signal?.aborted === true ? signal.reason : cause
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Whether a caught value is an abort error, including the `DOMException` a signal produces. */
|
|
211
|
+
function isAbortError(cause: unknown): boolean {
|
|
212
|
+
return (
|
|
213
|
+
(cause instanceof Error || cause instanceof DOMException) &&
|
|
214
|
+
cause.name === 'AbortError'
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** The HTTP service's failure carries the response it refused; recover it structurally. */
|
|
219
|
+
function responseOf(cause: unknown): Response | undefined {
|
|
220
|
+
if (typeof cause !== 'object' || cause === null || !('response' in cause))
|
|
221
|
+
return undefined
|
|
222
|
+
const response = (cause as { response?: unknown }).response
|
|
223
|
+
return response instanceof Response ? response : undefined
|
|
224
|
+
}
|
package/src/secret.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Domain-separation label for the derived webhook secret token. Changing it
|
|
5
|
+
* invalidates every already-configured webhook, so treat it as a wire constant.
|
|
6
|
+
*/
|
|
7
|
+
export const WEBHOOK_SECRET_LABEL =
|
|
8
|
+
'dsh-im-more/telegram/webhook/secret-token/v1'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Derive the `secret_token` Telegram echoes in
|
|
12
|
+
* `X-Telegram-Bot-Api-Secret-Token`.
|
|
13
|
+
*
|
|
14
|
+
* The value is an HMAC of a fixed label keyed by the Bot API token: it is
|
|
15
|
+
* stable for one bot, never exposes the token, and stays inside Telegram's
|
|
16
|
+
* 1–256 character `A-Z a-z 0-9 _ -` alphabet because base64url uses that set.
|
|
17
|
+
*
|
|
18
|
+
* @param botToken - the Bot API token (`<bot_id>:<secret>`).
|
|
19
|
+
* @returns a 43-character base64url secret token.
|
|
20
|
+
*/
|
|
21
|
+
export function deriveWebhookSecret(botToken: string): string {
|
|
22
|
+
return createHmac('sha256', botToken)
|
|
23
|
+
.update(WEBHOOK_SECRET_LABEL)
|
|
24
|
+
.digest('base64url')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Compare a received secret-token header with the derived value in constant
|
|
29
|
+
* time.
|
|
30
|
+
*
|
|
31
|
+
* @param expected - the derived secret token.
|
|
32
|
+
* @param received - the header value, if the request carried one.
|
|
33
|
+
* @returns whether the header matches.
|
|
34
|
+
*/
|
|
35
|
+
export function matchesWebhookSecret(
|
|
36
|
+
expected: string,
|
|
37
|
+
received: string | undefined,
|
|
38
|
+
): boolean {
|
|
39
|
+
if (received === undefined) return false
|
|
40
|
+
const expectedBytes = Buffer.from(expected)
|
|
41
|
+
const receivedBytes = Buffer.from(received)
|
|
42
|
+
return (
|
|
43
|
+
expectedBytes.length === receivedBytes.length &&
|
|
44
|
+
timingSafeEqual(expectedBytes, receivedBytes)
|
|
45
|
+
)
|
|
46
|
+
}
|