@kaels/casileys 0.4.5 → 0.4.7
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/README.md +332 -1
- package/lib/MessageBuilder/AIRich.js +3235 -0
- package/lib/Socket/index.js +8 -1
- package/lib/Socket/messages-recv.js +21 -0
- package/lib/Utils/rich-message-utils.js +57 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -321,7 +321,7 @@ sock.sendMessage(jid, {
|
|
|
321
321
|
})
|
|
322
322
|
|
|
323
323
|
// --- Send a text message with a large link preview and favicon
|
|
324
|
-
import { prepareWAMessageMedia } from '
|
|
324
|
+
import { prepareWAMessageMedia } from '@kaels/casileys'
|
|
325
325
|
const urlB = 'https://www.npmjs.com/package/@kaels/casileys#readme'
|
|
326
326
|
|
|
327
327
|
const { imageMessage: image } = await prepareWAMessageMedia({
|
|
@@ -1122,6 +1122,337 @@ sock.sendMessage(jid, {
|
|
|
1122
1122
|
|
|
1123
1123
|
## 🧰 Additional Contents
|
|
1124
1124
|
|
|
1125
|
+
### 🧱 AIRich — Rich HTML / Rich Response Messages
|
|
1126
|
+
|
|
1127
|
+
Casileys includes support for constructing **Rich Response Messages**, including
|
|
1128
|
+
interactive HTML content rendered inside supported WhatsApp clients.
|
|
1129
|
+
|
|
1130
|
+
`AIRich` is a convenience builder. It is **not the underlying protocol**.
|
|
1131
|
+
|
|
1132
|
+
If you need complete control over the payload, you can construct the Rich Message
|
|
1133
|
+
structure yourself and send it through `sock.relayMessage()`.
|
|
1134
|
+
|
|
1135
|
+
---
|
|
1136
|
+
|
|
1137
|
+
#### 🧬 Rich Message Structure
|
|
1138
|
+
|
|
1139
|
+
The important structure is:
|
|
1140
|
+
|
|
1141
|
+
~~~text
|
|
1142
|
+
botForwardedMessage
|
|
1143
|
+
└── message
|
|
1144
|
+
└── richResponseMessage
|
|
1145
|
+
├── messageType
|
|
1146
|
+
├── submessages[]
|
|
1147
|
+
├── unifiedResponse
|
|
1148
|
+
│ └── data
|
|
1149
|
+
│ └── Base64 encoded GenAIUnifiedResponse
|
|
1150
|
+
│ ├── __typename
|
|
1151
|
+
│ ├── response_id
|
|
1152
|
+
│ └── sections[]
|
|
1153
|
+
│ └── view_model
|
|
1154
|
+
│ ├── __typename
|
|
1155
|
+
│ └── primitive
|
|
1156
|
+
│ ├── __typename
|
|
1157
|
+
│ ├── payload
|
|
1158
|
+
│ └── trusted_sources[]
|
|
1159
|
+
└── contextInfo
|
|
1160
|
+
~~~
|
|
1161
|
+
|
|
1162
|
+
The HTML renderer is represented by a primitive such as:
|
|
1163
|
+
|
|
1164
|
+
~~~text
|
|
1165
|
+
GenAIaeacdsnwHtmlPrimitive
|
|
1166
|
+
~~~
|
|
1167
|
+
|
|
1168
|
+
Its `payload` contains the HTML source.
|
|
1169
|
+
|
|
1170
|
+
---
|
|
1171
|
+
|
|
1172
|
+
#### 📦 GenAIUnifiedResponse
|
|
1173
|
+
|
|
1174
|
+
The `unifiedResponse.data` field contains a serialized
|
|
1175
|
+
`GenAIUnifiedResponse` object which is then encoded as Base64.
|
|
1176
|
+
|
|
1177
|
+
Basic structure:
|
|
1178
|
+
|
|
1179
|
+
~~~js
|
|
1180
|
+
const responseId = crypto.randomUUID();
|
|
1181
|
+
|
|
1182
|
+
const unifiedResponse = {
|
|
1183
|
+
__typename: 'GenAIUnifiedResponse',
|
|
1184
|
+
response_id: responseId,
|
|
1185
|
+
sections: [
|
|
1186
|
+
{
|
|
1187
|
+
__typename: 'GenAIUnifiedResponseSection',
|
|
1188
|
+
view_model: {
|
|
1189
|
+
__typename: 'GenAISingleLayoutViewModel',
|
|
1190
|
+
primitive: {
|
|
1191
|
+
__typename: 'GenAIaeacdsnwHtmlPrimitive',
|
|
1192
|
+
payload: '<h1>Hello World</h1>',
|
|
1193
|
+
trusted_sources: []
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
]
|
|
1198
|
+
};
|
|
1199
|
+
|
|
1200
|
+
const data = Buffer.from(
|
|
1201
|
+
JSON.stringify(unifiedResponse)
|
|
1202
|
+
).toString('base64');
|
|
1203
|
+
~~~
|
|
1204
|
+
|
|
1205
|
+
> The exact serialization/escaping used by different implementations may vary.
|
|
1206
|
+
> The important part is that `unifiedResponse.data` carries the serialized
|
|
1207
|
+
> unified response as Base64.
|
|
1208
|
+
|
|
1209
|
+
---
|
|
1210
|
+
|
|
1211
|
+
#### 🛠️ Manual Rich Message Construction
|
|
1212
|
+
|
|
1213
|
+
You do **not** need `createAIRich()` to construct a Rich Message.
|
|
1214
|
+
|
|
1215
|
+
A message can be assembled manually:
|
|
1216
|
+
|
|
1217
|
+
~~~js
|
|
1218
|
+
const responseId = crypto.randomUUID();
|
|
1219
|
+
|
|
1220
|
+
const unifiedResponse = {
|
|
1221
|
+
__typename: 'GenAIUnifiedResponse',
|
|
1222
|
+
response_id: responseId,
|
|
1223
|
+
sections: [
|
|
1224
|
+
{
|
|
1225
|
+
__typename: 'GenAIUnifiedResponseSection',
|
|
1226
|
+
view_model: {
|
|
1227
|
+
__typename: 'GenAISingleLayoutViewModel',
|
|
1228
|
+
primitive: {
|
|
1229
|
+
__typename: 'GenAIaeacdsnwHtmlPrimitive',
|
|
1230
|
+
payload: `
|
|
1231
|
+
<h1>Hello World</h1>
|
|
1232
|
+
<p>This is a Rich HTML message.</p>
|
|
1233
|
+
`,
|
|
1234
|
+
trusted_sources: []
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
]
|
|
1239
|
+
};
|
|
1240
|
+
|
|
1241
|
+
const data = Buffer.from(
|
|
1242
|
+
JSON.stringify(unifiedResponse)
|
|
1243
|
+
).toString('base64');
|
|
1244
|
+
|
|
1245
|
+
const message = {
|
|
1246
|
+
messageContextInfo: {
|
|
1247
|
+
deviceListMetadata: {},
|
|
1248
|
+
deviceListMetadataVersion: 2,
|
|
1249
|
+
botMetadata: {
|
|
1250
|
+
messageDisclaimerText: '',
|
|
1251
|
+
botResponseId: crypto.randomUUID(),
|
|
1252
|
+
|
|
1253
|
+
// Verification metadata may be required by the
|
|
1254
|
+
// target renderer/client.
|
|
1255
|
+
verificationMetadata: {
|
|
1256
|
+
proofs: [
|
|
1257
|
+
{
|
|
1258
|
+
version: 1,
|
|
1259
|
+
useCase: 1,
|
|
1260
|
+
signature: '<SIGNATURE>',
|
|
1261
|
+
certificateChain: [
|
|
1262
|
+
'<CERTIFICATE_1>',
|
|
1263
|
+
'<CERTIFICATE_2>'
|
|
1264
|
+
]
|
|
1265
|
+
}
|
|
1266
|
+
]
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
},
|
|
1270
|
+
|
|
1271
|
+
botForwardedMessage: {
|
|
1272
|
+
message: {
|
|
1273
|
+
richResponseMessage: {
|
|
1274
|
+
messageType: 1,
|
|
1275
|
+
|
|
1276
|
+
submessages: [],
|
|
1277
|
+
|
|
1278
|
+
unifiedResponse: {
|
|
1279
|
+
data
|
|
1280
|
+
},
|
|
1281
|
+
|
|
1282
|
+
contextInfo: {}
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
};
|
|
1287
|
+
|
|
1288
|
+
await sock.relayMessage(
|
|
1289
|
+
jid,
|
|
1290
|
+
message,
|
|
1291
|
+
{}
|
|
1292
|
+
);
|
|
1293
|
+
~~~
|
|
1294
|
+
|
|
1295
|
+
The verification values above are placeholders. They are shown to document the
|
|
1296
|
+
message structure and must not be treated as valid credentials.
|
|
1297
|
+
|
|
1298
|
+
---
|
|
1299
|
+
|
|
1300
|
+
#### 🧩 Custom Primitives
|
|
1301
|
+
|
|
1302
|
+
The `primitive` object is the part that describes the actual content rendered
|
|
1303
|
+
by the client.
|
|
1304
|
+
|
|
1305
|
+
For example, an HTML primitive has this general form:
|
|
1306
|
+
|
|
1307
|
+
~~~js
|
|
1308
|
+
{
|
|
1309
|
+
__typename: 'GenAIaeacdsnwHtmlPrimitive',
|
|
1310
|
+
payload: '<h1>Hello</h1>',
|
|
1311
|
+
trusted_sources: []
|
|
1312
|
+
}
|
|
1313
|
+
~~~
|
|
1314
|
+
|
|
1315
|
+
This means the Rich Message system is not limited to a single HTML helper.
|
|
1316
|
+
Different primitive types can represent different kinds of content.
|
|
1317
|
+
|
|
1318
|
+
When experimenting with undocumented primitives, inspect the actual serialized
|
|
1319
|
+
`unifiedResponse` payload rather than assuming that every primitive behaves like
|
|
1320
|
+
HTML.
|
|
1321
|
+
|
|
1322
|
+
---
|
|
1323
|
+
|
|
1324
|
+
#### 🧰 AIRich Convenience Builder
|
|
1325
|
+
|
|
1326
|
+
`AIRich` provides a higher-level API for constructing the same general structure.
|
|
1327
|
+
|
|
1328
|
+
Create a builder from a socket:
|
|
1329
|
+
|
|
1330
|
+
~~~js
|
|
1331
|
+
const rich = sock.createAIRich();
|
|
1332
|
+
~~~
|
|
1333
|
+
|
|
1334
|
+
Add HTML:
|
|
1335
|
+
|
|
1336
|
+
~~~js
|
|
1337
|
+
rich.addHtml(`
|
|
1338
|
+
<h1>Hello World</h1>
|
|
1339
|
+
<p>Generated by Casileys.</p>
|
|
1340
|
+
`);
|
|
1341
|
+
~~~
|
|
1342
|
+
|
|
1343
|
+
Then send it:
|
|
1344
|
+
|
|
1345
|
+
~~~js
|
|
1346
|
+
await rich.send(jid);
|
|
1347
|
+
~~~
|
|
1348
|
+
|
|
1349
|
+
`createAIRich()`, `addHtml()`, and `send()` are **convenience methods**.
|
|
1350
|
+
They are not required by the Rich Message protocol itself.
|
|
1351
|
+
|
|
1352
|
+
Use the manual `relayMessage()` approach when you need to modify fields that
|
|
1353
|
+
are not exposed by the builder.
|
|
1354
|
+
|
|
1355
|
+
---
|
|
1356
|
+
|
|
1357
|
+
#### 🖥️ Interactive HTML
|
|
1358
|
+
|
|
1359
|
+
Rich HTML can contain normal browser-side JavaScript.
|
|
1360
|
+
|
|
1361
|
+
For example:
|
|
1362
|
+
|
|
1363
|
+
~~~js
|
|
1364
|
+
const rich = sock.createAIRich();
|
|
1365
|
+
|
|
1366
|
+
rich.addHtml(`
|
|
1367
|
+
<button onclick="
|
|
1368
|
+
document.getElementById('counter').textContent++;
|
|
1369
|
+
">
|
|
1370
|
+
Click me
|
|
1371
|
+
</button>
|
|
1372
|
+
|
|
1373
|
+
<p id="counter">0</p>
|
|
1374
|
+
`);
|
|
1375
|
+
|
|
1376
|
+
await rich.send(jid);
|
|
1377
|
+
~~~
|
|
1378
|
+
|
|
1379
|
+
The HTML runs inside the client's Rich Message renderer, so the JavaScript
|
|
1380
|
+
environment is separate from the Node.js environment running Casileys.
|
|
1381
|
+
|
|
1382
|
+
For example:
|
|
1383
|
+
|
|
1384
|
+
~~~js
|
|
1385
|
+
window
|
|
1386
|
+
~~~
|
|
1387
|
+
|
|
1388
|
+
exists inside the HTML renderer, but it does not exist in a normal Node.js
|
|
1389
|
+
evaluation context.
|
|
1390
|
+
|
|
1391
|
+
---
|
|
1392
|
+
|
|
1393
|
+
#### 🌐 WebSocket / Realtime HTML
|
|
1394
|
+
|
|
1395
|
+
The Rich HTML renderer exposes the browser WebSocket API on supported clients.
|
|
1396
|
+
|
|
1397
|
+
This makes realtime HTML applications technically possible without sending a
|
|
1398
|
+
new WhatsApp message for every state update.
|
|
1399
|
+
|
|
1400
|
+
Example:
|
|
1401
|
+
|
|
1402
|
+
~~~js
|
|
1403
|
+
const rich = sock.createAIRich();
|
|
1404
|
+
|
|
1405
|
+
rich.addHtml(`
|
|
1406
|
+
<button id="connect">Connect</button>
|
|
1407
|
+
<pre id="output"></pre>
|
|
1408
|
+
|
|
1409
|
+
<script>
|
|
1410
|
+
const output = document.getElementById('output');
|
|
1411
|
+
|
|
1412
|
+
document.getElementById('connect').onclick = () => {
|
|
1413
|
+
const ws = new WebSocket('wss://example.com');
|
|
1414
|
+
|
|
1415
|
+
ws.onopen = () => {
|
|
1416
|
+
output.textContent = 'Connected';
|
|
1417
|
+
ws.send('HELLO');
|
|
1418
|
+
};
|
|
1419
|
+
|
|
1420
|
+
ws.onmessage = event => {
|
|
1421
|
+
output.textContent = event.data;
|
|
1422
|
+
};
|
|
1423
|
+
|
|
1424
|
+
ws.onerror = () => {
|
|
1425
|
+
output.textContent = 'WebSocket error';
|
|
1426
|
+
};
|
|
1427
|
+
};
|
|
1428
|
+
</script>
|
|
1429
|
+
`);
|
|
1430
|
+
|
|
1431
|
+
await rich.send(jid);
|
|
1432
|
+
~~~
|
|
1433
|
+
|
|
1434
|
+
A realtime multiplayer game can therefore be designed as:
|
|
1435
|
+
|
|
1436
|
+
~~~text
|
|
1437
|
+
WhatsApp Rich HTML
|
|
1438
|
+
│
|
|
1439
|
+
│ WebSocket
|
|
1440
|
+
▼
|
|
1441
|
+
Realtime Game Server
|
|
1442
|
+
│
|
|
1443
|
+
├── Player 1 state
|
|
1444
|
+
├── Player 2 state
|
|
1445
|
+
└── Game state
|
|
1446
|
+
~~~
|
|
1447
|
+
|
|
1448
|
+
Casileys only needs to deliver the initial Rich Message. The realtime game
|
|
1449
|
+
state can then be handled by the HTML client and the external server.
|
|
1450
|
+
|
|
1451
|
+
> Availability of browser APIs and network access depends on the WhatsApp
|
|
1452
|
+
> client and Rich Message renderer.
|
|
1453
|
+
|
|
1454
|
+
---
|
|
1455
|
+
|
|
1125
1456
|
### 🏷️ Find User ID
|
|
1126
1457
|
|
|
1127
1458
|
```js
|