@theophilusdev/conduit 1.0.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 +674 -0
- package/README.md +139 -0
- package/dist/index.cjs +648 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +648 -0
- package/dist/index.d.ts +648 -0
- package/dist/index.mjs +640 -0
- package/dist/index.mjs.map +1 -0
- package/docs/DOCS.md +539 -0
- package/package.json +46 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fcaUnofficial = require('@dongdev/fca-unofficial');
|
|
4
|
+
|
|
5
|
+
// src/client/ConduitClient.ts
|
|
6
|
+
|
|
7
|
+
// src/utils/toFcaEvent.ts
|
|
8
|
+
var FCA_EVENT_MAP = {
|
|
9
|
+
"message:create": "message",
|
|
10
|
+
"message:remove": "message_unsend",
|
|
11
|
+
"message:react": "message_reaction",
|
|
12
|
+
"message:respond": "message_reply",
|
|
13
|
+
"message:writing": "typ",
|
|
14
|
+
"message:read": "read_receipt",
|
|
15
|
+
"user:create": "event",
|
|
16
|
+
// log:subscribe
|
|
17
|
+
"user:remove": "event",
|
|
18
|
+
// log:unsubscribe
|
|
19
|
+
"thread:update": "event",
|
|
20
|
+
"thread:title_change": "event",
|
|
21
|
+
// log:thread-name
|
|
22
|
+
"thread:photo_replaced": "event",
|
|
23
|
+
// log:thread-image
|
|
24
|
+
"thread:theme_changed": "event",
|
|
25
|
+
// log:thread-color
|
|
26
|
+
"thread:nickname_changed": "event",
|
|
27
|
+
// log:user-nickname
|
|
28
|
+
"thread:admin_changed": "event"
|
|
29
|
+
// log:admin-text
|
|
30
|
+
};
|
|
31
|
+
function toFcaEvent(event) {
|
|
32
|
+
return FCA_EVENT_MAP[event];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/errors/ConduitError.ts
|
|
36
|
+
var ConduitError = class _ConduitError extends Error {
|
|
37
|
+
constructor(message) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "ConduitError";
|
|
40
|
+
}
|
|
41
|
+
static new(message) {
|
|
42
|
+
return new _ConduitError(message);
|
|
43
|
+
}
|
|
44
|
+
static uninitializedClient() {
|
|
45
|
+
return _ConduitError.new(
|
|
46
|
+
"Conduit client not yet initialized. Please call the .login(credentials) method"
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/api/ConduitMessagesAPI.ts
|
|
52
|
+
var ConduitMessagesAPI = class {
|
|
53
|
+
constructor(bot) {
|
|
54
|
+
this.bot = bot;
|
|
55
|
+
}
|
|
56
|
+
bot;
|
|
57
|
+
/**
|
|
58
|
+
* Sends a message to a thread.
|
|
59
|
+
* @param body - The message text.
|
|
60
|
+
* @param threadID - The target thread ID.
|
|
61
|
+
*/
|
|
62
|
+
send(body, threadID) {
|
|
63
|
+
return this.bot.ctx.api.sendMessage({ body }, threadID);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Sends a quoted reply to a specific message.
|
|
67
|
+
* @param body - The reply text.
|
|
68
|
+
* @param threadID - The target thread ID.
|
|
69
|
+
* @param messageID - The message ID to reply to.
|
|
70
|
+
*/
|
|
71
|
+
reply(body, threadID, messageID) {
|
|
72
|
+
return this.bot.ctx.api.sendMessage(
|
|
73
|
+
{ body },
|
|
74
|
+
threadID,
|
|
75
|
+
void 0,
|
|
76
|
+
messageID
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Edits an existing message.
|
|
81
|
+
* @param messageID - The message ID to edit.
|
|
82
|
+
* @param body - The new message text.
|
|
83
|
+
*/
|
|
84
|
+
edit(messageID, body) {
|
|
85
|
+
return this.bot.ctx.api.editMessage(body, messageID);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Unsends (retracts) a message sent by the bot.
|
|
89
|
+
* @param messageID - The message ID to unsend.
|
|
90
|
+
*/
|
|
91
|
+
unsend(messageID) {
|
|
92
|
+
return this.bot.ctx.api.unsendMessage(messageID);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Deletes a message.
|
|
96
|
+
* @param messageID - The message ID to delete.
|
|
97
|
+
*/
|
|
98
|
+
delete(messageID) {
|
|
99
|
+
return this.bot.ctx.api.deleteMessage(messageID);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Adds or removes a reaction on a message.
|
|
103
|
+
* @param emoji - The emoji reaction string.
|
|
104
|
+
* @param messageID - The target message ID.
|
|
105
|
+
* @param threadID - The thread the message belongs to.
|
|
106
|
+
*/
|
|
107
|
+
react(emoji, messageID, threadID) {
|
|
108
|
+
return this.bot.ctx.api.setMessageReaction(emoji, messageID, threadID);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Sends a typing indicator to a thread.
|
|
112
|
+
* @param threadID - The target thread ID.
|
|
113
|
+
*/
|
|
114
|
+
sendTypingIndicator(threadID) {
|
|
115
|
+
return this.bot.ctx.api.sendTypingIndicator(threadID);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Marks a message as read.
|
|
119
|
+
* @param messageID - The message ID to mark as read.
|
|
120
|
+
*/
|
|
121
|
+
markAsRead(messageID) {
|
|
122
|
+
return this.bot.ctx.api.markAsRead(messageID);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Uploads a file attachment and returns an attachment object.
|
|
126
|
+
* @param file - A readable stream or file buffer.
|
|
127
|
+
*/
|
|
128
|
+
uploadAttachment(file) {
|
|
129
|
+
return this.bot.ctx.api.uploadAttachment(file);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Forwards an existing attachment to another thread.
|
|
133
|
+
* @param attachmentID - The attachment ID to forward.
|
|
134
|
+
* @param threadID - The target thread ID.
|
|
135
|
+
*/
|
|
136
|
+
forwardAttachment(attachmentID, threadID) {
|
|
137
|
+
return this.bot.ctx.api.forwardAttachment(attachmentID, threadID);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Shares a contact card to a thread.
|
|
141
|
+
* @param userID - The user ID of the contact to share.
|
|
142
|
+
* @param threadID - The target thread ID.
|
|
143
|
+
*/
|
|
144
|
+
shareContact(userID, threadID) {
|
|
145
|
+
return this.bot.ctx.api.shareContact(userID, threadID);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Changes the color theme of a thread.
|
|
149
|
+
* @param color - The color hex string.
|
|
150
|
+
* @param threadID - The target thread ID.
|
|
151
|
+
*/
|
|
152
|
+
changeThreadColor(color, threadID) {
|
|
153
|
+
return this.bot.ctx.api.changeThreadColor(color, threadID);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Changes the quick-reaction emoji of a thread.
|
|
157
|
+
* @param emoji - The emoji string.
|
|
158
|
+
* @param threadID - The target thread ID.
|
|
159
|
+
*/
|
|
160
|
+
changeThreadEmoji(emoji, threadID) {
|
|
161
|
+
return this.bot.ctx.api.changeThreadEmoji(emoji, threadID);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Fetches a specific message by ID.
|
|
165
|
+
* @param messageID - The message ID to fetch.
|
|
166
|
+
*/
|
|
167
|
+
getMessage(messageID) {
|
|
168
|
+
return this.bot.ctx.api.getMessage(messageID);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Returns all available thread color themes.
|
|
172
|
+
*/
|
|
173
|
+
getThreadColors() {
|
|
174
|
+
return this.bot.ctx.api.getThreadColors();
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// src/api/ConduitThreadsAPI.ts
|
|
179
|
+
var ConduitThreadsAPI = class {
|
|
180
|
+
constructor(bot) {
|
|
181
|
+
this.bot = bot;
|
|
182
|
+
}
|
|
183
|
+
bot;
|
|
184
|
+
/**
|
|
185
|
+
* Fetches detailed info about a thread.
|
|
186
|
+
* @param threadID - The thread ID to query.
|
|
187
|
+
*/
|
|
188
|
+
getInfo(threadID) {
|
|
189
|
+
return this.bot.ctx.api.getThreadInfo(threadID);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Fetches a paginated list of threads.
|
|
193
|
+
* @param limit - Number of threads to return.
|
|
194
|
+
* @param cursor - Pagination cursor, or `null` for the first page.
|
|
195
|
+
* @param folders - Folder filters e.g. `["INBOX"]`.
|
|
196
|
+
*/
|
|
197
|
+
getList(limit, cursor, folders) {
|
|
198
|
+
return this.bot.ctx.api.getThreadList(limit, cursor, folders);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Fetches message history for a thread.
|
|
202
|
+
* @param threadID - The thread ID to query.
|
|
203
|
+
* @param limit - Number of messages to return.
|
|
204
|
+
*/
|
|
205
|
+
getHistory(threadID, limit) {
|
|
206
|
+
return this.bot.ctx.api.getThreadHistory(threadID, limit);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Searches for threads by name or keyword.
|
|
210
|
+
* @param query - The search query string.
|
|
211
|
+
*/
|
|
212
|
+
search(query) {
|
|
213
|
+
return this.bot.ctx.api.searchForThread(query);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Creates a new group conversation.
|
|
217
|
+
* @param userIDs - Array of user IDs to add to the group.
|
|
218
|
+
* @param name - Optional group name.
|
|
219
|
+
*/
|
|
220
|
+
createGroup(userIDs, name) {
|
|
221
|
+
return this.bot.ctx.api.createNewGroup(userIDs, name);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Adds a user to an existing group thread.
|
|
225
|
+
* @param userID - The user ID to add.
|
|
226
|
+
* @param threadID - The target group thread ID.
|
|
227
|
+
*/
|
|
228
|
+
addUser(userID, threadID) {
|
|
229
|
+
return this.bot.ctx.api.addUserToGroup(userID, threadID);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Removes a user from a group thread.
|
|
233
|
+
* @param userID - The user ID to remove.
|
|
234
|
+
* @param threadID - The target group thread ID.
|
|
235
|
+
*/
|
|
236
|
+
removeUser(userID, threadID) {
|
|
237
|
+
return this.bot.ctx.api.removeUserFromGroup(userID, threadID);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Promotes or demotes a user's admin status in a group.
|
|
241
|
+
* @param userID - The target user ID.
|
|
242
|
+
* @param threadID - The group thread ID.
|
|
243
|
+
* @param admin - `true` to promote, `false` to demote.
|
|
244
|
+
*/
|
|
245
|
+
changeAdminStatus(userID, threadID, admin) {
|
|
246
|
+
return this.bot.ctx.api.changeAdminStatus(userID, threadID, admin);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Updates the group's profile image.
|
|
250
|
+
* @param image - A readable stream or file buffer.
|
|
251
|
+
* @param threadID - The target group thread ID.
|
|
252
|
+
*/
|
|
253
|
+
changeGroupImage(image, threadID) {
|
|
254
|
+
return this.bot.ctx.api.changeGroupImage(image, threadID);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Sets a participant's nickname in a thread.
|
|
258
|
+
* @param nickname - The new nickname. Pass an empty string to clear.
|
|
259
|
+
* @param threadID - The thread ID.
|
|
260
|
+
* @param userID - The target user ID.
|
|
261
|
+
*/
|
|
262
|
+
changeNickname(nickname, threadID, userID) {
|
|
263
|
+
return this.bot.ctx.api.changeNickname(nickname, threadID, userID);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Changes the title of a group thread.
|
|
267
|
+
* @param title - The new group title.
|
|
268
|
+
* @param threadID - The target group thread ID.
|
|
269
|
+
*/
|
|
270
|
+
setTitle(title, threadID) {
|
|
271
|
+
return this.bot.ctx.api.setTitle(title, threadID);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Creates a poll in a thread.
|
|
275
|
+
* @param title - The poll question.
|
|
276
|
+
* @param threadID - The target thread ID.
|
|
277
|
+
* @param options - Array of answer options.
|
|
278
|
+
*/
|
|
279
|
+
createPoll(title, threadID, options) {
|
|
280
|
+
return this.bot.ctx.api.createPoll(title, threadID, options);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Deletes a thread.
|
|
284
|
+
* @param threadID - The thread ID to delete.
|
|
285
|
+
*/
|
|
286
|
+
delete(threadID) {
|
|
287
|
+
return this.bot.ctx.api.deleteThread(threadID);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Mutes or unmutes notifications for a thread.
|
|
291
|
+
* @param threadID - The target thread ID.
|
|
292
|
+
* @param muteUntil - Timestamp (ms) to mute until. Pass `-1` to mute indefinitely, `0` to unmute.
|
|
293
|
+
*/
|
|
294
|
+
mute(threadID, muteUntil) {
|
|
295
|
+
return this.bot.ctx.api.muteThread(threadID, muteUntil);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Accepts or declines a message request.
|
|
299
|
+
* @param threadID - The thread ID of the request.
|
|
300
|
+
* @param accept - `true` to accept, `false` to decline.
|
|
301
|
+
*/
|
|
302
|
+
handleMessageRequest(threadID, accept) {
|
|
303
|
+
return this.bot.ctx.api.handleMessageRequest(threadID, accept);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// src/api/ConduitUsersAPI.ts
|
|
308
|
+
var ConduitUsersAPI = class {
|
|
309
|
+
constructor(bot) {
|
|
310
|
+
this.bot = bot;
|
|
311
|
+
}
|
|
312
|
+
bot;
|
|
313
|
+
/**
|
|
314
|
+
* Fetches info for one or more users by ID.
|
|
315
|
+
* @param userID - A single user ID or an array of user IDs.
|
|
316
|
+
*/
|
|
317
|
+
getInfo(userID) {
|
|
318
|
+
return this.bot.ctx.api.getUserInfo(userID);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Resolves a vanity URL or username to a Facebook user ID.
|
|
322
|
+
* @param vanity - The vanity name or profile URL slug.
|
|
323
|
+
*/
|
|
324
|
+
getID(vanity) {
|
|
325
|
+
return this.bot.ctx.api.getUserID(vanity);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Returns the authenticated user's friends list.
|
|
329
|
+
*/
|
|
330
|
+
getFriendsList() {
|
|
331
|
+
return this.bot.ctx.api.getFriendsList();
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
// src/api/ConduitAccountAPI.ts
|
|
336
|
+
var ConduitAccountAPI = class {
|
|
337
|
+
constructor(bot) {
|
|
338
|
+
this.bot = bot;
|
|
339
|
+
}
|
|
340
|
+
bot;
|
|
341
|
+
/**
|
|
342
|
+
* Returns the logged-in user's Facebook ID.
|
|
343
|
+
*/
|
|
344
|
+
getCurrentUserID() {
|
|
345
|
+
return this.bot.ctx.api.getCurrentUserID();
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Blocks or unblocks a user.
|
|
349
|
+
* @param userID - The target user ID.
|
|
350
|
+
* @param block - `true` to block, `false` to unblock.
|
|
351
|
+
*/
|
|
352
|
+
blockUser(userID, block) {
|
|
353
|
+
return this.bot.ctx.api.changeBlockedStatus(userID, block);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Accepts or declines a friend request.
|
|
357
|
+
* @param userID - The user ID who sent the request.
|
|
358
|
+
* @param accept - `true` to accept, `false` to decline.
|
|
359
|
+
*/
|
|
360
|
+
handleFriendRequest(userID, accept) {
|
|
361
|
+
return this.bot.ctx.api.handleFriendRequest(userID, accept);
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Removes a user from the friends list.
|
|
365
|
+
* @param userID - The target user ID.
|
|
366
|
+
*/
|
|
367
|
+
unfriend(userID) {
|
|
368
|
+
return this.bot.ctx.api.unfriend(userID);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Ends the current session and invalidates cookies.
|
|
372
|
+
*/
|
|
373
|
+
logout() {
|
|
374
|
+
return this.bot.ctx.api.logout();
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// src/client/ConduitClient.ts
|
|
379
|
+
var FANOUT_EVENTS = /* @__PURE__ */ new Set([
|
|
380
|
+
"user:create",
|
|
381
|
+
"user:remove",
|
|
382
|
+
"thread:update",
|
|
383
|
+
"thread:title_change",
|
|
384
|
+
"thread:photo_replaced",
|
|
385
|
+
"thread:theme_changed",
|
|
386
|
+
"thread:nickname_changed",
|
|
387
|
+
"thread:admin_changed"
|
|
388
|
+
]);
|
|
389
|
+
var LOG_MESSAGE_TYPE_MAP = {
|
|
390
|
+
"log:subscribe": "user:create",
|
|
391
|
+
"log:unsubscribe": "user:remove",
|
|
392
|
+
"log:thread-name": "thread:title_change",
|
|
393
|
+
"log:thread-image": "thread:photo_replaced",
|
|
394
|
+
"log:thread-color": "thread:theme_changed",
|
|
395
|
+
"log:user-nickname": "thread:nickname_changed",
|
|
396
|
+
"log:thread-admins": "thread:admin_changed"
|
|
397
|
+
};
|
|
398
|
+
var ConduitClient = class {
|
|
399
|
+
/** Underlying FCA bot instance. `null` until {@link login} resolves. */
|
|
400
|
+
_client;
|
|
401
|
+
/** Lazily-initialized messages API wrapper. */
|
|
402
|
+
_messages = null;
|
|
403
|
+
/** Lazily-initialized threads API wrapper. */
|
|
404
|
+
_threads = null;
|
|
405
|
+
/** Lazily-initialized users API wrapper. */
|
|
406
|
+
_users = null;
|
|
407
|
+
/** Lazily-initialized account API wrapper. */
|
|
408
|
+
_account = null;
|
|
409
|
+
/** Configuration forwarded to the FCA layer on login. */
|
|
410
|
+
config;
|
|
411
|
+
/**
|
|
412
|
+
* Registry of middleware stacks keyed by Conduit event name.
|
|
413
|
+
* Each stack is executed in insertion order via {@link runStack}.
|
|
414
|
+
*/
|
|
415
|
+
middlewares;
|
|
416
|
+
/**
|
|
417
|
+
* Guards against registering the `threadUpdate` fan-out listener more than once,
|
|
418
|
+
* regardless of how many fan-out events are subscribed to.
|
|
419
|
+
*/
|
|
420
|
+
fanOutBound;
|
|
421
|
+
/**
|
|
422
|
+
* @param config - Client configuration passed through to the FCA bot.
|
|
423
|
+
*/
|
|
424
|
+
constructor(config) {
|
|
425
|
+
this._client = null;
|
|
426
|
+
this.config = {
|
|
427
|
+
...config,
|
|
428
|
+
logLevel: config.logLevel ?? "silent"
|
|
429
|
+
};
|
|
430
|
+
this.middlewares = /* @__PURE__ */ new Map();
|
|
431
|
+
this.fanOutBound = false;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* API for message operations.
|
|
435
|
+
*
|
|
436
|
+
* Provides methods for sending messages, replying to threads,
|
|
437
|
+
* reacting to messages, editing content, deleting messages,
|
|
438
|
+
* and other message-level interactions.
|
|
439
|
+
*/
|
|
440
|
+
get messages() {
|
|
441
|
+
return this._messages ??= new ConduitMessagesAPI(this.client);
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* API for thread management operations.
|
|
445
|
+
*
|
|
446
|
+
* Includes functionality for retrieving thread data,
|
|
447
|
+
* managing participants, updating thread metadata (such as title),
|
|
448
|
+
* and handling thread-level features like polls.
|
|
449
|
+
*/
|
|
450
|
+
get threads() {
|
|
451
|
+
return this._threads ??= new ConduitThreadsAPI(this.client);
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* API for user-related operations.
|
|
455
|
+
*
|
|
456
|
+
* Supports fetching user profiles, resolving user IDs,
|
|
457
|
+
* and accessing social graph data such as friends or connections.
|
|
458
|
+
*/
|
|
459
|
+
get users() {
|
|
460
|
+
return this._users ??= new ConduitUsersAPI(this.client);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* API for account-level operations.
|
|
464
|
+
*
|
|
465
|
+
* Exposes actions tied to the authenticated account such as
|
|
466
|
+
* retrieving the current user ID, managing friend requests,
|
|
467
|
+
* blocking users, and logging out.
|
|
468
|
+
*/
|
|
469
|
+
get account() {
|
|
470
|
+
return this._account ??= new ConduitAccountAPI(this.client);
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Authenticates with Messenger and initialises the underlying FCA bot.
|
|
474
|
+
* Must be called before any events can be received.
|
|
475
|
+
*
|
|
476
|
+
* @param credentials - App-state, cookies, or email/password credentials.
|
|
477
|
+
* @returns The current instance for chaining.
|
|
478
|
+
*/
|
|
479
|
+
async login(credentials) {
|
|
480
|
+
this._client = await fcaUnofficial.createMessengerBot(
|
|
481
|
+
{
|
|
482
|
+
appState: credentials.appstate,
|
|
483
|
+
Cookie: credentials.cookies,
|
|
484
|
+
email: credentials.account?.email,
|
|
485
|
+
password: credentials.account?.password
|
|
486
|
+
},
|
|
487
|
+
this.config
|
|
488
|
+
);
|
|
489
|
+
return this;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Registers one or more middleware handlers for a Conduit event.
|
|
493
|
+
*
|
|
494
|
+
* The first call for a given event also binds the corresponding FCA listener.
|
|
495
|
+
* Fan-out events share a single `threadUpdate` FCA binding; all others get
|
|
496
|
+
* their own dedicated listener via {@link bindConduitEvent}.
|
|
497
|
+
*
|
|
498
|
+
* @param event - The Conduit event name to subscribe to.
|
|
499
|
+
* @param middlewares - Ordered middleware functions to push onto the stack.
|
|
500
|
+
* @returns The current instance for chaining.
|
|
501
|
+
*/
|
|
502
|
+
on(event, ...middlewares) {
|
|
503
|
+
if (!this.middlewares.has(event)) {
|
|
504
|
+
this.middlewares.set(event, []);
|
|
505
|
+
if (FANOUT_EVENTS.has(event)) {
|
|
506
|
+
this.bindFanOutEvents();
|
|
507
|
+
} else {
|
|
508
|
+
this.bindConduitEvent(event);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
this.middlewares.get(event).push(...middlewares);
|
|
512
|
+
return this;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Registers middleware directly against a raw FCA event, bypassing the
|
|
516
|
+
* Conduit event abstraction entirely. Useful for events not yet mapped
|
|
517
|
+
* by the Conduit layer.
|
|
518
|
+
*
|
|
519
|
+
* @param event - Raw FCA event name.
|
|
520
|
+
* @param middlewares - Middleware functions receiving the raw FCA payload.
|
|
521
|
+
* @returns The current instance for chaining.
|
|
522
|
+
*/
|
|
523
|
+
onFca(event, ...middlewares) {
|
|
524
|
+
this.client.on(event, async (data) => {
|
|
525
|
+
await this.runStack(
|
|
526
|
+
middlewares,
|
|
527
|
+
data
|
|
528
|
+
);
|
|
529
|
+
});
|
|
530
|
+
return this;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Uses the raw legacy api. Not recommended as this do not
|
|
534
|
+
* have any type safety and auto-completion features. Use
|
|
535
|
+
* at your own risk.
|
|
536
|
+
*
|
|
537
|
+
* @returns The raw api context.
|
|
538
|
+
* */
|
|
539
|
+
get api() {
|
|
540
|
+
return this.client.ctx.api;
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Translates a single Conduit event to its FCA equivalent and attaches a
|
|
544
|
+
* listener that enriches the raw payload before running the middleware stack.
|
|
545
|
+
*
|
|
546
|
+
* @param event - The Conduit event to bind.
|
|
547
|
+
*/
|
|
548
|
+
bindConduitEvent(event) {
|
|
549
|
+
const fcaEvent = toFcaEvent(event);
|
|
550
|
+
this.client.on(fcaEvent, async (raw) => {
|
|
551
|
+
const stack = this.middlewares.get(event) ?? [];
|
|
552
|
+
await this.runStack(stack, this.enrich(event, raw));
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Returns the initialized Messenger client instance.
|
|
557
|
+
*
|
|
558
|
+
* This getter enforces the client lifecycle by ensuring that the underlying
|
|
559
|
+
* FCA bot has been created via {@link login} before any access is allowed.
|
|
560
|
+
*
|
|
561
|
+
* @throws {ConduitError} If the client has not been initialized yet (login not called).
|
|
562
|
+
* @returns {MessengerBot} The active Messenger bot instance.
|
|
563
|
+
*/
|
|
564
|
+
get client() {
|
|
565
|
+
if (this._client) return this._client;
|
|
566
|
+
throw ConduitError.uninitializedClient();
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Attaches a single `threadUpdate` FCA listener that fans out to all
|
|
570
|
+
* relevant Conduit events based on the incoming `logMessageType`.
|
|
571
|
+
*
|
|
572
|
+
* `thread:update` always fires with the raw payload when its stack is
|
|
573
|
+
* non-empty. Specific sub-events (e.g. `thread:title_change`) fire only
|
|
574
|
+
* when their stack is also non-empty and a matching `logMessageType` exists.
|
|
575
|
+
*
|
|
576
|
+
* Calling this method more than once is a no-op.
|
|
577
|
+
*/
|
|
578
|
+
bindFanOutEvents() {
|
|
579
|
+
if (this.fanOutBound) return;
|
|
580
|
+
this.fanOutBound = true;
|
|
581
|
+
this.client.on("threadUpdate", async (raw) => {
|
|
582
|
+
const { logMessageType } = raw;
|
|
583
|
+
const updateStack = this.middlewares.get("thread:update") ?? [];
|
|
584
|
+
if (updateStack.length > 0) {
|
|
585
|
+
await this.runStack(updateStack, this.enrich("thread:update", raw));
|
|
586
|
+
}
|
|
587
|
+
const conduitEvent = LOG_MESSAGE_TYPE_MAP[logMessageType];
|
|
588
|
+
if (!conduitEvent) return;
|
|
589
|
+
const stack = this.middlewares.get(conduitEvent) ?? [];
|
|
590
|
+
if (stack.length === 0) return;
|
|
591
|
+
await this.runStack(stack, this.enrich(conduitEvent, raw));
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Augments a raw FCA payload with convenience helpers scoped to the event.
|
|
596
|
+
*
|
|
597
|
+
* All events receive a `send` helper for replying to the source thread.
|
|
598
|
+
* Events in the `message:` namespace additionally receive:
|
|
599
|
+
* - `reply` — sends a quoted reply to the triggering message.
|
|
600
|
+
* - `react` — sets an emoji reaction on the triggering message.
|
|
601
|
+
*
|
|
602
|
+
* @param event - The Conduit event being enriched.
|
|
603
|
+
* @param raw - The raw FCA payload.
|
|
604
|
+
* @returns The enriched context object passed to middleware.
|
|
605
|
+
*/
|
|
606
|
+
enrich(event, raw) {
|
|
607
|
+
const threadID = raw.threadID;
|
|
608
|
+
const messageID = raw.messageID;
|
|
609
|
+
const sendable = {
|
|
610
|
+
send: (body) => this.messages.send(body, threadID)
|
|
611
|
+
};
|
|
612
|
+
if (event.startsWith("message:")) {
|
|
613
|
+
return {
|
|
614
|
+
...raw,
|
|
615
|
+
...sendable,
|
|
616
|
+
reply: (body) => this.messages.reply(body, threadID, messageID),
|
|
617
|
+
react: (emoji) => this.messages.react(emoji, messageID, threadID)
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
return { ...raw, ...sendable };
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Executes a middleware stack sequentially, where each handler must call
|
|
624
|
+
* `next()` to advance to the following handler.
|
|
625
|
+
*
|
|
626
|
+
* @param stack - Ordered array of middleware functions to execute.
|
|
627
|
+
* @param data - The enriched event payload threaded through the stack.
|
|
628
|
+
*/
|
|
629
|
+
async runStack(stack, data) {
|
|
630
|
+
let i = 0;
|
|
631
|
+
const next = async () => {
|
|
632
|
+
if (i < stack.length) {
|
|
633
|
+
await stack[i++](data, next);
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
await next();
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
exports.ConduitAccountAPI = ConduitAccountAPI;
|
|
641
|
+
exports.ConduitClient = ConduitClient;
|
|
642
|
+
exports.ConduitError = ConduitError;
|
|
643
|
+
exports.ConduitMessagesAPI = ConduitMessagesAPI;
|
|
644
|
+
exports.ConduitThreadsAPI = ConduitThreadsAPI;
|
|
645
|
+
exports.ConduitUsersAPI = ConduitUsersAPI;
|
|
646
|
+
exports.toFcaEvent = toFcaEvent;
|
|
647
|
+
//# sourceMappingURL=index.cjs.map
|
|
648
|
+
//# sourceMappingURL=index.cjs.map
|