@im-more/dsh-whatsapp 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 +12 -0
- package/lib/index.js +1694 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +66 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +164 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +40 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +63 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +64 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +244 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +53 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +149 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +48 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +52 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +16 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +15 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/interactive.d.ts +45 -0
- package/lib/types/interactive.d.ts.map +1 -0
- package/lib/types/interactive.js +111 -0
- package/lib/types/interactive.js.map +1 -0
- package/lib/types/internal.d.ts +28 -0
- package/lib/types/internal.d.ts.map +1 -0
- package/lib/types/internal.js +35 -0
- package/lib/types/internal.js.map +1 -0
- package/lib/types/media.d.ts +17 -0
- package/lib/types/media.d.ts.map +1 -0
- package/lib/types/media.js +49 -0
- package/lib/types/media.js.map +1 -0
- package/lib/types/modes/shared.d.ts +112 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +285 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/modes/text.d.ts +19 -0
- package/lib/types/modes/text.d.ts.map +1 -0
- package/lib/types/modes/text.js +126 -0
- package/lib/types/modes/text.js.map +1 -0
- package/lib/types/request.d.ts +61 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +193 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +47 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +74 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/signature.d.ts +33 -0
- package/lib/types/signature.d.ts.map +1 -0
- package/lib/types/signature.js +46 -0
- package/lib/types/signature.js.map +1 -0
- package/lib/types/transport/webhook.d.ts +50 -0
- package/lib/types/transport/webhook.d.ts.map +1 -0
- package/lib/types/transport/webhook.js +197 -0
- package/lib/types/transport/webhook.js.map +1 -0
- package/lib/types/types.d.ts +198 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +2 -0
- package/lib/types/types.js.map +1 -0
- package/package.json +61 -3
- package/src/bot.ts +233 -0
- package/src/config.ts +98 -0
- package/src/decode.ts +335 -0
- package/src/errors.ts +189 -0
- package/src/identity.ts +76 -0
- package/src/index.ts +100 -0
- package/src/interactive.ts +135 -0
- package/src/internal.ts +49 -0
- package/src/media.ts +59 -0
- package/src/modes/shared.ts +354 -0
- package/src/modes/text.ts +174 -0
- package/src/request.ts +238 -0
- package/src/send.ts +120 -0
- package/src/signature.ts +54 -0
- package/src/transport/webhook.ts +267 -0
- package/src/types.ts +244 -0
- package/README.md +0 -3
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types and the draft machinery of the single `text` mode.
|
|
3
|
+
*
|
|
4
|
+
* One draft is exactly one platform message: WhatsApp has no request that carries several messages, so every draft is
|
|
5
|
+
* a `POST /{phoneNumberId}/messages` of its own. Text accumulates into a body that the platform's inline marks can
|
|
6
|
+
* format, each media element becomes a message of its own, and reply buttons become one interactive message.
|
|
7
|
+
*/
|
|
8
|
+
import { isPhrasingContainer } from '@deepseek-ai/satori-element';
|
|
9
|
+
/** The platform's documented bounds. */
|
|
10
|
+
export const WHATSAPP_LIMITS = {
|
|
11
|
+
/** One text message body: 4,096 characters. */
|
|
12
|
+
text: 4_096,
|
|
13
|
+
/** Reply buttons one interactive message accepts. */
|
|
14
|
+
buttons: 3,
|
|
15
|
+
/** One reply button title: 20 characters. */
|
|
16
|
+
buttonTitle: 20,
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The media types the Cloud API accepts, verbatim from the platform's documented list.
|
|
20
|
+
*
|
|
21
|
+
* The upload endpoint answers `100`/`131009` for anything else, so the adapter refuses an unsupported source before it
|
|
22
|
+
* sends a request. `audio/opus` is the one entry the upstream adapter added to the platform list.
|
|
23
|
+
*/
|
|
24
|
+
export const SUPPORTED_MEDIA = [
|
|
25
|
+
'audio/aac',
|
|
26
|
+
'audio/mp4',
|
|
27
|
+
'audio/mpeg',
|
|
28
|
+
'audio/amr',
|
|
29
|
+
'audio/ogg',
|
|
30
|
+
'audio/opus',
|
|
31
|
+
'application/vnd.ms-powerpoint',
|
|
32
|
+
'application/msword',
|
|
33
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
34
|
+
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
35
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
36
|
+
'application/pdf',
|
|
37
|
+
'text/plain',
|
|
38
|
+
'application/vnd.ms-excel',
|
|
39
|
+
'image/jpeg',
|
|
40
|
+
'image/png',
|
|
41
|
+
'image/webp',
|
|
42
|
+
'video/mp4',
|
|
43
|
+
'video/3gpp',
|
|
44
|
+
];
|
|
45
|
+
/** Report inline media that moves out of its text run into a message of its own. */
|
|
46
|
+
export function reportMovedMedia(el, ctx) {
|
|
47
|
+
const parent = ctx.ancestors[ctx.ancestors.length - 1];
|
|
48
|
+
if (parent !== undefined && isPhrasingContainer(parent.type)) {
|
|
49
|
+
ctx.report.moved.push({
|
|
50
|
+
tag: el.type,
|
|
51
|
+
reason: 'whatsapp-media-split',
|
|
52
|
+
path: ctx.stableId(el),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The latest paragraph, line or space boundary at or before `limit`.
|
|
58
|
+
*
|
|
59
|
+
* The whitespace that ends the boundary stays in the chunk, so concatenating the chunks reproduces the input.
|
|
60
|
+
*
|
|
61
|
+
* @param text - the text to cut.
|
|
62
|
+
* @param limit - the maximum chunk length in UTF-16 code units.
|
|
63
|
+
* @returns the cut index including the boundary whitespace, or -1 for none.
|
|
64
|
+
*/
|
|
65
|
+
function boundaryCut(text, limit) {
|
|
66
|
+
const window = text.slice(0, limit);
|
|
67
|
+
const paragraph = window.lastIndexOf('\n\n');
|
|
68
|
+
if (paragraph >= 0)
|
|
69
|
+
return paragraph + 2;
|
|
70
|
+
const line = window.lastIndexOf('\n');
|
|
71
|
+
if (line >= 0)
|
|
72
|
+
return line + 1;
|
|
73
|
+
const space = window.lastIndexOf(' ');
|
|
74
|
+
if (space >= 0)
|
|
75
|
+
return space + 1;
|
|
76
|
+
return -1;
|
|
77
|
+
}
|
|
78
|
+
/** Move a cut left by one unit when it would fall inside a surrogate pair. */
|
|
79
|
+
function avoidSplitPair(text, cut) {
|
|
80
|
+
if (cut <= 0 || cut >= text.length)
|
|
81
|
+
return cut;
|
|
82
|
+
const before = text.charCodeAt(cut - 1);
|
|
83
|
+
const after = text.charCodeAt(cut);
|
|
84
|
+
const high = before >= 0xd800 && before <= 0xdbff;
|
|
85
|
+
const low = after >= 0xdc00 && after <= 0xdfff;
|
|
86
|
+
return high && low ? cut - 1 : cut;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Cut text into chunks a text message accepts.
|
|
90
|
+
*
|
|
91
|
+
* Point cuts prefer a paragraph break, then a line break, then a space, then a code-unit boundary that does not split a
|
|
92
|
+
* surrogate pair. The chunks concatenate back to the input.
|
|
93
|
+
*
|
|
94
|
+
* @param text - the text to cut.
|
|
95
|
+
* @param limit - the maximum chunk length in UTF-16 code units.
|
|
96
|
+
* @returns one or more chunks.
|
|
97
|
+
*/
|
|
98
|
+
export function splitText(text, limit) {
|
|
99
|
+
if (text.length <= limit)
|
|
100
|
+
return [text];
|
|
101
|
+
const chunks = [];
|
|
102
|
+
let rest = text;
|
|
103
|
+
while (rest.length > limit) {
|
|
104
|
+
let cut = boundaryCut(rest, limit);
|
|
105
|
+
if (cut <= 0)
|
|
106
|
+
cut = Math.max(1, limit);
|
|
107
|
+
cut = Math.max(1, avoidSplitPair(rest, cut));
|
|
108
|
+
chunks.push(rest.slice(0, cut));
|
|
109
|
+
rest = rest.slice(cut);
|
|
110
|
+
}
|
|
111
|
+
if (rest !== '')
|
|
112
|
+
chunks.push(rest);
|
|
113
|
+
return chunks;
|
|
114
|
+
}
|
|
115
|
+
/** The carrier text of a message that exists only to show replies, and of every later button group. */
|
|
116
|
+
const BUTTON_CARRIER = ' ';
|
|
117
|
+
/** One media draft of a node. */
|
|
118
|
+
function mediaDraftOf(node) {
|
|
119
|
+
const type = node.kind === 'file' ? 'document' : node.kind;
|
|
120
|
+
return {
|
|
121
|
+
kind: 'media',
|
|
122
|
+
type,
|
|
123
|
+
ref: node.ref,
|
|
124
|
+
...(node.name === undefined ? {} : { name: node.name }),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Fold a node stream into platform messages.
|
|
129
|
+
*
|
|
130
|
+
* Text accumulates into one body; a media element closes the text body that precedes it and becomes a message of its
|
|
131
|
+
* own; buttons become one interactive message per three, sent before the text that follows the last media element, as
|
|
132
|
+
* the platform shows the body on the first group and nothing on the rest.
|
|
133
|
+
*
|
|
134
|
+
* @param nodes - rendered nodes, in document order.
|
|
135
|
+
* @param ctx - the render context, for the split count.
|
|
136
|
+
* @returns one draft per platform message, in send order; never empty.
|
|
137
|
+
*/
|
|
138
|
+
export function assembleDraft(nodes, ctx) {
|
|
139
|
+
const drafts = [];
|
|
140
|
+
const buttons = [];
|
|
141
|
+
let buffer = '';
|
|
142
|
+
let split = 0;
|
|
143
|
+
const flushText = () => {
|
|
144
|
+
const text = buffer;
|
|
145
|
+
buffer = '';
|
|
146
|
+
if (text === '')
|
|
147
|
+
return;
|
|
148
|
+
const chunks = splitText(text, WHATSAPP_LIMITS.text);
|
|
149
|
+
split += chunks.length - 1;
|
|
150
|
+
for (const chunk of chunks)
|
|
151
|
+
drafts.push({ kind: 'text', body: chunk });
|
|
152
|
+
};
|
|
153
|
+
const flushButtons = () => {
|
|
154
|
+
// The body rides the first group so the reader sees the text with the buttons it belongs to.
|
|
155
|
+
const body = buffer === '' ? BUTTON_CARRIER : buffer;
|
|
156
|
+
buffer = '';
|
|
157
|
+
for (let index = 0; index < buttons.length; index += WHATSAPP_LIMITS.buttons) {
|
|
158
|
+
drafts.push({
|
|
159
|
+
kind: 'buttons',
|
|
160
|
+
body: index === 0 ? body : BUTTON_CARRIER,
|
|
161
|
+
buttons: buttons.slice(index, index + WHATSAPP_LIMITS.buttons),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
buttons.length = 0;
|
|
165
|
+
};
|
|
166
|
+
for (const node of nodes) {
|
|
167
|
+
switch (node.type) {
|
|
168
|
+
case 'text':
|
|
169
|
+
buffer += node.text;
|
|
170
|
+
break;
|
|
171
|
+
case 'media':
|
|
172
|
+
flushText();
|
|
173
|
+
drafts.push(mediaDraftOf(node));
|
|
174
|
+
break;
|
|
175
|
+
case 'button':
|
|
176
|
+
buttons.push({ id: node.id, title: node.title });
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (buttons.length > 0)
|
|
181
|
+
flushButtons();
|
|
182
|
+
flushText();
|
|
183
|
+
ctx.report.split += split;
|
|
184
|
+
// Content the pipeline accepts always yields something; a mode may never answer with no platform message.
|
|
185
|
+
if (drafts.length === 0)
|
|
186
|
+
drafts.push({ kind: 'text', body: BUTTON_CARRIER });
|
|
187
|
+
return drafts;
|
|
188
|
+
}
|
|
189
|
+
/** One resolved resource key of a prepared draft. */
|
|
190
|
+
function resolvedKey(prepared, ref) {
|
|
191
|
+
const resolved = prepared.resolved.get(ref);
|
|
192
|
+
if (resolved === undefined) {
|
|
193
|
+
throw new Error('whatsapp: media was not prepared before serialize');
|
|
194
|
+
}
|
|
195
|
+
return resolved.key;
|
|
196
|
+
}
|
|
197
|
+
/** The platform-neutral part of a request body. */
|
|
198
|
+
function baseOf(to, ctx) {
|
|
199
|
+
// Only the first message of a logical message answers the quote; the platform rejects a reply to itself.
|
|
200
|
+
return {
|
|
201
|
+
messaging_product: 'whatsapp',
|
|
202
|
+
recipient_type: 'individual',
|
|
203
|
+
to,
|
|
204
|
+
...(ctx.index !== 0 || ctx.quote === undefined
|
|
205
|
+
? {}
|
|
206
|
+
: { context: { message_id: ctx.quote } }),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/** One media request body, per kind, because the media detail sits under the type name. */
|
|
210
|
+
function mediaPayload(type, id, base) {
|
|
211
|
+
switch (type) {
|
|
212
|
+
case 'image':
|
|
213
|
+
return { ...base, type: 'image', image: { id } };
|
|
214
|
+
case 'audio':
|
|
215
|
+
return { ...base, type: 'audio', audio: { id } };
|
|
216
|
+
case 'video':
|
|
217
|
+
return { ...base, type: 'video', video: { id } };
|
|
218
|
+
case 'document':
|
|
219
|
+
return { ...base, type: 'document', document: { id } };
|
|
220
|
+
case 'sticker':
|
|
221
|
+
return { ...base, type: 'sticker', sticker: { id } };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Build the exact request body of one prepared draft.
|
|
226
|
+
*
|
|
227
|
+
* @param prepared - the draft with its resource keys resolved.
|
|
228
|
+
* @param ctx - the send context: target, part index and quoted message.
|
|
229
|
+
* @param linkPreview - whether a text body asks the client for a link preview.
|
|
230
|
+
* @returns the request body.
|
|
231
|
+
*/
|
|
232
|
+
export function serializeDraft(prepared, ctx, linkPreview) {
|
|
233
|
+
const draft = prepared.draft;
|
|
234
|
+
const base = baseOf(ctx.target.chat.id, ctx);
|
|
235
|
+
switch (draft.kind) {
|
|
236
|
+
case 'text':
|
|
237
|
+
return {
|
|
238
|
+
...base,
|
|
239
|
+
type: 'text',
|
|
240
|
+
text: { body: draft.body, preview_url: linkPreview },
|
|
241
|
+
};
|
|
242
|
+
case 'media':
|
|
243
|
+
return mediaPayload(draft.type, resolvedKey(prepared, draft.ref), base);
|
|
244
|
+
case 'buttons': {
|
|
245
|
+
const buttons = draft.buttons.map((button) => ({
|
|
246
|
+
type: 'reply',
|
|
247
|
+
id: button.id,
|
|
248
|
+
title: button.title,
|
|
249
|
+
}));
|
|
250
|
+
return {
|
|
251
|
+
...base,
|
|
252
|
+
type: 'button',
|
|
253
|
+
body: { text: draft.body },
|
|
254
|
+
action: { buttons },
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Hard platform limits of one draft, checked before any request.
|
|
261
|
+
*
|
|
262
|
+
* Text splitting and button grouping already keep the draft inside the character and count bounds; what `assemble`
|
|
263
|
+
* cannot guarantee without changing the content is a button title over 20 characters, which the platform refuses.
|
|
264
|
+
*
|
|
265
|
+
* @param draft - the assembled draft.
|
|
266
|
+
* @returns every violation the platform would refuse.
|
|
267
|
+
*/
|
|
268
|
+
export function checkDraft(draft) {
|
|
269
|
+
if (draft.kind !== 'buttons')
|
|
270
|
+
return [];
|
|
271
|
+
const violations = [];
|
|
272
|
+
for (const button of draft.buttons) {
|
|
273
|
+
if (button.title.length <= WHATSAPP_LIMITS.buttonTitle)
|
|
274
|
+
continue;
|
|
275
|
+
violations.push({
|
|
276
|
+
kind: 'size',
|
|
277
|
+
limit: WHATSAPP_LIMITS.buttonTitle,
|
|
278
|
+
actual: button.title.length,
|
|
279
|
+
message: `WhatsApp reply button titles accept ${WHATSAPP_LIMITS.buttonTitle} characters, ` +
|
|
280
|
+
`"${button.title}" has ${button.title.length}`,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
return violations;
|
|
284
|
+
}
|
|
285
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/modes/shared.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAUH,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AAmCjE,wCAAwC;AACxC,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,+CAA+C;IAC/C,IAAI,EAAE,KAAK;IACX,qDAAqD;IACrD,OAAO,EAAE,CAAC;IACV,6CAA6C;IAC7C,WAAW,EAAE,EAAE;CACP,CAAA;AAEV;;;;;GAKG;AACH,MAAM,CAAC,MAAM,eAAe,GAAsB;IAChD,WAAW;IACX,WAAW;IACX,YAAY;IACZ,WAAW;IACX,WAAW;IACX,YAAY;IACZ,+BAA+B;IAC/B,oBAAoB;IACpB,yEAAyE;IACzE,2EAA2E;IAC3E,mEAAmE;IACnE,iBAAiB;IACjB,YAAY;IACZ,0BAA0B;IAC1B,YAAY;IACZ,WAAW;IACX,YAAY;IACZ,WAAW;IACX,YAAY;CACb,CAAA;AAED,oFAAoF;AACpF,MAAM,UAAU,gBAAgB,CAC9B,EAAW,EACX,GAAqC;IAErC,MAAM,MAAM,GAAG,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACtD,IAAI,MAAM,KAAK,SAAS,IAAI,mBAAmB,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7D,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;YACpB,GAAG,EAAE,EAAE,CAAC,IAAI;YACZ,MAAM,EAAE,sBAAsB;YAC9B,IAAI,EAAE,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;SACvB,CAAC,CAAA;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH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,KAAK,CAAC,MAAM,EAAE;SACjD,CAAC,CAAA;IACJ,CAAC;IACD,OAAO,UAAU,CAAA;AACnB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `text` mode: the only outbound representation the Cloud API programs against.
|
|
3
|
+
*
|
|
4
|
+
* The body is sent verbatim — WhatsApp has no escaping convention — but the platform documents four inline marks plus a
|
|
5
|
+
* quoted-line prefix, so bold, italic, strike-through, monospace and block quotes reach the reader formatted instead of
|
|
6
|
+
* disappearing. A link keeps its URL in the body because the platform has no link mark. Everything else takes the core
|
|
7
|
+
* fallback and is reported as degraded; media becomes a message of its own.
|
|
8
|
+
*/
|
|
9
|
+
import type { Mode } from '@deepseek-ai/cordis-plugin-im-core';
|
|
10
|
+
import type { Config } from '../config.ts';
|
|
11
|
+
import type { WhatsappModeTypes } from './shared.ts';
|
|
12
|
+
/**
|
|
13
|
+
* Build the `text` mode.
|
|
14
|
+
*
|
|
15
|
+
* @param config - the adapter configuration; it carries the link preview switch of every text body.
|
|
16
|
+
* @returns the single mode this adapter registers.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createTextMode(config: Config): Mode<WhatsappModeTypes>;
|
|
19
|
+
//# sourceMappingURL=text.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../../src/modes/text.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EAAE,IAAI,EAAiB,MAAM,oCAAoC,CAAA;AAE7E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAQ1C,OAAO,KAAK,EAEV,iBAAiB,EAElB,MAAM,aAAa,CAAA;AAgEpB;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,iBAAiB,CAAC,CA+EtE"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `text` mode: the only outbound representation the Cloud API programs against.
|
|
3
|
+
*
|
|
4
|
+
* The body is sent verbatim — WhatsApp has no escaping convention — but the platform documents four inline marks plus a
|
|
5
|
+
* quoted-line prefix, so bold, italic, strike-through, monospace and block quotes reach the reader formatted instead of
|
|
6
|
+
* disappearing. A link keeps its URL in the body because the platform has no link mark. Everything else takes the core
|
|
7
|
+
* fallback and is reported as degraded; media becomes a message of its own.
|
|
8
|
+
*/
|
|
9
|
+
import { ElementRegistry } from '@deepseek-ai/cordis-plugin-im-core';
|
|
10
|
+
import { assembleDraft, checkDraft, reportMovedMedia, serializeDraft, WHATSAPP_LIMITS, } from "./shared.js";
|
|
11
|
+
/** The text run of rendered children and the nodes that cannot ride inside it. */
|
|
12
|
+
function textRun(nodes) {
|
|
13
|
+
let text = '';
|
|
14
|
+
const rest = [];
|
|
15
|
+
for (const node of nodes) {
|
|
16
|
+
if (node.type === 'text')
|
|
17
|
+
text += node.text;
|
|
18
|
+
else
|
|
19
|
+
rest.push(node);
|
|
20
|
+
}
|
|
21
|
+
return { text, rest };
|
|
22
|
+
}
|
|
23
|
+
/** One inline mark around the rendered children, keeping any media of the subtree outside it. */
|
|
24
|
+
function marked(children, ctx, open, close = open) {
|
|
25
|
+
const run = textRun(ctx.render(children));
|
|
26
|
+
if (run.text === '')
|
|
27
|
+
return run.rest;
|
|
28
|
+
return [{ type: 'text', text: `${open}${run.text}${close}` }, ...run.rest];
|
|
29
|
+
}
|
|
30
|
+
/** One media message; a media element standing in phrasing content is reported as moved, not as degraded. */
|
|
31
|
+
function mediaNode(kind, el, ctx, src, meta) {
|
|
32
|
+
reportMovedMedia(el, ctx);
|
|
33
|
+
return {
|
|
34
|
+
type: 'media',
|
|
35
|
+
kind,
|
|
36
|
+
ref: ctx.resource(kind, src, meta),
|
|
37
|
+
...(meta.name === undefined ? {} : { name: meta.name }),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** The reply button of one `button[type=action]`. */
|
|
41
|
+
function buttonNode(el, ctx) {
|
|
42
|
+
const attrs = el.attrs;
|
|
43
|
+
if (attrs.type !== 'action') {
|
|
44
|
+
// The registry declares the `action` variant alone, so the core fallback renders every other one.
|
|
45
|
+
throw new Error(`whatsapp: <button type="${attrs.type}"> has no native handler`);
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
type: 'button',
|
|
49
|
+
id: attrs.id,
|
|
50
|
+
title: ctx.text(el.children).trim(),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Build the `text` mode.
|
|
55
|
+
*
|
|
56
|
+
* @param config - the adapter configuration; it carries the link preview switch of every text body.
|
|
57
|
+
* @returns the single mode this adapter registers.
|
|
58
|
+
*/
|
|
59
|
+
export function createTextMode(config) {
|
|
60
|
+
const elements = new ElementRegistry()
|
|
61
|
+
.register('text', (el) => ({ type: 'text', text: el.attrs.content }))
|
|
62
|
+
.register('br', () => ({ type: 'text', text: '\n' }))
|
|
63
|
+
// Paragraphs hand their children through, so inline media reaches `assemble` instead of being flattened away.
|
|
64
|
+
.register('p', (el, ctx) => [
|
|
65
|
+
...ctx.render(el.children),
|
|
66
|
+
{ type: 'text', text: '\n\n' },
|
|
67
|
+
])
|
|
68
|
+
.register('b', (el, ctx) => marked(el.children, ctx, '*'))
|
|
69
|
+
.register('i', (el, ctx) => marked(el.children, ctx, '_'))
|
|
70
|
+
.register('s', (el, ctx) => marked(el.children, ctx, '~'))
|
|
71
|
+
// The platform's monospace mark has one form only: triple backticks, inline and block alike.
|
|
72
|
+
.register('code', (el, ctx) => marked(el.children, ctx, '```'))
|
|
73
|
+
.register('pre', (el, ctx) => marked(el.children, ctx, '```\n', '\n```'), {
|
|
74
|
+
variants: ['lang'],
|
|
75
|
+
})
|
|
76
|
+
.register('blockquote', (el, ctx) => {
|
|
77
|
+
const run = textRun(ctx.render(el.children));
|
|
78
|
+
// The last paragraph of the subtree ends on a blank line; a quote does not spell it as an empty quoted line.
|
|
79
|
+
const quoted = run.text
|
|
80
|
+
.replace(/\n+$/, '')
|
|
81
|
+
.split('\n')
|
|
82
|
+
.map((line) => `> ${line}`)
|
|
83
|
+
.join('\n');
|
|
84
|
+
return [{ type: 'text', text: quoted }, ...run.rest];
|
|
85
|
+
})
|
|
86
|
+
.register('a', (el, ctx) => {
|
|
87
|
+
const run = textRun(ctx.render(el.children));
|
|
88
|
+
// WhatsApp auto-links a bare URL and has no link mark; appending the target keeps it visible in the body.
|
|
89
|
+
const text = run.text.trim() === ''
|
|
90
|
+
? el.attrs.href
|
|
91
|
+
: `${run.text} (${el.attrs.href})`;
|
|
92
|
+
return [{ type: 'text', text }, ...run.rest];
|
|
93
|
+
})
|
|
94
|
+
.register('img', (el, ctx) => mediaNode('image', el, ctx, el.attrs.src, {}), { variants: ['inline'] })
|
|
95
|
+
.register('audio', (el, ctx) => mediaNode('audio', el, ctx, el.attrs.src, {
|
|
96
|
+
...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
|
|
97
|
+
...(el.attrs.duration === undefined
|
|
98
|
+
? {}
|
|
99
|
+
: { duration: el.attrs.duration }),
|
|
100
|
+
}))
|
|
101
|
+
.register('video', (el, ctx) => mediaNode('video', el, ctx, el.attrs.src, {
|
|
102
|
+
...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
|
|
103
|
+
...(el.attrs.duration === undefined
|
|
104
|
+
? {}
|
|
105
|
+
: { duration: el.attrs.duration }),
|
|
106
|
+
}))
|
|
107
|
+
.register('file', (el, ctx) => mediaNode('file', el, ctx, el.attrs.src, {
|
|
108
|
+
...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
|
|
109
|
+
...(el.attrs.size === undefined ? {} : { size: el.attrs.size }),
|
|
110
|
+
}))
|
|
111
|
+
.register('button', (el, ctx) => buttonNode(el, ctx), {
|
|
112
|
+
variants: ['action'],
|
|
113
|
+
})
|
|
114
|
+
.register('button-group', (el, ctx) => ctx.render(el.children));
|
|
115
|
+
return {
|
|
116
|
+
id: 'text',
|
|
117
|
+
elements,
|
|
118
|
+
limits: {
|
|
119
|
+
'limit.text.chars': WHATSAPP_LIMITS.text,
|
|
120
|
+
},
|
|
121
|
+
assemble: (nodes, _envelope, ctx) => assembleDraft(nodes, ctx),
|
|
122
|
+
serialize: (prepared, ctx) => serializeDraft(prepared, ctx, config.linkPreview),
|
|
123
|
+
check: (draft) => checkDraft(draft),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../../src/modes/text.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAA;AAIpE,OAAO,EACL,aAAa,EACb,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,eAAe,GAChB,MAAM,aAAa,CAAA;AAOpB,kFAAkF;AAClF,SAAS,OAAO,CAAC,KAA8B;IAI7C,IAAI,IAAI,GAAG,EAAE,CAAA;IACb,MAAM,IAAI,GAAmB,EAAE,CAAA;IAC/B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM;YAAE,IAAI,IAAI,IAAI,CAAC,IAAI,CAAA;;YACtC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACtB,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAA;AACvB,CAAC;AAED,iGAAiG;AACjG,SAAS,MAAM,CACb,QAA4B,EAC5B,GAAqC,EACrC,IAAY,EACZ,KAAK,GAAG,IAAI;IAEZ,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAA;IACzC,IAAI,GAAG,CAAC,IAAI,KAAK,EAAE;QAAE,OAAO,GAAG,CAAC,IAAI,CAAA;IACpC,OAAO,CAAC,EAAE,IAAI,EAAE,MAA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C,EAAE,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;QAC/D,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;KAChE,CAAC,CACH;SACA,QAAQ,CAAC,QAAQ,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE;QACpD,QAAQ,EAAE,CAAC,QAAQ,CAAC;KACrB,CAAC;SACD,QAAQ,CAAC,cAAc,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAA;IAEjE,OAAO;QACL,EAAE,EAAE,MAAM;QACV,QAAQ;QACR,MAAM,EAAE;YACN,kBAAkB,EAAE,eAAe,CAAC,IAAI;SACzC;QACD,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,GAAG,CAAC;QAC9D,SAAS,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,CAC3B,cAAc,CAAC,QAAQ,EAAE,GAAG,EAAE,MAAM,CAAC,WAAW,CAAC;QACnD,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC;KACpC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The transport every WhatsApp Cloud API call shares: API root, bearer authentication, Graph error decoding, JSON
|
|
3
|
+
* unwrapping, and the byte download of an inbound media.
|
|
4
|
+
*
|
|
5
|
+
* `Internal` (the migrated platform API map) and the hand-written calls (identity, media address, upload, download) go
|
|
6
|
+
* through the same class, so they cannot diverge in authentication or failure handling.
|
|
7
|
+
*/
|
|
8
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
9
|
+
import type { Config } from './config.ts';
|
|
10
|
+
/**
|
|
11
|
+
* The Graph API client of one configured phone number.
|
|
12
|
+
*
|
|
13
|
+
* Every call carries `Authorization: Bearer <systemToken>`; a refusal raises {@link WhatsappApiError} carrying the
|
|
14
|
+
* HTTP status and the Graph error `code`, which {@link classifyWhatsappError} maps onto the core taxonomy.
|
|
15
|
+
*/
|
|
16
|
+
export declare class WhatsAppRequest {
|
|
17
|
+
readonly ctx: Context;
|
|
18
|
+
readonly config: Config;
|
|
19
|
+
/** API root without its trailing slashes (`apiBaseUrl`, or a Graph version pinned into it). */
|
|
20
|
+
readonly baseUrl: string;
|
|
21
|
+
constructor(ctx: Context, config: Config);
|
|
22
|
+
/**
|
|
23
|
+
* One endpoint under the API root.
|
|
24
|
+
* @param method - HTTP method.
|
|
25
|
+
* @param path - endpoint path, starting with `/`.
|
|
26
|
+
* @param data - JSON body or multipart form; absent on a read.
|
|
27
|
+
* @param signal - cancellation.
|
|
28
|
+
* @returns the parsed JSON answer.
|
|
29
|
+
* @throws WhatsappApiError when the call failed or did not answer with JSON.
|
|
30
|
+
*/
|
|
31
|
+
invoke<T>(method: string, path: string, data?: unknown, signal?: AbortSignal): Promise<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Read one endpoint.
|
|
34
|
+
* @param path - endpoint path, starting with `/`.
|
|
35
|
+
* @param params - query parameters, when the endpoint takes any.
|
|
36
|
+
* @param signal - cancellation.
|
|
37
|
+
* @returns the parsed JSON answer.
|
|
38
|
+
*/
|
|
39
|
+
get<T>(path: string, params?: Record<string, unknown>, signal?: AbortSignal): Promise<T>;
|
|
40
|
+
/**
|
|
41
|
+
* Post one JSON body or multipart form to an endpoint.
|
|
42
|
+
* @param path - endpoint path, starting with `/`.
|
|
43
|
+
* @param data - JSON body or {@link FormData}.
|
|
44
|
+
* @param signal - cancellation.
|
|
45
|
+
* @returns the parsed JSON answer.
|
|
46
|
+
*/
|
|
47
|
+
post<T>(path: string, data: unknown, signal?: AbortSignal): Promise<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Download the bytes behind a resolved media address.
|
|
50
|
+
*
|
|
51
|
+
* The media address is short-lived and requires the same bearer token as the API itself. The answer is returned as
|
|
52
|
+
* it arrived, whatever its status: the caller maps `200` to the bytes and anything else to "unavailable".
|
|
53
|
+
*
|
|
54
|
+
* @param url - the address `GET /{mediaId}` returned.
|
|
55
|
+
* @param signal - cancellation.
|
|
56
|
+
* @returns the download answer.
|
|
57
|
+
* @throws WhatsappApiError when the request never got an answer.
|
|
58
|
+
*/
|
|
59
|
+
download(url: string, signal?: AbortSignal): Promise<Response>;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAElD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AA6DzC;;;;;GAKG;AACH,qBAAa,eAAe;IAKxB,QAAQ,CAAC,GAAG,EAAE,OAAO;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM;IALzB,+FAA+F;IAC/F,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;gBAGb,GAAG,EAAE,OAAO,EACZ,MAAM,EAAE,MAAM;IAKzB;;;;;;;;OAQG;IACG,MAAM,CAAC,CAAC,EACZ,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,OAAO,EACd,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,CAAC,CAAC;IAwEb;;;;;;OAMG;IACG,GAAG,CAAC,CAAC,EACT,IAAI,EAAE,MAAM,EACZ,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,CAAC,CAAC;IAgBb;;;;;;OAMG;IACG,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC;IAI5E;;;;;;;;;;OAUG;IACG,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC;CAcrE"}
|