@jackle.dev/zalox-plugin 1.0.25 → 1.0.27
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/package.json +1 -1
- package/src/listener.ts +250 -3
package/package.json
CHANGED
package/src/listener.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* ZaloX Plugin
|
|
2
|
+
* ZaloX Plugin - In-process message listener
|
|
3
3
|
*
|
|
4
4
|
* Uses zca-js API directly. Handles:
|
|
5
5
|
* - Message events (text, image, sticker)
|
|
@@ -148,7 +148,8 @@ async function processMessage(
|
|
|
148
148
|
? core.channel.commands.resolveCommandAuthorizedFromAuthorizers({
|
|
149
149
|
useAccessGroups,
|
|
150
150
|
authorizers: [
|
|
151
|
-
{ configured: effectiveAllowFrom.length > 0, allowed: senderAllowedForCommands }
|
|
151
|
+
{ configured: effectiveAllowFrom.length > 0, allowed: senderAllowedForCommands },
|
|
152
|
+
],
|
|
152
153
|
})
|
|
153
154
|
: undefined;
|
|
154
155
|
|
|
@@ -170,4 +171,250 @@ async function processMessage(
|
|
|
170
171
|
try {
|
|
171
172
|
await sendText(
|
|
172
173
|
chatId,
|
|
173
|
-
core.channel.pairing.buildPairingReply({
|
|
174
|
+
core.channel.pairing.buildPairingReply({
|
|
175
|
+
channel: 'zalox',
|
|
176
|
+
idLine: `Your Zalo user id: ${senderId}`,
|
|
177
|
+
code,
|
|
178
|
+
}),
|
|
179
|
+
{ profile },
|
|
180
|
+
);
|
|
181
|
+
statusSink?.({ lastOutboundAt: Date.now() });
|
|
182
|
+
} catch (err) {
|
|
183
|
+
runtime.error?.(`[zalox] pairing reply failed: ${String(err)}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (
|
|
193
|
+
isGroup &&
|
|
194
|
+
core.channel.commands.isControlCommandMessage(rawBody, config) &&
|
|
195
|
+
commandAuthorized !== true
|
|
196
|
+
) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const peer = isGroup
|
|
201
|
+
? { kind: 'group' as const, id: chatId }
|
|
202
|
+
: { kind: 'dm' as const, id: senderId };
|
|
203
|
+
|
|
204
|
+
const route = core.channel.routing.resolveAgentRoute({
|
|
205
|
+
cfg: config,
|
|
206
|
+
channel: 'zalox',
|
|
207
|
+
accountId: account.accountId,
|
|
208
|
+
peer,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const fromLabel = isGroup ? `group:${chatId}` : senderName || `user:${senderId}`;
|
|
212
|
+
const storePath = core.channel.session.resolveStorePath(config.session?.store, {
|
|
213
|
+
agentId: route.agentId,
|
|
214
|
+
});
|
|
215
|
+
const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(config);
|
|
216
|
+
const previousTimestamp = core.channel.session.readSessionUpdatedAt({
|
|
217
|
+
storePath,
|
|
218
|
+
sessionKey: route.sessionKey,
|
|
219
|
+
});
|
|
220
|
+
const body = core.channel.reply.formatAgentEnvelope({
|
|
221
|
+
channel: 'Zalo (ZaloX)',
|
|
222
|
+
from: fromLabel,
|
|
223
|
+
timestamp: timestamp ? timestamp * 1000 : undefined,
|
|
224
|
+
previousTimestamp,
|
|
225
|
+
envelope: envelopeOptions,
|
|
226
|
+
body: rawBody || (mediaPath ? '[Image]' : ''),
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const ctxPayload = core.channel.reply.finalizeInboundContext({
|
|
230
|
+
Body: body,
|
|
231
|
+
RawBody: rawBody,
|
|
232
|
+
CommandBody: rawBody,
|
|
233
|
+
From: isGroup ? `zalox:group:${chatId}` : `zalox:${senderId}`,
|
|
234
|
+
To: `zalox:${chatId}`,
|
|
235
|
+
SessionKey: route.sessionKey,
|
|
236
|
+
AccountId: route.accountId,
|
|
237
|
+
ChatType: isGroup ? 'group' : 'direct',
|
|
238
|
+
ConversationLabel: fromLabel,
|
|
239
|
+
SenderName: senderName || undefined,
|
|
240
|
+
SenderId: senderId,
|
|
241
|
+
CommandAuthorized: commandAuthorized,
|
|
242
|
+
Provider: 'zalox',
|
|
243
|
+
Surface: 'zalox',
|
|
244
|
+
MessageSid: message.msgId ?? `${timestamp}`,
|
|
245
|
+
OriginatingChannel: 'zalox',
|
|
246
|
+
OriginatingTo: `zalox:${chatId}`,
|
|
247
|
+
MediaPath: mediaPath,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
await core.channel.session.recordInboundSession({
|
|
251
|
+
storePath,
|
|
252
|
+
sessionKey: ctxPayload.SessionKey ?? route.sessionKey,
|
|
253
|
+
ctx: ctxPayload,
|
|
254
|
+
onRecordError: (err: unknown) => {
|
|
255
|
+
runtime.error?.(`[zalox] session meta error: ${String(err)}`);
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
await core.channel.reply.dispatchReplyWithBufferedBlockDispatcher({
|
|
260
|
+
ctx: ctxPayload,
|
|
261
|
+
cfg: config,
|
|
262
|
+
dispatcherOptions: {
|
|
263
|
+
deliver: async (payload: any) => {
|
|
264
|
+
const text = payload.text ?? '';
|
|
265
|
+
if (!text) return;
|
|
266
|
+
|
|
267
|
+
const tableMode = core.channel.text.resolveMarkdownTableMode({
|
|
268
|
+
cfg: config,
|
|
269
|
+
channel: 'zalox',
|
|
270
|
+
accountId: account.accountId,
|
|
271
|
+
});
|
|
272
|
+
const converted = core.channel.text.convertMarkdownTables(text, tableMode ?? 'code');
|
|
273
|
+
const chunkMode = core.channel.text.resolveChunkMode(config, 'zalox', account.accountId);
|
|
274
|
+
const chunks = core.channel.text.chunkMarkdownTextWithMode(converted, 2000, chunkMode);
|
|
275
|
+
|
|
276
|
+
for (const chunk of chunks) {
|
|
277
|
+
try {
|
|
278
|
+
await sendText(chatId, chunk, { profile, isGroup });
|
|
279
|
+
statusSink?.({ lastOutboundAt: Date.now() });
|
|
280
|
+
} catch (err) {
|
|
281
|
+
runtime.error?.(`[zalox] send failed: ${String(err)}`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
onError: (err: unknown, info: any) => {
|
|
286
|
+
runtime.error?.(`[${account.accountId}] ZaloX ${info.kind} reply failed: ${String(err)}`);
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export async function startInProcessListener(
|
|
293
|
+
options: ListenerOptions,
|
|
294
|
+
): Promise<{ stop: () => void }> {
|
|
295
|
+
const { account, config, runtime, api, ownId, profile, abortSignal, statusSink } = options;
|
|
296
|
+
let stopped = false;
|
|
297
|
+
|
|
298
|
+
const listener = api.listener;
|
|
299
|
+
|
|
300
|
+
listener.on('message', (msg: any) => {
|
|
301
|
+
if (stopped || abortSignal.aborted) return;
|
|
302
|
+
|
|
303
|
+
const data = msg.data || msg;
|
|
304
|
+
const isGroup = msg.type === 1;
|
|
305
|
+
const senderId = data.uidFrom ? String(data.uidFrom) : '';
|
|
306
|
+
|
|
307
|
+
if (senderId === ownId) return;
|
|
308
|
+
|
|
309
|
+
const threadId = isGroup
|
|
310
|
+
? String(data.idTo || data.threadId || '')
|
|
311
|
+
: senderId;
|
|
312
|
+
|
|
313
|
+
const msgType = data.msgType || 0;
|
|
314
|
+
|
|
315
|
+
let mediaUrl = undefined;
|
|
316
|
+
mediaUrl = data.url || data.href;
|
|
317
|
+
if (!mediaUrl && (msgType === 2 || String(msgType) === 'chat.photo')) {
|
|
318
|
+
if (data.params?.url) mediaUrl = data.params.url;
|
|
319
|
+
else if (typeof data.content === 'object' && data.content !== null) {
|
|
320
|
+
mediaUrl = (data.content as any).href || (data.content as any).url || (data.content as any).thumb || (data.content as any).normalUrl;
|
|
321
|
+
}
|
|
322
|
+
else if (typeof data.content === 'string' && (data.content.includes('http') || data.content.startsWith('{'))) {
|
|
323
|
+
try {
|
|
324
|
+
const parsed = JSON.parse(data.content);
|
|
325
|
+
mediaUrl = parsed.href || parsed.url || parsed.thumb || parsed.normalUrl;
|
|
326
|
+
} catch {
|
|
327
|
+
if (data.content.startsWith('http')) mediaUrl = data.content;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (!mediaUrl && data.url && (data.url.includes('.jpg') || data.url.includes('.png'))) {
|
|
332
|
+
mediaUrl = data.url;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (!mediaUrl && data.quote) {
|
|
336
|
+
try {
|
|
337
|
+
const q = data.quote;
|
|
338
|
+
if (q.attach) {
|
|
339
|
+
const attach = typeof q.attach === 'string' ? JSON.parse(q.attach) : q.attach;
|
|
340
|
+
mediaUrl = attach.href || attach.url || attach.thumb || attach.normalUrl;
|
|
341
|
+
}
|
|
342
|
+
if (!mediaUrl && (q.href || q.url)) {
|
|
343
|
+
mediaUrl = q.href || q.url;
|
|
344
|
+
}
|
|
345
|
+
} catch {}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (isGroup) {
|
|
349
|
+
const mentions = data.mentions || data.mentionIds || [];
|
|
350
|
+
const content = typeof data.content === 'string' ? data.content : (data.msg || '');
|
|
351
|
+
|
|
352
|
+
const isMentioned = Array.isArray(mentions)
|
|
353
|
+
? mentions.some((m: any) => String(m.uid || m.id || m) === ownId)
|
|
354
|
+
: false;
|
|
355
|
+
|
|
356
|
+
console.log(`[ZaloX] Group Check Strict: ownId=${ownId} mentions=${JSON.stringify(mentions)} isMentioned=${isMentioned}`);
|
|
357
|
+
|
|
358
|
+
if (content.trim() === '/whoami') {
|
|
359
|
+
// Pass
|
|
360
|
+
} else if (!isMentioned && !content.startsWith('/')) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
statusSink?.({ lastInboundAt: Date.now() });
|
|
366
|
+
|
|
367
|
+
const normalized = {
|
|
368
|
+
threadId,
|
|
369
|
+
msgId: String(data.msgId || data.cliMsgId || ''),
|
|
370
|
+
content: (() => {
|
|
371
|
+
let c = data.content || data.msg || '';
|
|
372
|
+
if (typeof c !== 'string') return '';
|
|
373
|
+
return c;
|
|
374
|
+
})(),
|
|
375
|
+
msgType,
|
|
376
|
+
mediaUrl,
|
|
377
|
+
timestamp: data.ts ? Math.floor(data.ts / 1000) : Math.floor(Date.now() / 1000),
|
|
378
|
+
isGroup,
|
|
379
|
+
senderId,
|
|
380
|
+
senderName: data.dName || data.senderName || undefined,
|
|
381
|
+
groupName: data.threadName || data.groupName || undefined,
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
if (!normalized.content?.trim() && !normalized.mediaUrl) return;
|
|
385
|
+
|
|
386
|
+
try {
|
|
387
|
+
const reactThreadType = isGroup ? ThreadType.Group : ThreadType.User;
|
|
388
|
+
api.addReaction(Reactions.HEART, {
|
|
389
|
+
threadId: normalized.threadId,
|
|
390
|
+
type: reactThreadType,
|
|
391
|
+
data: {
|
|
392
|
+
msgId: String(data.msgId || ''),
|
|
393
|
+
cliMsgId: String(data.cliMsgId || ''),
|
|
394
|
+
},
|
|
395
|
+
}).catch(() => {});
|
|
396
|
+
} catch {}
|
|
397
|
+
|
|
398
|
+
processMessage(normalized, account, config, runtime, profile, statusSink).catch((err) => {
|
|
399
|
+
runtime.error?.(`[${account.accountId}] ZaloX process error: ${String(err)}`);
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
listener.on('error', (err: any) => {
|
|
404
|
+
runtime.error?.(`[${account.accountId}] ZaloX listener error: ${err.message || err}`);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
listener.start({ retryOnClose: true });
|
|
408
|
+
runtime.log?.(`[${account.accountId}] ZaloX: WebSocket listener started`);
|
|
409
|
+
|
|
410
|
+
const stop = () => {
|
|
411
|
+
stopped = true;
|
|
412
|
+
try {
|
|
413
|
+
listener.stop();
|
|
414
|
+
} catch {}
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
abortSignal.addEventListener('abort', stop, { once: true });
|
|
418
|
+
|
|
419
|
+
return { stop };
|
|
420
|
+
}
|