|
|
|
@ -7,7 +7,7 @@ const {
|
|
|
|
|
makeInMemoryStore,
|
|
|
|
|
useMultiFileAuthState,
|
|
|
|
|
downloadMediaMessage,
|
|
|
|
|
isJidUser
|
|
|
|
|
isJidUser, isJidGroup
|
|
|
|
|
} = require('@whiskeysockets/baileys');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const path = require('path');
|
|
|
|
@ -20,12 +20,16 @@ const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const NodeCache = require('node-cache');
|
|
|
|
|
const P = require('pino');
|
|
|
|
|
|
|
|
|
|
// https://baileys.whiskeysockets.io/
|
|
|
|
|
|
|
|
|
|
const createWhatsApp = async phone => {
|
|
|
|
|
let qrCode = null;
|
|
|
|
|
const channelId = generateId();
|
|
|
|
|
const whatsAppNo = phone;
|
|
|
|
|
// 缓存 msgId-externalId,过期时间为 5 分钟
|
|
|
|
|
const externalIdCache = new NodeCache({ stdTTL: 60*5 });
|
|
|
|
|
// 缓存群信息,过期时间为 5 分钟
|
|
|
|
|
const groupSubjectCache = new NodeCache({ stdTTL: 60*60*24 });
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./logs/wa-logs-' + phone + '_' + channelId + '.txt'));
|
|
|
|
|
logger.level = 'trace';
|
|
|
|
|
const msgRetryCounterCache = new NodeCache();
|
|
|
|
@ -81,6 +85,13 @@ const createWhatsApp = async phone => {
|
|
|
|
|
const messageType = Object.keys(msg.message)[0];
|
|
|
|
|
const remoteNo = decodeJid(msg.key.remoteJid);
|
|
|
|
|
const externalId = externalIdCache.get(msg.key.id);
|
|
|
|
|
const isGroup = isJidGroup(msg.key.remoteJid);
|
|
|
|
|
let groupSubject = groupSubjectCache.get(msg.key.remoteJid)
|
|
|
|
|
if (isGroup && groupSubject === undefined) {
|
|
|
|
|
const groupMetadata = await waSocket.groupMetadata(msg.key.remoteJid);
|
|
|
|
|
groupSubject = groupMetadata.subject;
|
|
|
|
|
groupSubjectCache.set(msg.key.remoteJid, groupMetadata.subject)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
|
|
|
|
|
@ -100,6 +111,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
},
|
|
|
|
|
conversation: {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
name: groupSubject,
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: decodeJid(msg.key.participant),
|
|
|
|
@ -124,6 +136,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
},
|
|
|
|
|
conversation: {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
name: groupSubject,
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: decodeJid(msg.key.participant),
|
|
|
|
@ -156,8 +169,8 @@ const createWhatsApp = async phone => {
|
|
|
|
|
image: {
|
|
|
|
|
mimetype: imageMessage.mimetype,
|
|
|
|
|
sha256: uint8ArrayToBase64(imageMessage.fileSha256),
|
|
|
|
|
caption: '', // 暂时为空
|
|
|
|
|
filename: imageFilename,
|
|
|
|
|
caption: imageMessage.caption,
|
|
|
|
|
filePath: imageFilename,
|
|
|
|
|
link_original: imageMessage.url,
|
|
|
|
|
},
|
|
|
|
|
conversation: {
|
|
|
|
|