@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,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `html` mode: Telegram's `parse_mode: HTML`.
|
|
3
|
+
*
|
|
4
|
+
* Native tags are the documented subset (`b`/`i`/`u`/`s`/`a`/`code`/`pre`/
|
|
5
|
+
* `blockquote`/`tg-spoiler`); everything else either becomes text inside a
|
|
6
|
+
* draft or takes the core fallback. Media is sent as separate media messages
|
|
7
|
+
* (albums for consecutive items) — including media that stood inside an inline
|
|
8
|
+
* wrapper, which is lifted out of the text run and reported as moved — and
|
|
9
|
+
* action, link and submit buttons become an inline keyboard. An `input` button
|
|
10
|
+
* has no native form control here, so it takes the core fallback.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
ElementRegistry,
|
|
15
|
+
type Mode,
|
|
16
|
+
type RenderContext,
|
|
17
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
18
|
+
import type { Element } from '@deepseek-ai/satori-element'
|
|
19
|
+
import {
|
|
20
|
+
assembleDraft,
|
|
21
|
+
checkDraft,
|
|
22
|
+
escapeAttribute,
|
|
23
|
+
escapeHtml,
|
|
24
|
+
mediaNode,
|
|
25
|
+
serializeDraft,
|
|
26
|
+
TELEGRAM_LIMITS,
|
|
27
|
+
textRun,
|
|
28
|
+
type TelegramModeTypes,
|
|
29
|
+
type TelegramNode,
|
|
30
|
+
} from './shared.ts'
|
|
31
|
+
|
|
32
|
+
/** Wrap the text of `children` in one inline tag; media of the subtree follows as its own message. */
|
|
33
|
+
function wrap(
|
|
34
|
+
tag: string,
|
|
35
|
+
ctx: RenderContext<TelegramModeTypes>,
|
|
36
|
+
children: readonly Element[],
|
|
37
|
+
): TelegramNode[] {
|
|
38
|
+
const { html, nodes } = textRun(ctx.render(children))
|
|
39
|
+
return [
|
|
40
|
+
...(html === ''
|
|
41
|
+
? []
|
|
42
|
+
: [{ type: 'text' as const, html: `<${tag}>${html}</${tag}>` }]),
|
|
43
|
+
...nodes,
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Render a heading as a bold line, then the media of the heading, then the paragraph break. */
|
|
48
|
+
function heading(
|
|
49
|
+
ctx: RenderContext<TelegramModeTypes>,
|
|
50
|
+
children: readonly Element[],
|
|
51
|
+
): TelegramNode[] {
|
|
52
|
+
const { html, nodes } = textRun(ctx.render(children))
|
|
53
|
+
const line =
|
|
54
|
+
html === '' ? [] : [{ type: 'text' as const, html: `<b>${html}</b>` }]
|
|
55
|
+
return [...line, ...nodes, { type: 'text', html: '\n\n' }]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Build the `html` mode. */
|
|
59
|
+
export function createHtmlMode(): Mode<TelegramModeTypes> {
|
|
60
|
+
const elements = new ElementRegistry<TelegramModeTypes>()
|
|
61
|
+
.register('text', (el) => ({
|
|
62
|
+
type: 'text',
|
|
63
|
+
html: escapeHtml(el.attrs.content),
|
|
64
|
+
}))
|
|
65
|
+
.register('br', () => ({ type: 'text', html: '\n' }))
|
|
66
|
+
// Paragraphs hand their children through so that media reaches `assemble` instead of being flattened away.
|
|
67
|
+
.register('p', (el, ctx) => [
|
|
68
|
+
...ctx.render(el.children),
|
|
69
|
+
{ type: 'text', html: '\n\n' },
|
|
70
|
+
])
|
|
71
|
+
.register('b', (el, ctx) => wrap('b', ctx, el.children))
|
|
72
|
+
.register('i', (el, ctx) => wrap('i', ctx, el.children))
|
|
73
|
+
.register('u', (el, ctx) => wrap('u', ctx, el.children))
|
|
74
|
+
.register('s', (el, ctx) => wrap('s', ctx, el.children))
|
|
75
|
+
.register('spoiler', (el, ctx) => wrap('tg-spoiler', ctx, el.children))
|
|
76
|
+
.register('blockquote', (el, ctx) => wrap('blockquote', ctx, el.children))
|
|
77
|
+
.register('code', (el, ctx) => ({
|
|
78
|
+
type: 'text',
|
|
79
|
+
html: `<code>${escapeHtml(ctx.text(el.children))}</code>`,
|
|
80
|
+
}))
|
|
81
|
+
.register('pre', (el, ctx) => {
|
|
82
|
+
const text = escapeHtml(ctx.text(el.children))
|
|
83
|
+
const language = el.attrs.lang
|
|
84
|
+
return {
|
|
85
|
+
type: 'text',
|
|
86
|
+
html:
|
|
87
|
+
language === undefined
|
|
88
|
+
? `<pre>${text}</pre>`
|
|
89
|
+
: `<pre><code class="language-${escapeAttribute(language)}">${text}</code></pre>`,
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
.register('a', (el, ctx) => {
|
|
93
|
+
const { html, nodes } = textRun(ctx.render(el.children))
|
|
94
|
+
// A link around nothing but an image keeps the URL as its label; the image follows as its own message.
|
|
95
|
+
const label = html === '' ? escapeHtml(el.attrs.href) : html
|
|
96
|
+
return [
|
|
97
|
+
{
|
|
98
|
+
type: 'text' as const,
|
|
99
|
+
html: `<a href="${escapeAttribute(el.attrs.href)}">${label}</a>`,
|
|
100
|
+
},
|
|
101
|
+
...nodes,
|
|
102
|
+
]
|
|
103
|
+
})
|
|
104
|
+
.register(
|
|
105
|
+
'at',
|
|
106
|
+
(el) => {
|
|
107
|
+
if (el.attrs.id !== undefined && el.attrs.type === 'user') {
|
|
108
|
+
const label = escapeHtml(el.attrs.name ?? el.attrs.id)
|
|
109
|
+
return {
|
|
110
|
+
type: 'text',
|
|
111
|
+
html: `<a href="tg://user?id=${escapeAttribute(el.attrs.id)}">${label}</a>`,
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const label = el.attrs.name ?? el.attrs.type
|
|
115
|
+
return { type: 'text', html: escapeHtml(`@${label}`) }
|
|
116
|
+
},
|
|
117
|
+
{ variants: ['user', 'all', 'here', 'role'] },
|
|
118
|
+
)
|
|
119
|
+
.register('h1', (el, ctx) => heading(ctx, el.children))
|
|
120
|
+
.register('h2', (el, ctx) => heading(ctx, el.children))
|
|
121
|
+
.register('h3', (el, ctx) => heading(ctx, el.children))
|
|
122
|
+
.register('h4', (el, ctx) => heading(ctx, el.children))
|
|
123
|
+
.register('h5', (el, ctx) => heading(ctx, el.children))
|
|
124
|
+
.register('h6', (el, ctx) => heading(ctx, el.children))
|
|
125
|
+
.register('ul', (el, ctx) => ctx.render(el.children))
|
|
126
|
+
.register('ol', (el, ctx) => ctx.render(el.children))
|
|
127
|
+
.register('li', (el, ctx) => {
|
|
128
|
+
const { html, nodes } = textRun(ctx.render(el.children))
|
|
129
|
+
const line =
|
|
130
|
+
html === '' ? [] : [{ type: 'text' as const, html: `• ${html}` }]
|
|
131
|
+
return [...line, ...nodes, { type: 'text', html: '\n' }]
|
|
132
|
+
})
|
|
133
|
+
.register('hr', () => ({ type: 'text', html: '\n—\n' }))
|
|
134
|
+
.register('img', (el, ctx) => mediaNode(el, ctx), { variants: ['inline'] })
|
|
135
|
+
.register('video', (el, ctx) => mediaNode(el, ctx))
|
|
136
|
+
.register('audio', (el, ctx) => mediaNode(el, ctx))
|
|
137
|
+
.register('file', (el, ctx) => mediaNode(el, ctx))
|
|
138
|
+
.register('emoji', (el) => ({
|
|
139
|
+
type: 'text',
|
|
140
|
+
html: escapeHtml(el.attrs.name ?? ''),
|
|
141
|
+
}))
|
|
142
|
+
.register('channel', (el) => ({
|
|
143
|
+
type: 'text',
|
|
144
|
+
html: escapeHtml(el.attrs.name ?? ''),
|
|
145
|
+
}))
|
|
146
|
+
.register('small', (el, ctx) => ctx.render(el.children))
|
|
147
|
+
.register(
|
|
148
|
+
'button',
|
|
149
|
+
(el, ctx) => {
|
|
150
|
+
if (el.attrs.type === 'link') {
|
|
151
|
+
return {
|
|
152
|
+
type: 'button',
|
|
153
|
+
button: {
|
|
154
|
+
kind: 'link',
|
|
155
|
+
text: ctx.text(el.children).trim() || el.attrs.href,
|
|
156
|
+
url: el.attrs.href,
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// `input` is not a declared variant (the core fallback renders it as text), but the attribute union still holds it.
|
|
161
|
+
if (el.attrs.type === 'input') {
|
|
162
|
+
return {
|
|
163
|
+
type: 'text',
|
|
164
|
+
html: escapeHtml(ctx.text(el.children).trim() || el.attrs.text),
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
type: 'button',
|
|
169
|
+
button: {
|
|
170
|
+
kind: 'callback',
|
|
171
|
+
text: ctx.text(el.children).trim() || el.attrs.id,
|
|
172
|
+
data: el.attrs.id,
|
|
173
|
+
},
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
{ variants: ['action', 'link', 'submit'] },
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
id: 'html',
|
|
181
|
+
elements,
|
|
182
|
+
limits: {
|
|
183
|
+
'limit.text.chars': TELEGRAM_LIMITS.text,
|
|
184
|
+
'limit.buttons.row': TELEGRAM_LIMITS.buttonsPerRow,
|
|
185
|
+
'limit.stream.interval': TELEGRAM_LIMITS.streamIntervalMs,
|
|
186
|
+
'limit.message.parts': TELEGRAM_LIMITS.parts,
|
|
187
|
+
'limit.delete.window': TELEGRAM_LIMITS.deleteWindowMs,
|
|
188
|
+
},
|
|
189
|
+
assemble: (nodes) => assembleDraft(nodes),
|
|
190
|
+
serialize: (prepared, ctx) => serializeDraft(prepared, ctx, 'HTML'),
|
|
191
|
+
check: (draft) => checkDraft(draft),
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `plain` mode: no `parse_mode`, no formatting, no inline keyboard.
|
|
3
|
+
*
|
|
4
|
+
* Text is sent verbatim; links keep only their label, mentions become
|
|
5
|
+
* `@name`, and buttons degrade to text lines. Media still goes out as media
|
|
6
|
+
* messages — including media that stood inside an inline wrapper, which is
|
|
7
|
+
* lifted out of the text run and reported as moved — so a platform that
|
|
8
|
+
* refuses HTML can still receive the content.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { ElementRegistry, type Mode } from '@deepseek-ai/cordis-plugin-im-core'
|
|
12
|
+
import {
|
|
13
|
+
assembleDraft,
|
|
14
|
+
checkDraft,
|
|
15
|
+
inlineText,
|
|
16
|
+
mediaNode,
|
|
17
|
+
serializeDraft,
|
|
18
|
+
TELEGRAM_LIMITS,
|
|
19
|
+
textRun,
|
|
20
|
+
type TelegramModeTypes,
|
|
21
|
+
} from './shared.ts'
|
|
22
|
+
|
|
23
|
+
/** Build the `plain` mode. */
|
|
24
|
+
export function createPlainMode(): Mode<TelegramModeTypes> {
|
|
25
|
+
const elements = new ElementRegistry<TelegramModeTypes>()
|
|
26
|
+
.register('text', (el) => ({ type: 'text', html: el.attrs.content }))
|
|
27
|
+
.register('br', () => ({ type: 'text', html: '\n' }))
|
|
28
|
+
// Paragraphs hand their children through so that media reaches `assemble` instead of being flattened away.
|
|
29
|
+
.register('p', (el, ctx) => [
|
|
30
|
+
...ctx.render(el.children),
|
|
31
|
+
{ type: 'text', html: '\n\n' },
|
|
32
|
+
])
|
|
33
|
+
.register('a', (el, ctx) => {
|
|
34
|
+
const { html, nodes } = textRun(ctx.render(el.children))
|
|
35
|
+
// A link around nothing but an image keeps the URL as its label; the image follows as its own message.
|
|
36
|
+
return [
|
|
37
|
+
{ type: 'text' as const, html: html === '' ? el.attrs.href : html },
|
|
38
|
+
...nodes,
|
|
39
|
+
]
|
|
40
|
+
})
|
|
41
|
+
.register(
|
|
42
|
+
'at',
|
|
43
|
+
(el) => ({
|
|
44
|
+
type: 'text',
|
|
45
|
+
html: `@${el.attrs.name ?? (el.attrs.type === 'user' ? el.attrs.id : undefined) ?? el.attrs.type}`,
|
|
46
|
+
}),
|
|
47
|
+
{ variants: ['user', 'all', 'here', 'role'] },
|
|
48
|
+
)
|
|
49
|
+
.register('img', (el, ctx) => mediaNode(el, ctx), { variants: ['inline'] })
|
|
50
|
+
.register('video', (el, ctx) => mediaNode(el, ctx))
|
|
51
|
+
.register('audio', (el, ctx) => mediaNode(el, ctx))
|
|
52
|
+
.register('file', (el, ctx) => mediaNode(el, ctx))
|
|
53
|
+
.register(
|
|
54
|
+
'button',
|
|
55
|
+
(el, ctx) => ({
|
|
56
|
+
type: 'text',
|
|
57
|
+
html: `[${inlineText(ctx.render(el.children)).trim() || (el.attrs.type === 'link' ? el.attrs.href : el.attrs.type === 'input' ? el.attrs.text : el.attrs.id)}]\n`,
|
|
58
|
+
}),
|
|
59
|
+
{ variants: ['action', 'link', 'submit'] },
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
id: 'plain',
|
|
64
|
+
elements,
|
|
65
|
+
limits: {
|
|
66
|
+
'limit.text.chars': TELEGRAM_LIMITS.text,
|
|
67
|
+
'limit.stream.interval': TELEGRAM_LIMITS.streamIntervalMs,
|
|
68
|
+
'limit.message.parts': TELEGRAM_LIMITS.parts,
|
|
69
|
+
'limit.delete.window': TELEGRAM_LIMITS.deleteWindowMs,
|
|
70
|
+
},
|
|
71
|
+
assemble: (nodes) => assembleDraft(nodes),
|
|
72
|
+
serialize: (prepared, ctx) => serializeDraft(prepared, ctx, undefined),
|
|
73
|
+
check: (draft) => checkDraft(draft),
|
|
74
|
+
}
|
|
75
|
+
}
|