chat-adapter-wwebjs 0.1.0 → 0.1.3
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/CHANGELOG.md +51 -0
- package/README.md +320 -85
- package/dist/index.d.ts +190 -19
- package/dist/index.js +834 -108
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Attachment, FileUpload, AdapterPostableMessage, Logger, Author, Adapter, LockScope,
|
|
1
|
+
import { Attachment, FileUpload, AdapterPostableMessage, CardElement, ChatInstance, Message as Message$1, Logger, Author, Adapter, LockScope, ModalElement, ChannelVisibility, WebhookOptions, RawMessage, StreamChunk, EmojiValue, FetchOptions, FetchResult, ThreadInfo, ChannelInfo, ListThreadsResult, ListThreadsOptions, UserInfo, FormattedContent, BaseFormatConverter, Root } from 'chat';
|
|
2
2
|
import { MessageMedia, Message, Client, ClientOptions, PollVote, Call, GroupNotification } from 'whatsapp-web.js';
|
|
3
3
|
|
|
4
4
|
/** Describe an inbound message's media as a Chat SDK attachment. */
|
|
@@ -9,12 +9,13 @@ declare function download(message: Message, maxBytes?: number): Promise<Buffer>;
|
|
|
9
9
|
declare function toMediaList(message: AdapterPostableMessage, maxBytes?: number): Promise<MessageMedia[]>;
|
|
10
10
|
/**
|
|
11
11
|
* Convert a single attachment or file upload into WhatsApp media. An attachment
|
|
12
|
-
* given only by `url` is fetched
|
|
13
|
-
*
|
|
12
|
+
* given only by `url` is fetched over HTTPS, and never from a private or
|
|
13
|
+
* internal address.
|
|
14
14
|
*/
|
|
15
15
|
declare function toMedia(source: Attachment | FileUpload, maxBytes?: number): Promise<MessageMedia>;
|
|
16
16
|
/** WhatsApp renders a caption on every media kind except audio. */
|
|
17
17
|
declare function isCaptionable(media: MessageMedia): boolean;
|
|
18
|
+
declare function isAudio(media: MessageMedia): boolean;
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
21
|
* Where the WhatsApp session stands. Only `connected` can send and receive;
|
|
@@ -29,26 +30,64 @@ interface WWebJSThreadId {
|
|
|
29
30
|
/** Configuration for the whatsapp-web.js adapter. */
|
|
30
31
|
interface WWebJSAdapterConfig {
|
|
31
32
|
/**
|
|
32
|
-
* Adapter identity used by Chat SDK serialization
|
|
33
|
-
* Set a unique value per account when several run on one `Chat`
|
|
34
|
-
* Must not contain `:`. Defaults to
|
|
33
|
+
* Adapter identity used by Chat SDK serialization, thread ID prefixes and
|
|
34
|
+
* state keys. Set a unique value per account when several run on one `Chat`
|
|
35
|
+
* instance or share one state store. Must not contain `:`. Defaults to
|
|
36
|
+
* `"wwebjs"`.
|
|
35
37
|
*/
|
|
36
38
|
adapterName?: string;
|
|
39
|
+
/** Layers that change how cards are sent or messages received; see `numberedActions()`. */
|
|
40
|
+
addons?: WWebJSAddon[];
|
|
37
41
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
42
|
+
* Messages that arrived while the session was down are delivered once it is
|
|
43
|
+
* back, up to this old, oldest first. A restart part-way through delivers
|
|
44
|
+
* them again rather than skip any, and the Chat SDK drops a repeat it has
|
|
45
|
+
* already handled. Defaults to 1 day; `false` turns it off.
|
|
46
|
+
*/
|
|
47
|
+
catchUpMs?: number | false;
|
|
48
|
+
/**
|
|
49
|
+
* An existing whatsapp-web.js client, exposed as `adapter.client`. One
|
|
50
|
+
* already started is bridged as it is: `connect()` adopts its session
|
|
51
|
+
* without a second `initialize()`, its options and listeners are left alone,
|
|
52
|
+
* and its lifecycle stays yours — the adapter never reconnects or destroys
|
|
53
|
+
* it, nor closes it on a signal, and `disconnect()` only detaches the
|
|
54
|
+
* adapter. One never started is the adapter's to run, as if it had built
|
|
55
|
+
* it; {@link WWebJSAdapterConfig.manageClient} decides otherwise. When
|
|
56
|
+
* omitted the adapter constructs one from
|
|
57
|
+
* {@link WWebJSAdapterConfig.clientOptions}.
|
|
40
58
|
*/
|
|
41
59
|
client?: Client;
|
|
42
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* Options for the client the adapter constructs, Puppeteer's included.
|
|
62
|
+
* `puppeteer.args` are added after {@link RECOMMENDED_CHROMIUM_ARGS}, so a
|
|
63
|
+
* flag given here wins. Ignored when `client` is set.
|
|
64
|
+
*/
|
|
43
65
|
clientOptions?: ClientOptions;
|
|
44
66
|
/**
|
|
45
67
|
* How long opening the session may take before it is abandoned — and, with
|
|
46
|
-
* `reconnect`, tried again. whatsapp-web.js itself never gives up.
|
|
47
|
-
*
|
|
68
|
+
* `reconnect`, tried again. whatsapp-web.js itself never gives up. Once a QR
|
|
69
|
+
* or pairing code is shown it waits on a person instead, without limit.
|
|
70
|
+
* Defaults to 2 minutes.
|
|
48
71
|
*/
|
|
49
72
|
connectTimeoutMs?: number;
|
|
73
|
+
/**
|
|
74
|
+
* Close the session on SIGINT, SIGTERM and SIGHUP, then let the signal end
|
|
75
|
+
* the process as it otherwise would — unless the app listens for it, and
|
|
76
|
+
* shuts down itself. Puppeteer alone closes the browser and leaves the
|
|
77
|
+
* process running. Defaults to `true`.
|
|
78
|
+
*/
|
|
79
|
+
handleSignals?: boolean;
|
|
50
80
|
/** Logger instance. Replaced by the Chat SDK logger once initialized. */
|
|
51
81
|
logger?: Logger;
|
|
82
|
+
/**
|
|
83
|
+
* Force who runs a `client` you passed, whatever its state: `true` has the
|
|
84
|
+
* adapter reconnect it, destroy it on `disconnect()` and close it on a
|
|
85
|
+
* signal; `false` only bridges it. Decided by whether it was started when
|
|
86
|
+
* omitted.
|
|
87
|
+
*/
|
|
88
|
+
manageClient?: boolean;
|
|
89
|
+
/** The few phrases a conversational modal sends, to put them in your users' language. */
|
|
90
|
+
modalTexts?: Partial<WWebJSModalTexts>;
|
|
52
91
|
/**
|
|
53
92
|
* Acknowledge inbound messages automatically, so the sender sees blue ticks
|
|
54
93
|
* as soon as the bot receives them. Defaults to `false`.
|
|
@@ -80,6 +119,17 @@ interface WWebJSAdapterConfig {
|
|
|
80
119
|
* reactions. Defaults to `false`.
|
|
81
120
|
*/
|
|
82
121
|
receiveOwnMessages?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Render what WhatsApp can only imitate: cards in its markup with their
|
|
124
|
+
* images as media, and modals as conversations. Off, a card is plain text
|
|
125
|
+
* and the adapter has no `openModal`. Defaults to `false`.
|
|
126
|
+
*/
|
|
127
|
+
richContent?: boolean;
|
|
128
|
+
/**
|
|
129
|
+
* Start Chromium with {@link RECOMMENDED_CHROMIUM_ARGS}, which keep it lean
|
|
130
|
+
* and steady in a container. Defaults to `true`.
|
|
131
|
+
*/
|
|
132
|
+
recommendedArgs?: boolean;
|
|
83
133
|
/**
|
|
84
134
|
* Open the session again when WhatsApp drops it, waiting longer after each
|
|
85
135
|
* failed attempt. A logout is never retried, since it needs a new pairing.
|
|
@@ -93,8 +143,53 @@ interface WWebJSAdapterConfig {
|
|
|
93
143
|
* minute; `false` turns it off.
|
|
94
144
|
*/
|
|
95
145
|
watchdogIntervalMs?: number | false;
|
|
146
|
+
/**
|
|
147
|
+
* Commands, such as `"/help"`, whose messages reach `onSlashCommand` rather
|
|
148
|
+
* than the message handlers. `"/help me"` arrives as command `/help` with
|
|
149
|
+
* text `"me"`. Any other text starting with `/` stays a message.
|
|
150
|
+
*/
|
|
151
|
+
slashCommands?: string[];
|
|
152
|
+
/**
|
|
153
|
+
* How a streamed reply reaches WhatsApp. `"buffer"` sends it once, whole;
|
|
154
|
+
* `"edit"` sends the first words and edits the message as the rest
|
|
155
|
+
* arrives, at most once a second. Defaults to `"buffer"`.
|
|
156
|
+
*/
|
|
157
|
+
streaming?: "buffer" | "edit";
|
|
96
158
|
/** Bot display name. Defaults to `"wwebjs-bot"`. */
|
|
97
159
|
userName?: string;
|
|
160
|
+
/**
|
|
161
|
+
* Send audio as a voice note, played inline with a waveform, rather than as
|
|
162
|
+
* a file: `true` for all audio, `"ogg"` for OGG/Opus only, which every
|
|
163
|
+
* phone plays as one, `false` for none. Defaults to `true`.
|
|
164
|
+
*/
|
|
165
|
+
voiceNotes?: boolean | "ogg";
|
|
166
|
+
}
|
|
167
|
+
/** The phrases a conversational modal sends; `cancel` and `skip` are the words people answer with. */
|
|
168
|
+
interface WWebJSModalTexts {
|
|
169
|
+
cancel: string;
|
|
170
|
+
cancelHint: string;
|
|
171
|
+
cancelled: string;
|
|
172
|
+
invalid: string;
|
|
173
|
+
noResults: string;
|
|
174
|
+
search: string;
|
|
175
|
+
skip: string;
|
|
176
|
+
skipHint: string;
|
|
177
|
+
}
|
|
178
|
+
/** What an addon is handed alongside each card or message. */
|
|
179
|
+
interface WWebJSAddonContext {
|
|
180
|
+
adapter: WWebJSAdapter;
|
|
181
|
+
/** Absent until the Chat SDK has initialized the adapter. */
|
|
182
|
+
chat: ChatInstance | null;
|
|
183
|
+
threadId: string;
|
|
184
|
+
}
|
|
185
|
+
/** A layer over the adapter; every hook is optional. */
|
|
186
|
+
interface WWebJSAddon {
|
|
187
|
+
/** Text for a card in place of its plain fallback; the first addon to answer wins. */
|
|
188
|
+
renderCard?(card: CardElement, context: WWebJSAddonContext): string | undefined;
|
|
189
|
+
/** Called once a card is on WhatsApp, with the id of the message carrying it. */
|
|
190
|
+
cardSent?(card: CardElement, messageId: string, context: WWebJSAddonContext): unknown;
|
|
191
|
+
/** Return `true` to keep an inbound message from reaching the Chat SDK as one. */
|
|
192
|
+
onMessage?(message: Message$1<Message>, context: WWebJSAddonContext): boolean | Promise<boolean>;
|
|
98
193
|
}
|
|
99
194
|
/** Delivery state of a sent message, reported by {@link WWebJSAdapter.onAck}. */
|
|
100
195
|
type WWebJSAckStatus = "delivered" | "error" | "pending" | "played" | "read" | "sent";
|
|
@@ -200,12 +295,23 @@ type WWebJSGroupEventHandler = (event: WWebJSGroupEvent) => unknown;
|
|
|
200
295
|
/** Handler for poll votes. */
|
|
201
296
|
type WWebJSPollVoteHandler = (vote: WWebJSPollVote) => unknown;
|
|
202
297
|
|
|
298
|
+
/** What a single WhatsApp Web tab can do without, on top of the flags Puppeteer already passes. */
|
|
299
|
+
declare const RECOMMENDED_CHROMIUM_ARGS: string[];
|
|
203
300
|
declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
204
301
|
readonly name: string;
|
|
205
302
|
readonly lockScope: LockScope;
|
|
206
303
|
/** The underlying whatsapp-web.js client, for anything this adapter does not wrap. */
|
|
207
304
|
readonly client: Client;
|
|
208
305
|
userName: string;
|
|
306
|
+
/**
|
|
307
|
+
* WhatsApp has no modals, so with `richContent` one becomes a conversation:
|
|
308
|
+
* each field is asked in turn, a select by number, and the answers reach
|
|
309
|
+
* `onModalSubmit`. `cancel`, or the modal's `closeLabel`, stops it. Absent
|
|
310
|
+
* otherwise, so the SDK knows a modal cannot open.
|
|
311
|
+
*/
|
|
312
|
+
readonly openModal?: (triggerId: string, modal: ModalElement, contextId?: string) => Promise<{
|
|
313
|
+
viewId: string;
|
|
314
|
+
}>;
|
|
209
315
|
private readonly _config;
|
|
210
316
|
private readonly _converter;
|
|
211
317
|
private readonly _ackHandlers;
|
|
@@ -222,6 +328,16 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
222
328
|
private _state;
|
|
223
329
|
private _watchdog?;
|
|
224
330
|
private _strikes;
|
|
331
|
+
private readonly _owned;
|
|
332
|
+
private _attempts;
|
|
333
|
+
private _launch?;
|
|
334
|
+
private _unwatch?;
|
|
335
|
+
private _listeners;
|
|
336
|
+
private _unsignal?;
|
|
337
|
+
private _seen?;
|
|
338
|
+
private _behind?;
|
|
339
|
+
private readonly _recent;
|
|
340
|
+
private _saving;
|
|
225
341
|
constructor(config?: WWebJSAdapterConfig);
|
|
226
342
|
/** Where the session stands; a health check reads `connection === "connected"`. */
|
|
227
343
|
get connection(): WWebJSConnection;
|
|
@@ -235,7 +351,7 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
235
351
|
* share one connection attempt.
|
|
236
352
|
*/
|
|
237
353
|
connect(): Promise<void>;
|
|
238
|
-
/** Close the browser session. */
|
|
354
|
+
/** Close the browser session, or only detach from a client the adapter does not manage. */
|
|
239
355
|
disconnect(): Promise<void>;
|
|
240
356
|
encodeThreadId(platformData: WWebJSThreadId): string;
|
|
241
357
|
decodeThreadId(threadId: string): WWebJSThreadId;
|
|
@@ -252,16 +368,22 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
252
368
|
postChannelMessage(channelId: string, message: AdapterPostableMessage): Promise<RawMessage<Message>>;
|
|
253
369
|
reply(threadId: string, messageId: string, message: AdapterPostableMessage): Promise<RawMessage<Message>>;
|
|
254
370
|
editMessage(threadId: string, messageId: string, message: AdapterPostableMessage): Promise<RawMessage<Message>>;
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
371
|
+
/** WhatsApp cannot stream, and editing twice a second as the SDK otherwise would floods the chat; the reply goes out once, whole. */
|
|
372
|
+
stream(threadId: string, textStream: AsyncIterable<string | StreamChunk>): Promise<RawMessage<Message> | null>;
|
|
373
|
+
deleteMessage(threadId: string, messageId: string): Promise<void>;
|
|
374
|
+
addReaction(threadId: string, messageId: string, emoji: EmojiValue | string): Promise<void>;
|
|
375
|
+
removeReaction(threadId: string, messageId: string, _emoji: EmojiValue | string): Promise<void>;
|
|
258
376
|
fetchMessages(threadId: string, options?: FetchOptions): Promise<FetchResult<Message>>;
|
|
259
377
|
fetchChannelMessages(channelId: string, options?: FetchOptions): Promise<FetchResult<Message>>;
|
|
260
|
-
fetchMessage(
|
|
378
|
+
fetchMessage(threadId: string, messageId: string): Promise<Message$1<Message> | null>;
|
|
261
379
|
fetchThread(threadId: string): Promise<ThreadInfo>;
|
|
262
380
|
fetchChannelInfo(channelId: string): Promise<ChannelInfo>;
|
|
263
|
-
/** WhatsApp
|
|
264
|
-
listThreads(): Promise<ListThreadsResult<Message>>;
|
|
381
|
+
/** A WhatsApp chat is one flat conversation, so a channel holds just its own thread. */
|
|
382
|
+
listThreads(channelId: string): Promise<ListThreadsResult<Message>>;
|
|
383
|
+
/** Every chat the account has, newest first, with its latest message: an inbox's list. */
|
|
384
|
+
listChats(options?: ListThreadsOptions): Promise<ListThreadsResult<Message>>;
|
|
385
|
+
private _summaries;
|
|
386
|
+
private _openModal;
|
|
265
387
|
getUser(userId: string): Promise<UserInfo | null>;
|
|
266
388
|
/** A number, or its `@c.us` id, resolves to the chat WhatsApp uses for it — often a LID — so a DM is one thread whoever opened it. */
|
|
267
389
|
openDM(userId: string): Promise<string>;
|
|
@@ -331,8 +453,36 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
331
453
|
private _clientOptions;
|
|
332
454
|
private _connect;
|
|
333
455
|
private _open;
|
|
456
|
+
/** A crashed browser ends the session without WhatsApp ever reporting a disconnect. */
|
|
457
|
+
private _watchBrowser;
|
|
458
|
+
/** A client started elsewhere is up when its browser is connected and WhatsApp answers. */
|
|
459
|
+
private _running;
|
|
460
|
+
/** Closes the browser this adapter runs; closing it on purpose is not a crash to recover from. */
|
|
461
|
+
private _close;
|
|
462
|
+
/** `destroy()` closes only a browser it still sees connected, so one that dropped keeps its profile locked. */
|
|
463
|
+
private _killBrowser;
|
|
334
464
|
private _listen;
|
|
465
|
+
private _unlisten;
|
|
466
|
+
private _onReady;
|
|
335
467
|
private _onMessage;
|
|
468
|
+
private _formKey;
|
|
469
|
+
private _texts;
|
|
470
|
+
private _start;
|
|
471
|
+
/** A reply from someone filling in a modal answers it, before anything else sees it. */
|
|
472
|
+
private _answer;
|
|
473
|
+
/** WhatsApp has no slash commands, so a message naming a declared one becomes one. */
|
|
474
|
+
private _command;
|
|
475
|
+
private _claimed;
|
|
476
|
+
private _addons;
|
|
477
|
+
private _context;
|
|
478
|
+
private _seenKey;
|
|
479
|
+
/** Writes run in turn, so an older position never lands after a newer one. */
|
|
480
|
+
private _save;
|
|
481
|
+
/** While a catch-up runs the position holds still, so a crash part-way replays rather than skips. */
|
|
482
|
+
private _remember;
|
|
483
|
+
private _advance;
|
|
484
|
+
/** WhatsApp Web loads what arrived while the session was down without announcing every message. */
|
|
485
|
+
private _catchUp;
|
|
336
486
|
private _onEdit;
|
|
337
487
|
private _onRevoke;
|
|
338
488
|
private _onReaction;
|
|
@@ -354,17 +504,22 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
354
504
|
private _guard;
|
|
355
505
|
private _isOwn;
|
|
356
506
|
private _onDisconnected;
|
|
507
|
+
private _reconnects;
|
|
357
508
|
private _reconnect;
|
|
358
509
|
private _watch;
|
|
359
510
|
/** Revives a session that died without an event, or a first connection that failed. */
|
|
360
511
|
private _check;
|
|
361
512
|
private _render;
|
|
513
|
+
/** The card is already on WhatsApp, so an addon failing here must not fail the send. */
|
|
514
|
+
private _cardSent;
|
|
362
515
|
private _send;
|
|
363
516
|
private _sendContent;
|
|
364
517
|
private _raw;
|
|
365
518
|
private _page;
|
|
366
519
|
private _findChat;
|
|
367
520
|
private _requireChat;
|
|
521
|
+
/** A DM can be addressed by phone or by LID, so only a group's messages must match the thread exactly. */
|
|
522
|
+
private _findMessage;
|
|
368
523
|
private _requireMessage;
|
|
369
524
|
}
|
|
370
525
|
/** Create a WhatsApp adapter backed by whatsapp-web.js. */
|
|
@@ -409,10 +564,26 @@ declare function toUser(chatId: string): string;
|
|
|
409
564
|
/**
|
|
410
565
|
* Rewrite Chat SDK mention tokens into WhatsApp's `@number` form and collect
|
|
411
566
|
* the chat IDs WhatsApp needs alongside the body to render them as mentions.
|
|
567
|
+
* A bare `@number` stays text, so echoing what someone wrote pings no one.
|
|
412
568
|
*/
|
|
413
569
|
declare function extractMentions(text: string): {
|
|
414
570
|
mentions: string[];
|
|
415
571
|
text: string;
|
|
416
572
|
};
|
|
417
573
|
|
|
418
|
-
|
|
574
|
+
/** Opaque to the SDK; carries where a modal opens and for whom, since WhatsApp has no trigger of its own. */
|
|
575
|
+
declare function triggerIdOf(threadId: string, userId: string): string;
|
|
576
|
+
|
|
577
|
+
interface NumberedActionsOptions {
|
|
578
|
+
/** Also deliver the reply that picked an option as a message. Defaults to `false`. */
|
|
579
|
+
keepMessage?: boolean;
|
|
580
|
+
/** How long a card's options answer to their number or label; the first pick ends them. Defaults to 1 day. */
|
|
581
|
+
ttlMs?: number;
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* WhatsApp Web cannot send buttons, so a card's buttons and select options are
|
|
585
|
+
* listed by number, and a reply with one — or its label — reaches `onAction`.
|
|
586
|
+
*/
|
|
587
|
+
declare function numberedActions(options?: NumberedActionsOptions): WWebJSAddon;
|
|
588
|
+
|
|
589
|
+
export { type NumberedActionsOptions, RECOMMENDED_CHROMIUM_ARGS, type WWebJSAck, type WWebJSAckHandler, type WWebJSAckStatus, WWebJSAdapter, type WWebJSAdapterConfig, type WWebJSAddon, type WWebJSAddonContext, type WWebJSCallHandler, type WWebJSConnection, WWebJSFormatConverter, type WWebJSGroupEvent, type WWebJSGroupEventHandler, type WWebJSGroupParticipant, type WWebJSModalTexts, type WWebJSPollVote, type WWebJSPollVoteHandler, type WWebJSSendEventArgs, type WWebJSSendLocationArgs, type WWebJSSendPollArgs, type WWebJSThreadId, createWWebJSAdapter, download, extractMentions, isAudio, isCaptionable, isGroupId, isNewsletterId, isUserId, isWWebJSAdapter, numberedActions, requireWWebJSAdapter, toAttachments, toChatId, toMedia, toMediaList, toUser, triggerIdOf };
|