teledzik 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +1126 -0
  3. package/filters.d.ts +1 -0
  4. package/filters.js +1 -0
  5. package/format.d.ts +1 -0
  6. package/format.js +1 -0
  7. package/future.d.ts +1 -0
  8. package/future.js +1 -0
  9. package/lib/button.js +101 -0
  10. package/lib/cli.mjs +105 -0
  11. package/lib/composer.js +581 -0
  12. package/lib/context.js +1300 -0
  13. package/lib/core/helpers/ai-stream.js +69 -0
  14. package/lib/core/helpers/args.js +58 -0
  15. package/lib/core/helpers/check.js +56 -0
  16. package/lib/core/helpers/compact.js +17 -0
  17. package/lib/core/helpers/deunionize.js +13 -0
  18. package/lib/core/helpers/formatting.js +89 -0
  19. package/lib/core/helpers/input-box.js +87 -0
  20. package/lib/core/helpers/rich-sanitizer.js +31 -0
  21. package/lib/core/helpers/smart-queue.js +70 -0
  22. package/lib/core/helpers/util.js +50 -0
  23. package/lib/core/network/client.js +318 -0
  24. package/lib/core/network/error.js +21 -0
  25. package/lib/core/network/multipart-stream.js +61 -0
  26. package/lib/core/network/polling.js +86 -0
  27. package/lib/core/network/webhook.js +54 -0
  28. package/lib/core/types/rich-message.js +287 -0
  29. package/lib/core/types/typegram.js +18 -0
  30. package/lib/filters.js +69 -0
  31. package/lib/format.js +38 -0
  32. package/lib/future.js +166 -0
  33. package/lib/index.js +61 -0
  34. package/lib/input.js +61 -0
  35. package/lib/markup.js +111 -0
  36. package/lib/middleware.js +2 -0
  37. package/lib/paginated.js +76 -0
  38. package/lib/reactions.js +81 -0
  39. package/lib/router.js +45 -0
  40. package/lib/scenes/base.js +39 -0
  41. package/lib/scenes/context.js +99 -0
  42. package/lib/scenes/form-scene.js +23 -0
  43. package/lib/scenes/index.js +21 -0
  44. package/lib/scenes/stage.js +49 -0
  45. package/lib/scenes/wizard/context.js +30 -0
  46. package/lib/scenes/wizard/index.js +45 -0
  47. package/lib/scenes.js +17 -0
  48. package/lib/session.js +163 -0
  49. package/lib/telegraf.js +274 -0
  50. package/lib/telegram-types.js +6 -0
  51. package/lib/telegram.js +1286 -0
  52. package/lib/types.js +2 -0
  53. package/lib/utils.js +5 -0
  54. package/markup.d.ts +1 -0
  55. package/markup.js +1 -0
  56. package/package.json +141 -0
  57. package/scenes.d.ts +1 -0
  58. package/scenes.js +1 -0
  59. package/session.d.ts +1 -0
  60. package/session.js +1 -0
  61. package/src/button.ts +182 -0
  62. package/src/composer.ts +1008 -0
  63. package/src/context.ts +1778 -0
  64. package/src/core/helpers/ai-stream.ts +96 -0
  65. package/src/core/helpers/args.ts +63 -0
  66. package/src/core/helpers/check.ts +71 -0
  67. package/src/core/helpers/compact.ts +18 -0
  68. package/src/core/helpers/deunionize.ts +26 -0
  69. package/src/core/helpers/formatting.ts +119 -0
  70. package/src/core/helpers/input-box.ts +86 -0
  71. package/src/core/helpers/rich-sanitizer.ts +36 -0
  72. package/src/core/helpers/smart-queue.ts +89 -0
  73. package/src/core/helpers/util.ts +96 -0
  74. package/src/core/network/client.ts +396 -0
  75. package/src/core/network/error.ts +29 -0
  76. package/src/core/network/multipart-stream.ts +45 -0
  77. package/src/core/network/polling.ts +94 -0
  78. package/src/core/network/webhook.ts +58 -0
  79. package/src/core/types/rich-message.ts +375 -0
  80. package/src/core/types/typegram.ts +44 -0
  81. package/src/filters.js +69 -0
  82. package/src/filters.ts +109 -0
  83. package/src/format.js +38 -0
  84. package/src/format.ts +110 -0
  85. package/src/future.js +147 -0
  86. package/src/future.ts +231 -0
  87. package/src/index.ts +27 -0
  88. package/src/input.ts +59 -0
  89. package/src/markup.js +93 -0
  90. package/src/markup.ts +142 -0
  91. package/src/middleware.ts +24 -0
  92. package/src/paginated.ts +75 -0
  93. package/src/reactions.ts +118 -0
  94. package/src/router.ts +55 -0
  95. package/src/scenes/base.ts +52 -0
  96. package/src/scenes/context.ts +136 -0
  97. package/src/scenes/form-scene.ts +34 -0
  98. package/src/scenes/index.ts +21 -0
  99. package/src/scenes/stage.ts +71 -0
  100. package/src/scenes/wizard/context.ts +58 -0
  101. package/src/scenes/wizard/index.ts +63 -0
  102. package/src/scenes.js +17 -0
  103. package/src/scenes.ts +1 -0
  104. package/src/session.js +175 -0
  105. package/src/session.ts +204 -0
  106. package/src/telegraf.ts +385 -0
  107. package/src/telegram-types.ts +256 -0
  108. package/src/telegram.ts +1700 -0
  109. package/src/types.js +2 -0
  110. package/src/types.ts +2 -0
  111. package/src/utils.js +5 -0
  112. package/src/utils.ts +1 -0
  113. package/types.d.ts +1 -0
  114. package/types.js +1 -0
  115. package/typings/button.d.ts +35 -0
  116. package/typings/button.d.ts.map +1 -0
  117. package/typings/composer.d.ts +226 -0
  118. package/typings/composer.d.ts.map +1 -0
  119. package/typings/context.d.ts +711 -0
  120. package/typings/context.d.ts.map +1 -0
  121. package/typings/core/helpers/ai-stream.d.ts +33 -0
  122. package/typings/core/helpers/args.d.ts +10 -0
  123. package/typings/core/helpers/args.d.ts.map +1 -0
  124. package/typings/core/helpers/check.d.ts +55 -0
  125. package/typings/core/helpers/check.d.ts.map +1 -0
  126. package/typings/core/helpers/compact.d.ts +3 -0
  127. package/typings/core/helpers/compact.d.ts.map +1 -0
  128. package/typings/core/helpers/deunionize.d.ts +17 -0
  129. package/typings/core/helpers/deunionize.d.ts.map +1 -0
  130. package/typings/core/helpers/formatting.d.ts +29 -0
  131. package/typings/core/helpers/formatting.d.ts.map +1 -0
  132. package/typings/core/helpers/input-box.d.ts +32 -0
  133. package/typings/core/helpers/rich-sanitizer.d.ts +5 -0
  134. package/typings/core/helpers/smart-queue.d.ts +18 -0
  135. package/typings/core/helpers/util.d.ts +26 -0
  136. package/typings/core/helpers/util.d.ts.map +1 -0
  137. package/typings/core/network/client.d.ts +54 -0
  138. package/typings/core/network/client.d.ts.map +1 -0
  139. package/typings/core/network/error.d.ts +15 -0
  140. package/typings/core/network/error.d.ts.map +1 -0
  141. package/typings/core/network/multipart-stream.d.ts +19 -0
  142. package/typings/core/network/multipart-stream.d.ts.map +1 -0
  143. package/typings/core/network/polling.d.ts +15 -0
  144. package/typings/core/network/polling.d.ts.map +1 -0
  145. package/typings/core/network/webhook.d.ts +6 -0
  146. package/typings/core/network/webhook.d.ts.map +1 -0
  147. package/typings/core/types/rich-message.d.ts +143 -0
  148. package/typings/core/types/rich-message.d.ts.map +1 -0
  149. package/typings/core/types/typegram.d.ts +36 -0
  150. package/typings/core/types/typegram.d.ts.map +1 -0
  151. package/typings/filters.d.ts +17 -0
  152. package/typings/filters.d.ts.map +1 -0
  153. package/typings/format.d.ts +21 -0
  154. package/typings/format.d.ts.map +1 -0
  155. package/typings/future.d.ts +11 -0
  156. package/typings/future.d.ts.map +1 -0
  157. package/typings/index.d.ts +21 -0
  158. package/typings/index.d.ts.map +1 -0
  159. package/typings/input.d.ts +58 -0
  160. package/typings/input.d.ts.map +1 -0
  161. package/typings/markup.d.ts +26 -0
  162. package/typings/markup.d.ts.map +1 -0
  163. package/typings/middleware.d.ts +7 -0
  164. package/typings/middleware.d.ts.map +1 -0
  165. package/typings/paginated.d.ts +20 -0
  166. package/typings/reactions.d.ts +31 -0
  167. package/typings/reactions.d.ts.map +1 -0
  168. package/typings/router.d.ts +20 -0
  169. package/typings/router.d.ts.map +1 -0
  170. package/typings/scenes/base.d.ts +21 -0
  171. package/typings/scenes/base.d.ts.map +1 -0
  172. package/typings/scenes/context.d.ts +35 -0
  173. package/typings/scenes/context.d.ts.map +1 -0
  174. package/typings/scenes/form-scene.d.ts +17 -0
  175. package/typings/scenes/index.d.ts +10 -0
  176. package/typings/scenes/index.d.ts.map +1 -0
  177. package/typings/scenes/stage.d.ts +23 -0
  178. package/typings/scenes/stage.d.ts.map +1 -0
  179. package/typings/scenes/wizard/context.d.ts +28 -0
  180. package/typings/scenes/wizard/context.d.ts.map +1 -0
  181. package/typings/scenes/wizard/index.d.ts +15 -0
  182. package/typings/scenes/wizard/index.d.ts.map +1 -0
  183. package/typings/scenes.d.ts +1 -0
  184. package/typings/scenes.d.ts.map +1 -0
  185. package/typings/session.d.ts +54 -0
  186. package/typings/session.d.ts.map +1 -0
  187. package/typings/telegraf.d.ts +118 -0
  188. package/typings/telegraf.d.ts.map +1 -0
  189. package/typings/telegram-types.d.ts +140 -0
  190. package/typings/telegram-types.d.ts.map +1 -0
  191. package/typings/telegram.d.ts +697 -0
  192. package/typings/telegram.d.ts.map +1 -0
  193. package/typings/types.d.ts +2 -0
  194. package/typings/types.d.ts.map +1 -0
  195. package/typings/utils.d.ts +1 -0
  196. package/typings/utils.d.ts.map +1 -0
  197. package/utils.d.ts +1 -0
  198. package/utils.js +1 -0
