chat-adapter-wwebjs 0.1.2 → 0.1.4
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 +44 -0
- package/README.md +313 -100
- package/dist/index.d.ts +119 -21
- package/dist/index.js +657 -129
- package/package.json +7 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Attachment, FileUpload, AdapterPostableMessage, CardElement, ChatInstance, Message as Message$1, Logger, Author, Adapter, LockScope, ChannelVisibility, WebhookOptions, RawMessage, EmojiValue, FetchOptions, FetchResult, ThreadInfo, ChannelInfo, ListThreadsResult, UserInfo, FormattedContent, BaseFormatConverter, Root } from 'chat';
|
|
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,8 +9,8 @@ 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. */
|
|
@@ -30,22 +30,31 @@ interface WWebJSThreadId {
|
|
|
30
30
|
/** Configuration for the whatsapp-web.js adapter. */
|
|
31
31
|
interface WWebJSAdapterConfig {
|
|
32
32
|
/**
|
|
33
|
-
* Adapter identity used by Chat SDK serialization
|
|
34
|
-
* Set a unique value per account when several run on one `Chat`
|
|
35
|
-
* 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"`.
|
|
36
37
|
*/
|
|
37
38
|
adapterName?: string;
|
|
38
39
|
/** Layers that change how cards are sent or messages received; see `numberedActions()`. */
|
|
39
40
|
addons?: WWebJSAddon[];
|
|
40
41
|
/**
|
|
41
42
|
* Messages that arrived while the session was down are delivered once it is
|
|
42
|
-
* back, up to this old,
|
|
43
|
-
*
|
|
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.
|
|
44
46
|
*/
|
|
45
47
|
catchUpMs?: number | false;
|
|
46
48
|
/**
|
|
47
|
-
* An existing whatsapp-web.js client
|
|
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}.
|
|
49
58
|
*/
|
|
50
59
|
client?: Client;
|
|
51
60
|
/**
|
|
@@ -70,6 +79,15 @@ interface WWebJSAdapterConfig {
|
|
|
70
79
|
handleSignals?: boolean;
|
|
71
80
|
/** Logger instance. Replaced by the Chat SDK logger once initialized. */
|
|
72
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>;
|
|
73
91
|
/**
|
|
74
92
|
* Acknowledge inbound messages automatically, so the sender sees blue ticks
|
|
75
93
|
* as soon as the bot receives them. Defaults to `false`.
|
|
@@ -101,6 +119,12 @@ interface WWebJSAdapterConfig {
|
|
|
101
119
|
* reactions. Defaults to `false`.
|
|
102
120
|
*/
|
|
103
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;
|
|
104
128
|
/**
|
|
105
129
|
* Start Chromium with {@link RECOMMENDED_CHROMIUM_ARGS}, which keep it lean
|
|
106
130
|
* and steady in a container. Defaults to `true`.
|
|
@@ -119,19 +143,45 @@ interface WWebJSAdapterConfig {
|
|
|
119
143
|
* minute; `false` turns it off.
|
|
120
144
|
*/
|
|
121
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";
|
|
122
158
|
/** Bot display name. Defaults to `"wwebjs-bot"`. */
|
|
123
159
|
userName?: string;
|
|
124
160
|
/**
|
|
125
161
|
* Send audio as a voice note, played inline with a waveform, rather than as
|
|
126
|
-
* a file
|
|
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`.
|
|
127
164
|
*/
|
|
128
|
-
voiceNotes?: boolean;
|
|
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;
|
|
129
177
|
}
|
|
130
178
|
/** What an addon is handed alongside each card or message. */
|
|
131
179
|
interface WWebJSAddonContext {
|
|
132
180
|
adapter: WWebJSAdapter;
|
|
133
181
|
/** Absent until the Chat SDK has initialized the adapter. */
|
|
134
182
|
chat: ChatInstance | null;
|
|
183
|
+
/** Whether cards use WhatsApp's markup; see {@link WWebJSAdapterConfig.richContent}. */
|
|
184
|
+
richContent: boolean;
|
|
135
185
|
threadId: string;
|
|
136
186
|
}
|
|
137
187
|
/** A layer over the adapter; every hook is optional. */
|
|
@@ -255,6 +305,15 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
255
305
|
/** The underlying whatsapp-web.js client, for anything this adapter does not wrap. */
|
|
256
306
|
readonly client: Client;
|
|
257
307
|
userName: string;
|
|
308
|
+
/**
|
|
309
|
+
* WhatsApp has no modals, so with `richContent` one becomes a conversation:
|
|
310
|
+
* each field is asked in turn, a select by number, and the answers reach
|
|
311
|
+
* `onModalSubmit`. `cancel`, or the modal's `closeLabel`, stops it. Absent
|
|
312
|
+
* otherwise, so the SDK knows a modal cannot open.
|
|
313
|
+
*/
|
|
314
|
+
readonly openModal?: (triggerId: string, modal: ModalElement, contextId?: string) => Promise<{
|
|
315
|
+
viewId: string;
|
|
316
|
+
}>;
|
|
258
317
|
private readonly _config;
|
|
259
318
|
private readonly _converter;
|
|
260
319
|
private readonly _ackHandlers;
|
|
@@ -271,8 +330,15 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
271
330
|
private _state;
|
|
272
331
|
private _watchdog?;
|
|
273
332
|
private _strikes;
|
|
333
|
+
private readonly _owned;
|
|
334
|
+
private _attempts;
|
|
335
|
+
private _launch?;
|
|
336
|
+
private _unwatch?;
|
|
337
|
+
private _listeners;
|
|
274
338
|
private _unsignal?;
|
|
275
339
|
private _seen?;
|
|
340
|
+
private _behind?;
|
|
341
|
+
private readonly _recent;
|
|
276
342
|
private _saving;
|
|
277
343
|
constructor(config?: WWebJSAdapterConfig);
|
|
278
344
|
/** Where the session stands; a health check reads `connection === "connected"`. */
|
|
@@ -287,7 +353,7 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
287
353
|
* share one connection attempt.
|
|
288
354
|
*/
|
|
289
355
|
connect(): Promise<void>;
|
|
290
|
-
/** Close the browser session. */
|
|
356
|
+
/** Close the browser session, or only detach from a client the adapter does not manage. */
|
|
291
357
|
disconnect(): Promise<void>;
|
|
292
358
|
encodeThreadId(platformData: WWebJSThreadId): string;
|
|
293
359
|
decodeThreadId(threadId: string): WWebJSThreadId;
|
|
@@ -304,16 +370,22 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
304
370
|
postChannelMessage(channelId: string, message: AdapterPostableMessage): Promise<RawMessage<Message>>;
|
|
305
371
|
reply(threadId: string, messageId: string, message: AdapterPostableMessage): Promise<RawMessage<Message>>;
|
|
306
372
|
editMessage(threadId: string, messageId: string, message: AdapterPostableMessage): Promise<RawMessage<Message>>;
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
373
|
+
/** WhatsApp cannot stream, and editing twice a second as the SDK otherwise would floods the chat; the reply goes out once, whole. */
|
|
374
|
+
stream(threadId: string, textStream: AsyncIterable<string | StreamChunk>): Promise<RawMessage<Message> | null>;
|
|
375
|
+
deleteMessage(threadId: string, messageId: string): Promise<void>;
|
|
376
|
+
addReaction(threadId: string, messageId: string, emoji: EmojiValue | string): Promise<void>;
|
|
377
|
+
removeReaction(threadId: string, messageId: string, _emoji: EmojiValue | string): Promise<void>;
|
|
310
378
|
fetchMessages(threadId: string, options?: FetchOptions): Promise<FetchResult<Message>>;
|
|
311
379
|
fetchChannelMessages(channelId: string, options?: FetchOptions): Promise<FetchResult<Message>>;
|
|
312
|
-
fetchMessage(
|
|
380
|
+
fetchMessage(threadId: string, messageId: string): Promise<Message$1<Message> | null>;
|
|
313
381
|
fetchThread(threadId: string): Promise<ThreadInfo>;
|
|
314
382
|
fetchChannelInfo(channelId: string): Promise<ChannelInfo>;
|
|
315
|
-
/** WhatsApp
|
|
316
|
-
listThreads(): Promise<ListThreadsResult<Message>>;
|
|
383
|
+
/** A WhatsApp chat is one flat conversation, so a channel holds just its own thread. */
|
|
384
|
+
listThreads(channelId: string): Promise<ListThreadsResult<Message>>;
|
|
385
|
+
/** Every chat the account has, newest first, with its latest message: an inbox's list. */
|
|
386
|
+
listChats(options?: ListThreadsOptions): Promise<ListThreadsResult<Message>>;
|
|
387
|
+
private _summaries;
|
|
388
|
+
private _openModal;
|
|
317
389
|
getUser(userId: string): Promise<UserInfo | null>;
|
|
318
390
|
/** 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. */
|
|
319
391
|
openDM(userId: string): Promise<string>;
|
|
@@ -383,15 +455,34 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
383
455
|
private _clientOptions;
|
|
384
456
|
private _connect;
|
|
385
457
|
private _open;
|
|
458
|
+
/** A crashed browser ends the session without WhatsApp ever reporting a disconnect. */
|
|
459
|
+
private _watchBrowser;
|
|
460
|
+
/** A client started elsewhere is up when its browser is connected and WhatsApp answers. */
|
|
461
|
+
private _running;
|
|
462
|
+
/** Closes the browser this adapter runs; closing it on purpose is not a crash to recover from. */
|
|
463
|
+
private _close;
|
|
464
|
+
/** `destroy()` closes only a browser it still sees connected, so one that dropped keeps its profile locked. */
|
|
465
|
+
private _killBrowser;
|
|
386
466
|
private _listen;
|
|
467
|
+
private _unlisten;
|
|
468
|
+
private _onReady;
|
|
387
469
|
private _onMessage;
|
|
470
|
+
private _formKey;
|
|
471
|
+
private _texts;
|
|
472
|
+
private _start;
|
|
473
|
+
/** A reply from someone filling in a modal answers it, before anything else sees it. */
|
|
474
|
+
private _answer;
|
|
475
|
+
/** WhatsApp has no slash commands, so a message naming a declared one becomes one. */
|
|
476
|
+
private _command;
|
|
388
477
|
private _claimed;
|
|
389
478
|
private _addons;
|
|
390
479
|
private _context;
|
|
391
480
|
private _seenKey;
|
|
392
481
|
/** Writes run in turn, so an older position never lands after a newer one. */
|
|
393
482
|
private _save;
|
|
483
|
+
/** While a catch-up runs the position holds still, so a crash part-way replays rather than skips. */
|
|
394
484
|
private _remember;
|
|
485
|
+
private _advance;
|
|
395
486
|
/** WhatsApp Web loads what arrived while the session was down without announcing every message. */
|
|
396
487
|
private _catchUp;
|
|
397
488
|
private _onEdit;
|
|
@@ -415,6 +506,7 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
415
506
|
private _guard;
|
|
416
507
|
private _isOwn;
|
|
417
508
|
private _onDisconnected;
|
|
509
|
+
private _reconnects;
|
|
418
510
|
private _reconnect;
|
|
419
511
|
private _watch;
|
|
420
512
|
/** Revives a session that died without an event, or a first connection that failed. */
|
|
@@ -428,6 +520,8 @@ declare class WWebJSAdapter implements Adapter<WWebJSThreadId, Message> {
|
|
|
428
520
|
private _page;
|
|
429
521
|
private _findChat;
|
|
430
522
|
private _requireChat;
|
|
523
|
+
/** A DM can be addressed by phone or by LID, so only a group's messages must match the thread exactly. */
|
|
524
|
+
private _findMessage;
|
|
431
525
|
private _requireMessage;
|
|
432
526
|
}
|
|
433
527
|
/** Create a WhatsApp adapter backed by whatsapp-web.js. */
|
|
@@ -472,16 +566,20 @@ declare function toUser(chatId: string): string;
|
|
|
472
566
|
/**
|
|
473
567
|
* Rewrite Chat SDK mention tokens into WhatsApp's `@number` form and collect
|
|
474
568
|
* the chat IDs WhatsApp needs alongside the body to render them as mentions.
|
|
569
|
+
* A bare `@number` stays text, so echoing what someone wrote pings no one.
|
|
475
570
|
*/
|
|
476
571
|
declare function extractMentions(text: string): {
|
|
477
572
|
mentions: string[];
|
|
478
573
|
text: string;
|
|
479
574
|
};
|
|
480
575
|
|
|
576
|
+
/** Opaque to the SDK; carries where a modal opens and for whom, since WhatsApp has no trigger of its own. */
|
|
577
|
+
declare function triggerIdOf(threadId: string, userId: string): string;
|
|
578
|
+
|
|
481
579
|
interface NumberedActionsOptions {
|
|
482
580
|
/** Also deliver the reply that picked an option as a message. Defaults to `false`. */
|
|
483
581
|
keepMessage?: boolean;
|
|
484
|
-
/** How long a card's options answer to their number or label. Defaults to 1 day. */
|
|
582
|
+
/** How long a card's options answer to their number or label; the first pick ends them. Defaults to 1 day. */
|
|
485
583
|
ttlMs?: number;
|
|
486
584
|
}
|
|
487
585
|
/**
|
|
@@ -490,4 +588,4 @@ interface NumberedActionsOptions {
|
|
|
490
588
|
*/
|
|
491
589
|
declare function numberedActions(options?: NumberedActionsOptions): WWebJSAddon;
|
|
492
590
|
|
|
493
|
-
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 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 };
|
|
591
|
+
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 };
|