n8n-nodes-zalo-nnt
Version:
Unofficial Zalo integration for n8n - Send messages, manage groups, user operations with QR login. No API key required, works via browser simulation.
107 lines • 4.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveUserThreadId = resolveUserThreadId;
exports.resolveGroupThreadId = resolveGroupThreadId;
exports.parseUserMessage = parseUserMessage;
exports.parseGroupMessage = parseGroupMessage;
exports.parseMessageContent = parseMessageContent;
exports.parseReaction = parseReaction;
exports.parseMessages = parseMessages;
const zaloMessage_1 = require("../../utils/zaloMessage");
function resolveUserThreadId(msg, data) {
var _a;
if (msg === null || msg === void 0 ? void 0 : msg.threadId) {
return String(msg.threadId);
}
const uidFrom = data === null || data === void 0 ? void 0 : data.uidFrom;
if (uidFrom !== undefined && uidFrom !== null && String(uidFrom) !== '0') {
return String(uidFrom);
}
return String((_a = data === null || data === void 0 ? void 0 : data.idTo) !== null && _a !== void 0 ? _a : '');
}
function resolveGroupThreadId(msg, data) {
var _a, _b, _c, _d;
return String((_d = (_c = (_b = (_a = data === null || data === void 0 ? void 0 : data.grid) !== null && _a !== void 0 ? _a : data === null || data === void 0 ? void 0 : data.groupId) !== null && _b !== void 0 ? _b : msg === null || msg === void 0 ? void 0 : msg.threadId) !== null && _c !== void 0 ? _c : data === null || data === void 0 ? void 0 : data.idTo) !== null && _d !== void 0 ? _d : '');
}
function parseUserMessage(msg) {
const data = msg.data || msg;
return {
msgId: data.msgId || '',
threadId: resolveUserThreadId(msg, data),
content: data.content || '',
msgType: data.msgType || 'unknown',
contentType: (0, zaloMessage_1.detectContentType)(data.msgType, data.content),
mediaUrls: (0, zaloMessage_1.extractMediaUrls)(data.content),
senderId: data.uidFrom || data.userId || '',
timestamp: data.ts || data.timestamp || Date.now(),
isGroup: false,
ttl: data.ttl,
cliMsgId: data.cliMsgId,
actionId: data.actionId,
propertyExt: data.propertyExt,
};
}
function parseGroupMessage(msg) {
const data = msg.data || msg;
const groupId = resolveGroupThreadId(msg, data);
return {
msgId: data.msgId || '',
threadId: groupId,
content: data.content || '',
msgType: data.msgType || 'unknown',
contentType: (0, zaloMessage_1.detectContentType)(data.msgType, data.content),
mediaUrls: (0, zaloMessage_1.extractMediaUrls)(data.content),
senderId: data.uidFrom || data.userId || data.uin || '',
timestamp: data.ts || data.timestamp || Date.now(),
isGroup: true,
groupId,
senderName: data.dName || data.displayName || '',
ttl: data.ttl,
cliMsgId: data.cliMsgId,
actionId: data.actionId,
propertyExt: data.propertyExt,
};
}
function parseMessageContent(content, msgType) {
if (typeof content === 'string') {
return content;
}
if (typeof content === 'object' && content !== null) {
switch (msgType) {
case 'chat.text':
case 'webchat':
return content;
case 'chat.photo':
case 'chat.video':
case 'chat.file':
return content;
case 'chat.sticker':
return content;
case 'chat.link':
return content;
default:
return content;
}
}
return content;
}
function parseReaction(reaction) {
var _a;
return {
msgId: reaction.msgId || '',
senderId: reaction.userId || reaction.uidFrom || '',
icon: reaction.icon || ((_a = reaction.content) === null || _a === void 0 ? void 0 : _a.icon) || '',
timestamp: reaction.ts || reaction.timestamp || Date.now(),
};
}
function parseMessages(messages, isGroup) {
if (!Array.isArray(messages)) {
return [];
}
return messages.map((msg) => {
const parsed = isGroup ? parseGroupMessage(msg) : parseUserMessage(msg);
parsed.content = parseMessageContent(parsed.content, parsed.msgType);
return parsed;
});
}
//# sourceMappingURL=messageParser.js.map