@@ -0,0 +1,58 @@
1
+ import * as http from 'http'
2
+ import d from 'debug'
3
+ import { type Update } from '../types/typegram'
4
+ const debug = d('telegraf:webhook')
5
+
6
+ export default function generateWebhook(
7
+ filter: (req: http.IncomingMessage) => boolean,
8
+ updateHandler: (update: Update, res: http.ServerResponse) => Promise<void>
9
+ ) {
10
+ return async (
11
+ req: http.IncomingMessage & { body?: Update },
12
+ res: http.ServerResponse,
13
+ next = (): void => {
14
+ res.statusCode = 403
15
+ debug('Replying with status code', res.statusCode)
16
+ res.end()
17
+ }
18
+ ): Promise<void> => {
19
+ debug('Incoming request', req.method, req.url)
20
+
21
+ if (!filter(req)) {
22
+ debug('Webhook filter failed', req.method, req.url)
23
+ return next()
24
+ }
25
+
26
+ let update: Update
27
+
28
+ try {
29
+ if (req.body != null) {
30
+ /* If req.body is already set, we expect it to be the parsed
31
+ request body (update object) received from Telegram
32
+ However, some libraries such as `serverless-http` set req.body to the
33
+ raw buffer, so we'll handle that additionally */
34
+
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
+ let body: any = req.body
37
+ // if body is Buffer, parse it into string
38
+ if (body instanceof Buffer) body = String(req.body)
39
+ // if body is string, parse it into object
40
+ if (typeof body === 'string') body = JSON.parse(body)
41
+ update = body
42
+ } else {
43
+ let body = ''
44
+ // parse each buffer to string and append to body
45
+ for await (const chunk of req) body += String(chunk)
46
+ // parse body to object
47
+ update = JSON.parse(body)
48
+ }
49
+ } catch (error: unknown) {
50
+ // if any of the parsing steps fails, give up and respond with error
51
+ res.writeHead(415).end()
52
+ debug('Failed to parse request body:', error)
53
+ return
54
+ }
55
+
56
+ return await updateHandler(update, res)
57
+ }
58
+ }
@@ -0,0 +1,375 @@
1
+ /**
2
+ * Telegram Bot API 10.1 — Rich Messages
3
+ * https://core.telegram.org/bots/api#june-11-2026
4
+ */
5
+
6
+ // ---------------------------------------------------------------------------
7
+ // Core types
8
+ // ---------------------------------------------------------------------------
9
+
10
+ /**
11
+ * Rich formatted message received from Telegram.
12
+ * The `blocks` array is rendered server-side; to send, use InputRichMessage.
13
+ */
14
+ export interface RichMessage {
15
+ blocks: object[]
16
+ is_rtl?: boolean
17
+ }
18
+
19
+ /**
20
+ * Describes a rich message to send.
21
+ * Exactly ONE of `html` or `markdown` must be specified.
22
+ */
23
+ export interface InputRichMessage {
24
+ html?: string
25
+ markdown?: string
26
+ is_rtl?: boolean
27
+ skip_entity_detection?: boolean
28
+ }
29
+
30
+ /** Can be used as InputMessageContent in inline, guest, and Web App queries. */
31
+ export interface InputRichMessageContent {
32
+ rich_message: InputRichMessage
33
+ }
34
+
35
+ // ---------------------------------------------------------------------------
36
+ // HTML Builder
37
+ // ---------------------------------------------------------------------------
38
+
39
+ /**
40
+ * Fluent builder for Rich Messages using HTML format.
41
+ *
42
+ * @example
43
+ * const msg = new RichHTMLBuilder()
44
+ * .heading(1, 'Title')
45
+ * .paragraph(RichHTMLBuilder.bold('Hello') + ' world')
46
+ * .build()
47
+ */
48
+ export class RichHTMLBuilder {
49
+ private readonly _parts: string[] = []
50
+
51
+ // ── Inline static helpers ──────────────────────────────────────────────
52
+
53
+ static bold(t: string) { return `<b>${t}</b>` }
54
+ static italic(t: string) { return `<i>${t}</i>` }
55
+ static underline(t: string) { return `<u>${t}</u>` }
56
+ static strikethrough(t: string) { return `<s>${t}</s>` }
57
+ static spoiler(t: string) { return `<tg-spoiler>${t}</tg-spoiler>` }
58
+ static code(t: string) { return `<code>${t}</code>` }
59
+ static marked(t: string) { return `<mark>${t}</mark>` }
60
+ static sub(t: string) { return `<sub>${t}</sub>` }
61
+ static sup(t: string) { return `<sup>${t}</sup>` }
62
+ static url(href: string, text: string) { return `<a href="${href}">${text}</a>` }
63
+ static email(email: string, text?: string) { return `<a href="mailto:${email}">${text ?? email}</a>` }
64
+ static phone(phone: string, text?: string) { return `<a href="tel:${phone}">${text ?? phone}</a>` }
65
+ static mention(userId: number, text: string) { return `<a href="tg://user?id=${userId}">${text}</a>` }
66
+ static customEmoji(emojiId: string, fallback: string) { return `<tg-emoji emoji-id="${emojiId}">${fallback}</tg-emoji>` }
67
+ static time(unix: number, format: string, text: string) { return `<tg-time unix="${unix}" format="${format}">${text}</tg-time>` }
68
+ static inlineMath(expression: string) { return `$${expression}$` }
69
+ static anchor(name: string) { return `<a name="${name}"></a>` }
70
+ static anchorLink(name: string, text: string) { return `<a href="#${name}">${text}</a>` }
71
+ static ref(id: string, text: string) { return `<a href="#${id}">${text}</a>` }
72
+
73
+ // ── Block methods ──────────────────────────────────────────────────────
74
+
75
+ heading(level: 1 | 2 | 3 | 4 | 5 | 6, html: string) {
76
+ this._parts.push(`<h${level}>${html}</h${level}>`)
77
+ return this
78
+ }
79
+
80
+ paragraph(html: string) {
81
+ this._parts.push(`<p>${html}</p>`)
82
+ return this
83
+ }
84
+
85
+ pre(code: string, language?: string) {
86
+ this._parts.push(
87
+ language
88
+ ? `<pre><code class="language-${language}">${code}</code></pre>`
89
+ : `<pre>${code}</pre>`
90
+ )
91
+ return this
92
+ }
93
+
94
+ footer(html: string) {
95
+ this._parts.push(`<footer>${html}</footer>`)
96
+ return this
97
+ }
98
+
99
+ divider() {
100
+ this._parts.push('<hr/>')
101
+ return this
102
+ }
103
+
104
+ ul(...items: string[]) {
105
+ this._parts.push(`<ul>${items.map((i) => `<li>${i}</li>`).join('')}</ul>`)
106
+ return this
107
+ }
108
+
109
+ ol(...items: string[]) {
110
+ this._parts.push(`<ol>${items.map((i) => `<li>${i}</li>`).join('')}</ol>`)
111
+ return this
112
+ }
113
+
114
+ taskList(...items: Array<{ text: string; checked?: boolean }>) {
115
+ const lis = items.map(
116
+ (i) => `<li><input type="checkbox"${i.checked ? ' checked' : ''}>${i.text}</li>`
117
+ )
118
+ this._parts.push(`<ul>${lis.join('')}</ul>`)
119
+ return this
120
+ }
121
+
122
+ blockQuote(html: string) {
123
+ this._parts.push(`<blockquote>${html}</blockquote>`)
124
+ return this
125
+ }
126
+
127
+ pullQuote(html: string, cite?: string) {
128
+ this._parts.push(`<aside>${html}${cite ? `<cite>${cite}</cite>` : ''}</aside>`)
129
+ return this
130
+ }
131
+
132
+ photo(src: string, caption?: string, spoiler?: boolean) {
133
+ const img = `<img src="${src}"${spoiler ? ' tg-spoiler' : ''}/>`
134
+ this._parts.push(caption ? `<figure>${img}<figcaption>${caption}</figcaption></figure>` : img)
135
+ return this
136
+ }
137
+
138
+ video(src: string, caption?: string, spoiler?: boolean) {
139
+ const vid = `<video src="${src}"${spoiler ? ' tg-spoiler' : ''}></video>`
140
+ this._parts.push(caption ? `<figure>${vid}<figcaption>${caption}</figcaption></figure>` : vid)
141
+ return this
142
+ }
143
+
144
+ audio(src: string, caption?: string) {
145
+ const aud = `<audio src="${src}"></audio>`
146
+ this._parts.push(caption ? `<figure>${aud}<figcaption>${caption}</figcaption></figure>` : aud)
147
+ return this
148
+ }
149
+
150
+ map(lat: number, long: number, zoom?: number, caption?: string) {
151
+ const tag = `<tg-map lat="${lat}" long="${long}"${zoom != null ? ` zoom="${zoom}"` : ''}/>`
152
+ this._parts.push(caption ? `<figure>${tag}<figcaption>${caption}</figcaption></figure>` : tag)
153
+ return this
154
+ }
155
+
156
+ collage(...media: string[]) {
157
+ this._parts.push(`<tg-collage>${media.join('')}</tg-collage>`)
158
+ return this
159
+ }
160
+
161
+ slideshow(...media: string[]) {
162
+ this._parts.push(`<tg-slideshow>${media.join('')}</tg-slideshow>`)
163
+ return this
164
+ }
165
+
166
+ table(
167
+ rows: string[][],
168
+ options?: { bordered?: boolean; striped?: boolean; hasHeader?: boolean }
169
+ ) {
170
+ const attrs = [options?.bordered && 'bordered', options?.striped && 'striped']
171
+ .filter(Boolean)
172
+ .join(' ')
173
+ const [header, ...body] = rows
174
+ const headHtml = options?.hasHeader && header
175
+ ? `<tr>${header.map((c) => `<th>${c}</th>`).join('')}</tr>`
176
+ : ''
177
+ const bodyRows = (options?.hasHeader ? body : rows)
178
+ .map((r) => `<tr>${r.map((c) => `<td>${c}</td>`).join('')}</tr>`)
179
+ .join('')
180
+ this._parts.push(`<table${attrs ? ` ${attrs}` : ''}>${headHtml}${bodyRows}</table>`)
181
+ return this
182
+ }
183
+
184
+ details(summary: string, content: string, open?: boolean) {
185
+ this._parts.push(
186
+ `<details${open ? ' open' : ''}><summary>${summary}</summary>${content}</details>`
187
+ )
188
+ return this
189
+ }
190
+
191
+ mathBlock(expression: string) {
192
+ this._parts.push(`<tg-math-block>${expression}</tg-math-block>`)
193
+ return this
194
+ }
195
+
196
+ thinking(html: string) {
197
+ this._parts.push(`<tg-thinking>${html}</tg-thinking>`)
198
+ return this
199
+ }
200
+
201
+ referenceDefinition(id: string, html: string) {
202
+ this._parts.push(`<tg-reference name="${id}">${html}</tg-reference>`)
203
+ return this
204
+ }
205
+
206
+ raw(html: string) {
207
+ this._parts.push(html)
208
+ return this
209
+ }
210
+
211
+ build(options?: Pick<InputRichMessage, 'is_rtl' | 'skip_entity_detection'>): InputRichMessage {
212
+ return { html: this._parts.join('\n'), ...options }
213
+ }
214
+ }
215
+
216
+ // ---------------------------------------------------------------------------
217
+ // Markdown Builder
218
+ // ---------------------------------------------------------------------------
219
+
220
+ /**
221
+ * Fluent builder for Rich Messages using Markdown format.
222
+ *
223
+ * @example
224
+ * const msg = new RichMarkdownBuilder()
225
+ * .heading(1, 'Title')
226
+ * .paragraph(RichMarkdownBuilder.bold('Hello') + ' world')
227
+ * .build()
228
+ */
229
+ export class RichMarkdownBuilder {
230
+ private readonly _parts: string[] = []
231
+
232
+ // ── Inline static helpers ──────────────────────────────────────────────
233
+
234
+ static bold(t: string) { return `**${t}**` }
235
+ static italic(t: string) { return `*${t}*` }
236
+ static underline(t: string) { return `<u>${t}</u>` }
237
+ static strikethrough(t: string) { return `~~${t}~~` }
238
+ static spoiler(t: string) { return `||${t}||` }
239
+ static code(t: string) { return `\`${t}\`` }
240
+ static marked(t: string) { return `==${t}==` }
241
+ static sub(t: string) { return `<sub>${t}</sub>` }
242
+ static sup(t: string) { return `<sup>${t}</sup>` }
243
+ static url(href: string, text: string) { return `[${text}](${href})` }
244
+ static email(email: string, text?: string) { return `[${text ?? email}](mailto:${email})` }
245
+ static phone(phone: string, text?: string) { return `[${text ?? phone}](tel:${phone})` }
246
+ static mention(userId: number, text: string) { return `[${text}](tg://user?id=${userId})` }
247
+ static customEmoji(emojiId: string, fallback: string) { return `![${fallback}](tg://emoji?id=${emojiId})` }
248
+ static time(unix: number, format: string) { return `![](tg://time?unix=${unix}&format=${format})` }
249
+ static inlineMath(expression: string) { return `$${expression}$` }
250
+
251
+ // ── Block methods ──────────────────────────────────────────────────────
252
+
253
+ heading(level: 1 | 2 | 3 | 4 | 5 | 6, text: string) {
254
+ this._parts.push(`${'#'.repeat(level)} ${text}`)
255
+ return this
256
+ }
257
+
258
+ paragraph(text: string) {
259
+ this._parts.push(text)
260
+ return this
261
+ }
262
+
263
+ pre(code: string, language?: string) {
264
+ this._parts.push(`\`\`\`${language ?? ''}\n${code}\n\`\`\``)
265
+ return this
266
+ }
267
+
268
+ divider() {
269
+ this._parts.push('---')
270
+ return this
271
+ }
272
+
273
+ ul(...items: string[]) {
274
+ this._parts.push(items.map((i) => `- ${i}`).join('\n'))
275
+ return this
276
+ }
277
+
278
+ ol(...items: string[]) {
279
+ this._parts.push(items.map((i, idx) => `${idx + 1}. ${i}`).join('\n'))
280
+ return this
281
+ }
282
+
283
+ taskList(...items: Array<{ text: string; checked?: boolean }>) {
284
+ this._parts.push(items.map((i) => `- [${i.checked ? 'x' : ' '}] ${i.text}`).join('\n'))
285
+ return this
286
+ }
287
+
288
+ blockQuote(...lines: string[]) {
289
+ this._parts.push(lines.map((l) => `>${l}`).join('\n'))
290
+ return this
291
+ }
292
+
293
+ photo(src: string, caption?: string) {
294
+ this._parts.push(`![](${src}${caption ? ` "${caption}"` : ''})`)
295
+ return this
296
+ }
297
+
298
+ video(src: string, caption?: string) {
299
+ this._parts.push(`![](${src}${caption ? ` "${caption}"` : ''})`)
300
+ return this
301
+ }
302
+
303
+ audio(src: string, caption?: string) {
304
+ this._parts.push(`![](${src}${caption ? ` "${caption}"` : ''})`)
305
+ return this
306
+ }
307
+
308
+ table(headers: string[], rows: string[][], alignment?: Array<'left' | 'center' | 'right'>) {
309
+ const sep = headers.map((_, i) => {
310
+ const a = alignment?.[i]
311
+ if (a === 'center') return ':---:'
312
+ if (a === 'right') return '---:'
313
+ return ':---'
314
+ })
315
+ const lines = [
316
+ `| ${headers.join(' | ')} |`,
317
+ `|${sep.map((s) => s + '-').join('|')}|`,
318
+ ...rows.map((r) => `| ${r.join(' | ')} |`),
319
+ ]
320
+ this._parts.push(lines.join('\n'))
321
+ return this
322
+ }
323
+
324
+ footnoteRef(id: string) { return `[^${id}]` }
325
+
326
+ footnote(id: string, definition: string) {
327
+ this._parts.push(`[^${id}]: ${definition}`)
328
+ return this
329
+ }
330
+
331
+ mathBlock(expression: string) {
332
+ this._parts.push(`$$${expression}$$`)
333
+ return this
334
+ }
335
+
336
+ thinking(text: string) {
337
+ this._parts.push(`<tg-thinking>${text}</tg-thinking>`)
338
+ return this
339
+ }
340
+
341
+ collage(...mediaSrcs: string[]) {
342
+ this._parts.push(
343
+ `<tg-collage>\n${mediaSrcs.map((s) => `![](${s})`).join('\n')}\n</tg-collage>`
344
+ )
345
+ return this
346
+ }
347
+
348
+ slideshow(...mediaSrcs: string[]) {
349
+ this._parts.push(
350
+ `<tg-slideshow>\n${mediaSrcs.map((s) => `![](${s})`).join('\n')}\n</tg-slideshow>`
351
+ )
352
+ return this
353
+ }
354
+
355
+ details(summary: string, content: string, open?: boolean) {
356
+ this._parts.push(
357
+ `<details${open ? ' open' : ''}><summary>${summary}</summary>\n${content}\n</details>`
358
+ )
359
+ return this
360
+ }
361
+
362
+ raw(md: string) {
363
+ this._parts.push(md)
364
+ return this
365
+ }
366
+
367
+ build(options?: Pick<InputRichMessage, 'is_rtl' | 'skip_entity_detection'>): InputRichMessage {
368
+ return { markdown: this._parts.join('\n\n'), ...options }
369
+ }
370
+ }
371
+
372
+ /** Shorthand alias for RichHTMLBuilder */
373
+ export const H = RichHTMLBuilder
374
+ /** Shorthand alias for RichMarkdownBuilder */
375
+ export const MD = RichMarkdownBuilder
@@ -0,0 +1,44 @@
1
+ import * as Typegram from '@telegraf/types'
2
+ export * from '@telegraf/types'
3
+ export * from './rich-message'
4
+
5
+ // telegraf input file definition
6
+ interface InputFileByPath {
7
+ source: string
8
+ filename?: string
9
+ }
10
+ interface InputFileByReadableStream {
11
+ source: NodeJS.ReadableStream
12
+ filename?: string
13
+ }
14
+ interface InputFileByBuffer {
15
+ source: Buffer
16
+ filename?: string
17
+ }
18
+ interface InputFileByURL {
19
+ url: string
20
+ filename?: string
21
+ }
22
+ export type InputFile =
23
+ | InputFileByPath
24
+ | InputFileByReadableStream
25
+ | InputFileByBuffer
26
+ | InputFileByURL
27
+
28
+ export type Telegram = Typegram.ApiMethods<InputFile>
29
+
30
+ export type Opts<M extends keyof Telegram> = Typegram.Opts<InputFile>[M]
31
+ export type InputMedia = Typegram.InputMedia<InputFile>
32
+ export type InputMediaPhoto = Typegram.InputMediaPhoto<InputFile>
33
+ export type InputMediaVideo = Typegram.InputMediaVideo<InputFile>
34
+ export type InputMediaAnimation = Typegram.InputMediaAnimation<InputFile>
35
+ export type InputMediaAudio = Typegram.InputMediaAudio<InputFile>
36
+ export type InputMediaDocument = Typegram.InputMediaDocument<InputFile>
37
+
38
+ // tiny helper types
39
+ export type ChatAction = Opts<'sendChatAction'>['action']
40
+
41
+ /**
42
+ * Sending video notes by a URL is currently unsupported
43
+ */
44
+ export type InputFileVideoNote = Exclude<InputFile, InputFileByURL>
package/src/filters.js ADDED
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.allOf = exports.anyOf = exports.callbackQuery = exports.editedChannelPost = exports.channelPost = exports.editedMessage = exports.message = void 0;
4
+ const message = (...keys) => (update) => {
5
+ if (!('message' in update))
6
+ return false;
7
+ for (const key of keys) {
8
+ if (!(key in update.message))
9
+ return false;
10
+ }
11
+ return true;
12
+ };
13
+ exports.message = message;
14
+ const editedMessage = (...keys) => (update) => {
15
+ if (!('edited_message' in update))
16
+ return false;
17
+ for (const key of keys) {
18
+ if (!(key in update.edited_message))
19
+ return false;
20
+ }
21
+ return true;
22
+ };
23
+ exports.editedMessage = editedMessage;
24
+ const channelPost = (...keys) => (update) => {
25
+ if (!('channel_post' in update))
26
+ return false;
27
+ for (const key of keys) {
28
+ if (!(key in update.channel_post))
29
+ return false;
30
+ }
31
+ return true;
32
+ };
33
+ exports.channelPost = channelPost;
34
+ const editedChannelPost = (...keys) => (update) => {
35
+ if (!('edited_channel_post' in update))
36
+ return false;
37
+ for (const key of keys) {
38
+ if (!(key in update.edited_channel_post))
39
+ return false;
40
+ }
41
+ return true;
42
+ };
43
+ exports.editedChannelPost = editedChannelPost;
44
+ const callbackQuery = (...keys) => (update) => {
45
+ if (!('callback_query' in update))
46
+ return false;
47
+ for (const key of keys) {
48
+ if (!(key in update.callback_query))
49
+ return false;
50
+ }
51
+ return true;
52
+ };
53
+ exports.callbackQuery = callbackQuery;
54
+ /** Any of the provided filters must match */
55
+ const anyOf = (...filters) => (update) => {
56
+ for (const filter of filters)
57
+ if (filter(update))
58
+ return true;
59
+ return false;
60
+ };
61
+ exports.anyOf = anyOf;
62
+ /** All of the provided filters must match */
63
+ const allOf = (...filters) => (update) => {
64
+ for (const filter of filters)
65
+ if (!filter(update))
66
+ return false;
67
+ return true;
68
+ };
69
+ exports.allOf = allOf;
package/src/filters.ts ADDED
@@ -0,0 +1,109 @@
1
+ import type {
2
+ CallbackQuery,
3
+ CommonMessageBundle,
4
+ Message,
5
+ Update,
6
+ } from '@telegraf/types'
7
+ import { DistinctKeys, KeyedDistinct, Guarded } from './core/helpers/util'
8
+
9
+ export type Filter<U extends Update> = (update: Update) => update is U
10
+
11
+ export { Guarded }
12
+
13
+ export type AllGuarded<Fs extends Filter<Update>[]> = Fs extends [
14
+ infer A,
15
+ ...infer B,
16
+ ]
17
+ ? B extends []
18
+ ? Guarded<A>
19
+ : // TS doesn't know otherwise that B is Filter[]
20
+ B extends Filter<Update>[]
21
+ ? Guarded<A> & AllGuarded<B>
22
+ : never
23
+ : never
24
+
25
+ export const message =
26
+ <Ks extends DistinctKeys<Message>[]>(...keys: Ks) =>
27
+ (
28
+ update: Update
29
+ ): update is Update.MessageUpdate<KeyedDistinct<Message, Ks[number]>> => {
30
+ if (!('message' in update)) return false
31
+ for (const key of keys) {
32
+ if (!(key in update.message)) return false
33
+ }
34
+ return true
35
+ }
36
+
37
+ export const editedMessage =
38
+ <Ks extends DistinctKeys<CommonMessageBundle>[]>(...keys: Ks) =>
39
+ (
40
+ update: Update
41
+ ): update is Update.EditedMessageUpdate<
42
+ KeyedDistinct<CommonMessageBundle, Ks[number]>
43
+ > => {
44
+ if (!('edited_message' in update)) return false
45
+ for (const key of keys) {
46
+ if (!(key in update.edited_message)) return false
47
+ }
48
+ return true
49
+ }
50
+
51
+ export const channelPost =
52
+ <Ks extends DistinctKeys<Message>[]>(...keys: Ks) =>
53
+ (
54
+ update: Update
55
+ ): update is Update.ChannelPostUpdate<KeyedDistinct<Message, Ks[number]>> => {
56
+ if (!('channel_post' in update)) return false
57
+ for (const key of keys) {
58
+ if (!(key in update.channel_post)) return false
59
+ }
60
+ return true
61
+ }
62
+
63
+ export const editedChannelPost =
64
+ <Ks extends DistinctKeys<CommonMessageBundle>[]>(...keys: Ks) =>
65
+ (
66
+ update: Update
67
+ ): update is Update.EditedChannelPostUpdate<
68
+ KeyedDistinct<CommonMessageBundle, Ks[number]>
69
+ > => {
70
+ if (!('edited_channel_post' in update)) return false
71
+ for (const key of keys) {
72
+ if (!(key in update.edited_channel_post)) return false
73
+ }
74
+ return true
75
+ }
76
+
77
+ export const callbackQuery =
78
+ <Ks extends DistinctKeys<CallbackQuery>[]>(...keys: Ks) =>
79
+ (
80
+ update: Update
81
+ ): update is Update.CallbackQueryUpdate<
82
+ KeyedDistinct<CallbackQuery, Ks[number]>
83
+ > => {
84
+ if (!('callback_query' in update)) return false
85
+ for (const key of keys) {
86
+ if (!(key in update.callback_query)) return false
87
+ }
88
+ return true
89
+ }
90
+
91
+ /** Any of the provided filters must match */
92
+ export const anyOf =
93
+ <Us extends Update[]>(
94
+ ...filters: {
95
+ [UIdx in keyof Us]: Filter<Us[UIdx]>
96
+ }
97
+ ) =>
98
+ (update: Update): update is Us[number] => {
99
+ for (const filter of filters) if (filter(update)) return true
100
+ return false
101
+ }
102
+
103
+ /** All of the provided filters must match */
104
+ export const allOf =
105
+ <U extends Update, Fs extends Filter<U>[]>(...filters: Fs) =>
106
+ (update: Update): update is AllGuarded<Fs> => {
107
+ for (const filter of filters) if (!filter(update)) return false
108
+ return true
109
+ }
package/src/format.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mention = exports.link = exports.pre = exports.code = exports.quote = exports.underline = exports.strikethrough = exports.spoiler = exports.italic = exports.bold = exports.fmt = exports.join = exports.FmtString = void 0;
4
+ const formatting_1 = require("./core/helpers/formatting");
5
+ Object.defineProperty(exports, "FmtString", { enumerable: true, get: function () { return formatting_1.FmtString; } });
6
+ // Nests<A, B> means the function will return A, and it can nest B
7
+ // Nests<'fmt', string> means it will nest anything
8
+ // Nests<'code', never> means it will not nest anything
9
+ // Allowing everything to nest 'fmt' is a necessary evil; it allows to indirectly nest illegal entities
10
+ // Except for 'code' and 'pre', which don't nest anything anyway, so they only deal with strings
11
+ exports.join = formatting_1.join;
12
+ exports.fmt = (0, formatting_1.createFmt)();
13
+ exports.bold = (0, formatting_1.createFmt)('bold');
14
+ exports.italic = (0, formatting_1.createFmt)('italic');
15
+ exports.spoiler = (0, formatting_1.createFmt)('spoiler');
16
+ exports.strikethrough =
17
+ //
18
+ (0, formatting_1.createFmt)('strikethrough');
19
+ exports.underline =
20
+ //
21
+ (0, formatting_1.createFmt)('underline');
22
+ exports.quote =
23
+ //
24
+ (0, formatting_1.createFmt)('blockquote');
25
+ exports.code = (0, formatting_1.createFmt)('code');
26
+ const pre = (language) => (0, formatting_1.createFmt)('pre', { language });
27
+ exports.pre = pre;
28
+ const link = (content, url) =>
29
+ //
30
+ (0, formatting_1.linkOrMention)(content, { type: 'text_link', url });
31
+ exports.link = link;
32
+ const mention = (name, user) => typeof user === 'number'
33
+ ? (0, exports.link)(name, 'tg://user?id=' + user)
34
+ : (0, formatting_1.linkOrMention)(name, {
35
+ type: 'text_mention',
36
+ user,
37
+ });
38
+ exports.mention = mention;