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.
126 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MEDIA_CONTENT_TYPES = exports.MEDIA_URL_FIELDS = void 0;
exports.detectContentType = detectContentType;
exports.extractMediaUrls = extractMediaUrls;
exports.messageSortKey = messageSortKey;
exports.compareBySortKey = compareBySortKey;
const MSG_TYPE_MAP = {
webchat: 'text',
'chat.text': 'text',
'chat.photo': 'image',
'chat.video.msg': 'video',
'chat.voice': 'voice',
'share.file': 'file',
'chat.sticker': 'sticker',
'chat.gif': 'gif',
'chat.doodle': 'sticker',
'chat.link': 'link',
'chat.recommended': 'link',
'chat.location.new': 'location',
};
exports.MEDIA_URL_FIELDS = [
'hdUrl',
'href',
'normalUrl',
'fileUrl',
'url',
'thumbUrl',
'thumb',
'thumbnail',
];
exports.MEDIA_CONTENT_TYPES = new Set([
'image',
'video',
'voice',
'file',
'gif',
]);
function detectContentType(msgType, content) {
var _a;
const mapped = MSG_TYPE_MAP[String(msgType !== null && msgType !== void 0 ? msgType : '')];
if (mapped) {
return mapped;
}
if (typeof content === 'string') {
return content.length > 0 ? 'text' : 'unknown';
}
if (content && typeof content === 'object') {
const c = content;
const type = String((_a = c.type) !== null && _a !== void 0 ? _a : '').toLowerCase();
if (type === 'photo' || type === 'image')
return 'image';
if (type === 'video')
return 'video';
if (type === 'audio' || type === 'voice')
return 'voice';
if (type === 'file')
return 'file';
if (type === 'sticker')
return 'sticker';
if (type === 'gif')
return 'gif';
if (typeof c.href === 'string' && typeof c.title === 'string' && c.title.length > 0) {
return 'link';
}
}
return 'unknown';
}
function extractMediaUrls(content) {
const parsed = coerceContentObject(content);
if (!parsed) {
return typeof content === 'string' && isHttpUrl(content) ? [content] : [];
}
const urls = [];
const seen = new Set();
for (const field of exports.MEDIA_URL_FIELDS) {
const value = parsed[field];
if (!isHttpUrl(value) || seen.has(value))
continue;
seen.add(value);
urls.push(value);
}
const params = coerceContentObject(parsed.params);
if (params) {
for (const field of exports.MEDIA_URL_FIELDS) {
const value = params[field];
if (!isHttpUrl(value) || seen.has(value))
continue;
seen.add(value);
urls.push(value);
}
}
return urls;
}
function messageSortKey(msgId, timestamp) {
const raw = String(msgId !== null && msgId !== void 0 ? msgId : '').trim();
if (/^\d+$/.test(raw)) {
return BigInt(raw);
}
const ts = Number(timestamp);
return Number.isFinite(ts) ? BigInt(Math.trunc(ts)) : BigInt(0);
}
function compareBySortKey(a, b, direction) {
if (a === b)
return 0;
return (a < b ? -1 : 1) * direction;
}
function coerceContentObject(value) {
if (value && typeof value === 'object' && !Array.isArray(value)) {
return value;
}
if (typeof value === 'string' && value.trim().startsWith('{')) {
try {
const parsed = JSON.parse(value);
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
}
catch {
return null;
}
}
return null;
}
function isHttpUrl(value) {
return typeof value === 'string' && /^https?:\/\//i.test(value.trim());
}
//# sourceMappingURL=zaloMessage.js.map