@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.
Files changed (122) hide show
  1. package/LICENSE +21 -0
  2. package/cordis.patch.yml +10 -0
  3. package/lib/index.js +2067 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/bot.d.ts +70 -0
  6. package/lib/types/bot.d.ts.map +1 -0
  7. package/lib/types/bot.js +231 -0
  8. package/lib/types/bot.js.map +1 -0
  9. package/lib/types/config.d.ts +34 -0
  10. package/lib/types/config.d.ts.map +1 -0
  11. package/lib/types/config.js +41 -0
  12. package/lib/types/config.js.map +1 -0
  13. package/lib/types/decode.d.ts +39 -0
  14. package/lib/types/decode.d.ts.map +1 -0
  15. package/lib/types/decode.js +382 -0
  16. package/lib/types/decode.js.map +1 -0
  17. package/lib/types/errors.d.ts +33 -0
  18. package/lib/types/errors.d.ts.map +1 -0
  19. package/lib/types/errors.js +89 -0
  20. package/lib/types/errors.js.map +1 -0
  21. package/lib/types/identity.d.ts +19 -0
  22. package/lib/types/identity.d.ts.map +1 -0
  23. package/lib/types/identity.js +34 -0
  24. package/lib/types/identity.js.map +1 -0
  25. package/lib/types/index.d.ts +10 -0
  26. package/lib/types/index.d.ts.map +1 -0
  27. package/lib/types/index.js +8 -0
  28. package/lib/types/index.js.map +1 -0
  29. package/lib/types/media.d.ts +38 -0
  30. package/lib/types/media.d.ts.map +1 -0
  31. package/lib/types/media.js +65 -0
  32. package/lib/types/media.js.map +1 -0
  33. package/lib/types/modes/html.d.ts +16 -0
  34. package/lib/types/modes/html.d.ts.map +1 -0
  35. package/lib/types/modes/html.js +155 -0
  36. package/lib/types/modes/html.js.map +1 -0
  37. package/lib/types/modes/plain.d.ts +14 -0
  38. package/lib/types/modes/plain.d.ts.map +1 -0
  39. package/lib/types/modes/plain.js +56 -0
  40. package/lib/types/modes/plain.js.map +1 -0
  41. package/lib/types/modes/shared.d.ts +140 -0
  42. package/lib/types/modes/shared.d.ts.map +1 -0
  43. package/lib/types/modes/shared.js +405 -0
  44. package/lib/types/modes/shared.js.map +1 -0
  45. package/lib/types/polling.d.ts +26 -0
  46. package/lib/types/polling.d.ts.map +1 -0
  47. package/lib/types/polling.js +149 -0
  48. package/lib/types/polling.js.map +1 -0
  49. package/lib/types/request.d.ts +67 -0
  50. package/lib/types/request.d.ts.map +1 -0
  51. package/lib/types/request.js +158 -0
  52. package/lib/types/request.js.map +1 -0
  53. package/lib/types/secret.d.ts +27 -0
  54. package/lib/types/secret.d.ts.map +1 -0
  55. package/lib/types/secret.js +39 -0
  56. package/lib/types/secret.js.map +1 -0
  57. package/lib/types/send.d.ts +48 -0
  58. package/lib/types/send.d.ts.map +1 -0
  59. package/lib/types/send.js +326 -0
  60. package/lib/types/send.js.map +1 -0
  61. package/lib/types/types/game.d.ts +102 -0
  62. package/lib/types/types/game.d.ts.map +1 -0
  63. package/lib/types/types/game.js +5 -0
  64. package/lib/types/types/game.js.map +1 -0
  65. package/lib/types/types/index.d.ts +2751 -0
  66. package/lib/types/types/index.d.ts.map +1 -0
  67. package/lib/types/types/index.js +88 -0
  68. package/lib/types/types/index.js.map +1 -0
  69. package/lib/types/types/inline.d.ts +796 -0
  70. package/lib/types/types/inline.d.ts.map +1 -0
  71. package/lib/types/types/inline.js +4 -0
  72. package/lib/types/types/inline.js.map +1 -0
  73. package/lib/types/types/internal.d.ts +32 -0
  74. package/lib/types/types/internal.d.ts.map +1 -0
  75. package/lib/types/types/internal.js +30 -0
  76. package/lib/types/types/internal.js.map +1 -0
  77. package/lib/types/types/passport.d.ts +229 -0
  78. package/lib/types/types/passport.d.ts.map +1 -0
  79. package/lib/types/types/passport.js +3 -0
  80. package/lib/types/types/passport.js.map +1 -0
  81. package/lib/types/types/payment.d.ts +285 -0
  82. package/lib/types/types/payment.d.ts.map +1 -0
  83. package/lib/types/types/payment.js +6 -0
  84. package/lib/types/types/payment.js.map +1 -0
  85. package/lib/types/types/sticker.d.ts +221 -0
  86. package/lib/types/types/sticker.d.ts.map +1 -0
  87. package/lib/types/types/sticker.js +11 -0
  88. package/lib/types/types/sticker.js.map +1 -0
  89. package/lib/types/types/update.d.ts +97 -0
  90. package/lib/types/types/update.d.ts.map +1 -0
  91. package/lib/types/types/update.js +6 -0
  92. package/lib/types/types/update.js.map +1 -0
  93. package/lib/types/webhook.d.ts +29 -0
  94. package/lib/types/webhook.d.ts.map +1 -0
  95. package/lib/types/webhook.js +129 -0
  96. package/lib/types/webhook.js.map +1 -0
  97. package/package.json +61 -3
  98. package/src/bot.ts +349 -0
  99. package/src/config.ts +73 -0
  100. package/src/decode.ts +472 -0
  101. package/src/errors.ts +103 -0
  102. package/src/identity.ts +41 -0
  103. package/src/index.ts +14 -0
  104. package/src/media.ts +79 -0
  105. package/src/modes/html.ts +193 -0
  106. package/src/modes/plain.ts +75 -0
  107. package/src/modes/shared.ts +505 -0
  108. package/src/polling.ts +176 -0
  109. package/src/request.ts +224 -0
  110. package/src/secret.ts +46 -0
  111. package/src/send.ts +454 -0
  112. package/src/types/.eslintrc.yml +2 -0
  113. package/src/types/game.ts +112 -0
  114. package/src/types/index.ts +3028 -0
  115. package/src/types/inline.ts +857 -0
  116. package/src/types/internal.ts +64 -0
  117. package/src/types/passport.ts +256 -0
  118. package/src/types/payment.ts +304 -0
  119. package/src/types/sticker.ts +244 -0
  120. package/src/types/update.ts +107 -0
  121. package/src/webhook.ts +168 -0
  122. package/README.md +0 -3
package/src/send.ts ADDED
@@ -0,0 +1,454 @@
1
+ import {
2
+ PlatformError,
3
+ type MessageHandle,
4
+ type SendContext,
5
+ type SentPart,
6
+ type Target,
7
+ } from '@deepseek-ai/cordis-plugin-im-core'
8
+ import type { Config } from './config.ts'
9
+ import { classifyTelegramError, isNotModified } from './errors.ts'
10
+ import type { MediaStaging } from './media.ts'
11
+ import type { TelegramPayload } from './modes/shared.ts'
12
+ import type { InlineKeyboardMarkup, Message } from './types/index.ts'
13
+ import type { Internal } from './types/internal.ts'
14
+
15
+ /** Everything `sendPayload` / `editPayload` / `deletePart` need from the adapter. */
16
+ export interface TelegramSendDeps {
17
+ readonly internal: Internal
18
+ readonly config: Config
19
+ readonly staging: MediaStaging
20
+ readonly sendMedia: (
21
+ method: string,
22
+ form: FormData,
23
+ signal: AbortSignal | undefined,
24
+ ) => Promise<Message>
25
+ readonly sendMediaGroup: (
26
+ form: FormData,
27
+ signal: AbortSignal | undefined,
28
+ ) => Promise<Message[]>
29
+ }
30
+
31
+ /** Telegram method for one single-media payload. */
32
+ function sendMethod(kind: string): string {
33
+ switch (kind) {
34
+ case 'photo':
35
+ return 'sendPhoto'
36
+ case 'video':
37
+ return 'sendVideo'
38
+ case 'audio':
39
+ return 'sendAudio'
40
+ case 'animation':
41
+ return 'sendAnimation'
42
+ default:
43
+ return 'sendDocument'
44
+ }
45
+ }
46
+
47
+ /** Multipart field name of one media kind. */
48
+ function mediaField(kind: string): string {
49
+ switch (kind) {
50
+ case 'photo':
51
+ return 'photo'
52
+ case 'video':
53
+ return 'video'
54
+ case 'audio':
55
+ return 'audio'
56
+ case 'animation':
57
+ return 'animation'
58
+ default:
59
+ return 'document'
60
+ }
61
+ }
62
+
63
+ /** The inline keyboard a payload carries, if any. */
64
+ function markupOf(payload: TelegramPayload): InlineKeyboardMarkup | undefined {
65
+ return payload.buttons === undefined
66
+ ? undefined
67
+ : { inline_keyboard: payload.buttons }
68
+ }
69
+
70
+ /** Put the fields every send request shares into a multipart form. */
71
+ function appendCommon(
72
+ form: FormData,
73
+ payload: TelegramPayload,
74
+ threadId: number | undefined,
75
+ ): void {
76
+ form.set('chat_id', payload.chatId)
77
+ if (threadId !== undefined) form.set('message_thread_id', String(threadId))
78
+ if (payload.replyTo !== undefined) {
79
+ form.set(
80
+ 'reply_parameters',
81
+ JSON.stringify({
82
+ message_id: payload.replyTo,
83
+ allow_sending_without_reply: true,
84
+ }),
85
+ )
86
+ }
87
+ if (payload.caption !== undefined && payload.caption !== '') {
88
+ form.set('caption', payload.caption)
89
+ if (payload.parseMode !== undefined)
90
+ form.set('parse_mode', payload.parseMode)
91
+ }
92
+ const markup = markupOf(payload)
93
+ if (markup !== undefined) form.set('reply_markup', JSON.stringify(markup))
94
+ }
95
+
96
+ /** Wrap staged bytes in a named blob for multipart upload. */
97
+ function blobOf(staged: {
98
+ data: Uint8Array
99
+ name: string
100
+ mime?: string | undefined
101
+ }): Blob {
102
+ return new Blob(
103
+ [staged.data],
104
+ staged.mime === undefined ? {} : { type: staged.mime },
105
+ )
106
+ }
107
+
108
+ /**
109
+ * The published part of one sent message.
110
+ *
111
+ * @param message - the Telegram message the send answered with.
112
+ * @param mediaKeys - staged media keys the request carried, when it carried media.
113
+ * @returns the part, with the media keys recorded in `raw` for a later edit check.
114
+ */
115
+ function partOf(message: Message, mediaKeys?: readonly string[]): SentPart {
116
+ const id = message.message_id
117
+ // The request already reached the platform, so an answer without an id is a published outcome, not a refusal.
118
+ if (id === undefined)
119
+ throw new PlatformError(
120
+ 'invalid',
121
+ 'telegram: the Bot API answered without a message id',
122
+ { published: 'unknown' },
123
+ )
124
+ return {
125
+ id: String(id),
126
+ kind: 'message',
127
+ time: message.date === undefined ? Date.now() : message.date * 1000,
128
+ ...(message.message_thread_id === undefined
129
+ ? {}
130
+ : { thread: { id: String(message.message_thread_id) } }),
131
+ ...(mediaKeys === undefined ? {} : { raw: { mediaKeys } }),
132
+ }
133
+ }
134
+
135
+ /** Media keys recorded in `SentPart.raw` by {@link partOf}, absent on a handle rebuilt after a restart. */
136
+ function recordedMediaKeys(part: SentPart): readonly string[] | undefined {
137
+ const raw = part.raw
138
+ if (typeof raw !== 'object' || raw === null || !('mediaKeys' in raw))
139
+ return undefined
140
+ const keys = (raw as { mediaKeys?: string[] }).mediaKeys
141
+ return Array.isArray(keys) ? keys : undefined
142
+ }
143
+
144
+ /** Whether an edit still targets the media the part was published with. */
145
+ function sameMediaKeys(a: readonly string[], b: readonly string[]): boolean {
146
+ return a.length === b.length && a.every((key, index) => key === b[index])
147
+ }
148
+
149
+ /** Look one staged media item up, failing before any request when it expired. */
150
+ function stagedOf(deps: TelegramSendDeps, key: string) {
151
+ const staged = deps.staging.get(key)
152
+ if (staged === undefined) {
153
+ throw new PlatformError(
154
+ 'invalid',
155
+ 'telegram: the staged media bytes expired before send; nothing was published',
156
+ {
157
+ published: 'no',
158
+ },
159
+ )
160
+ }
161
+ return staged
162
+ }
163
+
164
+ /** Thread id carried by an adapter referrer, when the target asks to follow it. */
165
+ function referrerThread(referrer: unknown): number | undefined {
166
+ if (
167
+ typeof referrer !== 'object' ||
168
+ referrer === null ||
169
+ !('threadId' in referrer)
170
+ )
171
+ return undefined
172
+ const value = (referrer as { threadId?: unknown }).threadId
173
+ return typeof value === 'string' && /^\d+$/.test(value)
174
+ ? Number(value)
175
+ : undefined
176
+ }
177
+
178
+ /**
179
+ * Send one serialized payload.
180
+ *
181
+ * @param deps - adapter services.
182
+ * @param payload - the serialized draft.
183
+ * @param target - the chat (and thread) the payload goes to.
184
+ * @param ctx - core send context (signal, part index).
185
+ * @returns the published part.
186
+ */
187
+ export async function sendPayload(
188
+ deps: TelegramSendDeps,
189
+ payload: TelegramPayload,
190
+ target: Target,
191
+ ctx: SendContext,
192
+ ): Promise<SentPart> {
193
+ const signal = ctx.options.signal
194
+ const markup = markupOf(payload)
195
+ const threadId =
196
+ payload.threadId ??
197
+ (target.thread === true ? referrerThread(target.referrer) : undefined)
198
+ if (payload.kind === 'text') {
199
+ const linkPreview = deps.config.linkPreview
200
+ ? {}
201
+ : { link_preview_options: { is_disabled: true } }
202
+ const message = await deps.internal.sendMessage(
203
+ {
204
+ chat_id: payload.chatId,
205
+ text: payload.text ?? '',
206
+ ...(payload.parseMode === undefined
207
+ ? {}
208
+ : { parse_mode: payload.parseMode }),
209
+ ...(threadId === undefined ? {} : { message_thread_id: threadId }),
210
+ ...(payload.replyTo === undefined
211
+ ? {}
212
+ : {
213
+ reply_parameters: {
214
+ message_id: payload.replyTo,
215
+ allow_sending_without_reply: true,
216
+ },
217
+ }),
218
+ ...(markup === undefined ? {} : { reply_markup: markup }),
219
+ ...linkPreview,
220
+ },
221
+ signal,
222
+ )
223
+ return partOf(message)
224
+ }
225
+
226
+ const items = payload.media ?? []
227
+ const first = items[0]
228
+ if (first === undefined)
229
+ throw new PlatformError(
230
+ 'invalid',
231
+ 'telegram: media payload without media',
232
+ { published: 'no' },
233
+ )
234
+
235
+ if (payload.kind === 'album') {
236
+ const form = new FormData()
237
+ const media = items.map((item, index) => {
238
+ form.append(
239
+ `file${index}`,
240
+ blobOf(stagedOf(deps, item.key)),
241
+ item.name || `file${index}`,
242
+ )
243
+ return {
244
+ type: item.kind,
245
+ media: `attach://file${index}`,
246
+ ...(index !== 0 ||
247
+ payload.caption === undefined ||
248
+ payload.caption === ''
249
+ ? {}
250
+ : {
251
+ caption: payload.caption,
252
+ ...(payload.parseMode === undefined
253
+ ? {}
254
+ : { parse_mode: payload.parseMode }),
255
+ }),
256
+ }
257
+ })
258
+ // `sendMediaGroup` accepts no caption, parse_mode or reply_markup fields: the
259
+ // caption rides the first media item.
260
+ form.set('chat_id', payload.chatId)
261
+ if (threadId !== undefined) form.set('message_thread_id', String(threadId))
262
+ if (payload.replyTo !== undefined) {
263
+ form.set(
264
+ 'reply_parameters',
265
+ JSON.stringify({
266
+ message_id: payload.replyTo,
267
+ allow_sending_without_reply: true,
268
+ }),
269
+ )
270
+ }
271
+ form.set('media', JSON.stringify(media))
272
+ const messages = await deps.sendMediaGroup(form, signal)
273
+ const head = messages[0]
274
+ if (head === undefined)
275
+ throw new PlatformError(
276
+ 'invalid',
277
+ 'telegram: sendMediaGroup returned no messages',
278
+ )
279
+ const part = partOf(
280
+ head,
281
+ items.map((item) => item.key),
282
+ )
283
+ // `partOf` refused an answer without an id, so the head carries one.
284
+ const headId = head.message_id!
285
+ const aliases = messages
286
+ .slice(1)
287
+ .flatMap((message) =>
288
+ message.message_id === undefined ? [] : [String(message.message_id)],
289
+ )
290
+ // An album carries no keyboard, so a keyboard message answers its first item instead of being dropped.
291
+ if (payload.buttons !== undefined && payload.buttons.length > 0) {
292
+ let keyboard: Message
293
+ try {
294
+ keyboard = await deps.internal.sendMessage(
295
+ {
296
+ chat_id: payload.chatId,
297
+ // The caption already rides the first album item; the keyboard message carries no body of its own.
298
+ text: '\u2060',
299
+ ...(payload.parseMode === undefined
300
+ ? {}
301
+ : { parse_mode: payload.parseMode }),
302
+ ...(threadId === undefined ? {} : { message_thread_id: threadId }),
303
+ reply_parameters: {
304
+ message_id: headId,
305
+ allow_sending_without_reply: true,
306
+ },
307
+ reply_markup: { inline_keyboard: payload.buttons },
308
+ },
309
+ signal,
310
+ )
311
+ } catch (error) {
312
+ // The album is already on the platform, so the outcome is unknown even when Telegram refused the keyboard.
313
+ const cause = classifyTelegramError(error)
314
+ throw new PlatformError(cause.kind, cause.message, {
315
+ ...(cause.code === undefined ? {} : { code: cause.code }),
316
+ ...(cause.retryAfterMs === undefined
317
+ ? {}
318
+ : { retryAfterMs: cause.retryAfterMs }),
319
+ published: 'unknown',
320
+ cause,
321
+ })
322
+ }
323
+ aliases.push(String(keyboard.message_id))
324
+ }
325
+ return aliases.length === 0 ? part : { ...part, aliases }
326
+ }
327
+
328
+ const form = new FormData()
329
+ appendCommon(form, payload, threadId)
330
+ form.append(
331
+ mediaField(first.kind),
332
+ blobOf(stagedOf(deps, first.key)),
333
+ first.name || 'file',
334
+ )
335
+ const message = await deps.sendMedia(sendMethod(first.kind), form, signal)
336
+ return partOf(message, [first.key])
337
+ }
338
+
339
+ /**
340
+ * Edit one published message in place.
341
+ *
342
+ * Text messages use `editMessageText`; media messages edit their caption. An
343
+ * edit cannot replace the media, and a caption edit without a caption or a
344
+ * keyboard would publish nothing, so both are refused for the caller to
345
+ * withdraw and resend.
346
+ *
347
+ * @param deps - adapter services.
348
+ * @param part - the published part.
349
+ * @param payload - the freshly serialized draft.
350
+ * @param handle - the message handle (carries the target chat).
351
+ * @param signal - cancellation.
352
+ */
353
+ export async function editPayload(
354
+ deps: TelegramSendDeps,
355
+ part: SentPart,
356
+ payload: TelegramPayload,
357
+ handle: MessageHandle,
358
+ signal?: AbortSignal,
359
+ ): Promise<void> {
360
+ const messageId = Number(part.id)
361
+ if (!Number.isSafeInteger(messageId)) {
362
+ throw new PlatformError(
363
+ 'invalid',
364
+ `telegram: "${part.id}" is not a Telegram message id`,
365
+ )
366
+ }
367
+ const markup =
368
+ payload.buttons === undefined
369
+ ? { inline_keyboard: [] }
370
+ : { inline_keyboard: payload.buttons }
371
+ try {
372
+ if (payload.kind === 'text') {
373
+ await deps.internal.editMessageText(
374
+ {
375
+ chat_id: handle.target.chat.id,
376
+ message_id: messageId,
377
+ text: payload.text ?? '',
378
+ ...(payload.parseMode === undefined
379
+ ? {}
380
+ : { parse_mode: payload.parseMode }),
381
+ reply_markup: markup,
382
+ ...(deps.config.linkPreview
383
+ ? {}
384
+ : { link_preview_options: { is_disabled: true } }),
385
+ },
386
+ signal,
387
+ )
388
+ return
389
+ }
390
+ const caption = payload.caption ?? ''
391
+ const recorded = recordedMediaKeys(part)
392
+ if (recorded !== undefined) {
393
+ const edited = (payload.media ?? []).map((item) => item.key)
394
+ if (!sameMediaKeys(recorded, edited)) {
395
+ throw new PlatformError(
396
+ 'invalid',
397
+ 'telegram: an edit cannot replace media; withdraw and resend the message instead',
398
+ { published: 'no' },
399
+ )
400
+ }
401
+ }
402
+ // Telegram has no empty-caption edit: an empty caption with no keyboard would silently do nothing.
403
+ if (
404
+ caption === '' &&
405
+ (payload.buttons === undefined || payload.buttons.length === 0)
406
+ ) {
407
+ throw new PlatformError(
408
+ 'invalid',
409
+ 'telegram: a media edit needs a caption or a keyboard; withdraw and resend the message instead',
410
+ { published: 'no' },
411
+ )
412
+ }
413
+ await deps.internal.editMessageCaption(
414
+ {
415
+ chat_id: handle.target.chat.id,
416
+ message_id: messageId,
417
+ caption,
418
+ ...(payload.parseMode === undefined
419
+ ? {}
420
+ : { parse_mode: payload.parseMode }),
421
+ reply_markup: markup,
422
+ },
423
+ signal,
424
+ )
425
+ } catch (error) {
426
+ if (isNotModified(error)) return
427
+ throw error
428
+ }
429
+ }
430
+
431
+ /**
432
+ * Delete one published message.
433
+ *
434
+ * @param deps - adapter services.
435
+ * @param part - the published part.
436
+ * @param handle - the message handle (carries the target chat).
437
+ */
438
+ export async function deletePart(
439
+ deps: TelegramSendDeps,
440
+ part: SentPart,
441
+ handle: MessageHandle,
442
+ ): Promise<void> {
443
+ const messageId = Number(part.id)
444
+ if (!Number.isSafeInteger(messageId)) {
445
+ throw new PlatformError(
446
+ 'invalid',
447
+ `telegram: "${part.id}" is not a Telegram message id`,
448
+ )
449
+ }
450
+ await deps.internal.deleteMessage({
451
+ chat_id: handle.target.chat.id,
452
+ message_id: messageId,
453
+ })
454
+ }
@@ -0,0 +1,2 @@
1
+ rules:
2
+ max-len: off
@@ -0,0 +1,112 @@
1
+ import { Animation, InlineKeyboardMarkup, Integer, Internal, Message, MessageEntity, PhotoSize, User } from './index.ts'
2
+
3
+ export interface SendGamePayload {
4
+ /** Unique identifier for the target chat */
5
+ chat_id?: Integer
6
+ /** Unique identifier for the target message thread (topic) of the forum; for forum supergroups only */
7
+ message_thread_id?: number
8
+ /** Short name of the game, serves as the unique identifier for the game. Set up your games via Botfather. */
9
+ game_short_name?: string
10
+ /** Sends the message silently. Users will receive a notification with no sound. */
11
+ disable_notification?: boolean
12
+ /** Protects the contents of the sent message from forwarding and saving */
13
+ protect_content?: boolean
14
+ /** If the message is a reply, ID of the original message */
15
+ reply_to_message_id?: Integer
16
+ /** Pass True, if the message should be sent even if the specified replied-to message is not found */
17
+ allow_sending_without_reply?: boolean
18
+ /** A JSON-serialized object for an inline keyboard. If empty, one 'Play game_title' button will be shown. If not empty, the first button must launch the game. */
19
+ reply_markup?: InlineKeyboardMarkup
20
+ }
21
+
22
+ /**
23
+ * This object represents a game. Use BotFather to create and edit games, their short names will act as unique identifiers.
24
+ * @see https://core.telegram.org/bots/api#game
25
+ */
26
+ export interface Game {
27
+ /** Title of the game */
28
+ title?: string
29
+ /** Description of the game */
30
+ description?: string
31
+ /** Photo that will be displayed in the game message in chats. */
32
+ photo?: PhotoSize[]
33
+ /** Optional. Brief description of the game or high scores included in the game message. Can be automatically edited to include current high scores for the game when the bot calls setGameScore, or manually edited using editMessageText. 0-4096 characters. */
34
+ text?: string
35
+ /** Optional. Special entities that appear in text, such as usernames, URLs, bot commands, etc. */
36
+ text_entities?: MessageEntity[]
37
+ /** Optional. Animation that will be displayed in the game message in chats. Upload via BotFather */
38
+ animation?: Animation
39
+ }
40
+
41
+ /**
42
+ * A placeholder, currently holds no information. Use BotFather to set up your game.
43
+ * @see https://core.telegram.org/bots/api#callbackgame
44
+ */
45
+ export type CallbackGame = any
46
+
47
+ export interface SetGameScorePayload {
48
+ /** User identifier */
49
+ user_id?: Integer
50
+ /** New score, must be non-negative */
51
+ score?: Integer
52
+ /** Pass True, if the high score is allowed to decrease. This can be useful when fixing mistakes or banning cheaters */
53
+ force?: boolean
54
+ /** Pass True, if the game message should not be automatically edited to include the current scoreboard */
55
+ disable_edit_message?: boolean
56
+ /** Required if inline_message_id is not specified. Unique identifier for the target chat */
57
+ chat_id?: Integer
58
+ /** Required if inline_message_id is not specified. Identifier of the sent message */
59
+ message_id?: Integer
60
+ /** Required if chat_id and message_id are not specified. Identifier of the inline message */
61
+ inline_message_id?: string
62
+ }
63
+
64
+ export interface GetGameHighScoresPayload {
65
+ /** Target user id */
66
+ user_id?: Integer
67
+ /** Required if inline_message_id is not specified. Unique identifier for the target chat */
68
+ chat_id?: Integer
69
+ /** Required if inline_message_id is not specified. Identifier of the sent message */
70
+ message_id?: Integer
71
+ /** Required if chat_id and message_id are not specified. Identifier of the inline message */
72
+ inline_message_id?: string
73
+ }
74
+
75
+ /**
76
+ * This object represents one row of the high scores table for a game.
77
+ * @see https://core.telegram.org/bots/api#gamehighscore
78
+ */
79
+ export interface GameHighScore {
80
+ /** Position in high score table for the game */
81
+ position?: Integer
82
+ /** User */
83
+ user?: User
84
+ /** Score */
85
+ score?: Integer
86
+ }
87
+
88
+ declare module './internal.ts' {
89
+ interface Internal {
90
+ /**
91
+ * Use this method to send a game. On success, the sent Message is returned.
92
+ * @see https://core.telegram.org/bots/api#sendgame
93
+ */
94
+ sendGame(payload: SendGamePayload): Promise<Message>
95
+ /**
96
+ * Use this method to set the score of the specified user in a game message. On success, if the message is not an inline message, the Message is returned, otherwise True is returned. Returns an error, if the new score is not greater than the user's current score in the chat and force is False.
97
+ * @see https://core.telegram.org/bots/api#setgamescore
98
+ */
99
+ setGameScore(payload: SetGameScorePayload): Promise<Message | boolean>
100
+ /**
101
+ * Use this method to get data for high score tables. Will return the score of the specified user and several of their neighbors in a game. On success, returns an GameHighScore objects.
102
+ *
103
+ * This method will currently return scores for the target user, plus two of their closest neighbors on each side. Will also return the top three users if the user and his neighbors are not among them. Please note that this behavior is subject to change.
104
+ * @see https://core.telegram.org/bots/api#getgamehighscores
105
+ */
106
+ getGameHighScores(payload: GetGameHighScoresPayload): Promise<GameHighScore>
107
+ }
108
+ }
109
+
110
+ Internal.define('sendGame')
111
+ Internal.define('setGameScore')
112
+ Internal.define('getGameHighScores')