|
|
|
@ -22,9 +22,8 @@ const createWhatsApp = async phone => {
|
|
|
|
|
let qrCode = null;
|
|
|
|
|
const channelId = generateId();
|
|
|
|
|
const whatsAppNo = phone;
|
|
|
|
|
// 储存键值对 msgId-externalId
|
|
|
|
|
// TODO 使用 NodeCache 储存,设置有效期。⬇️
|
|
|
|
|
const msgIdMap = new Map();
|
|
|
|
|
// 缓存 msgId-externalId,过期时间为 5 分钟
|
|
|
|
|
const externalIdCache = new NodeCache({ stdTTL: 60*5 });
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs-' + phone + '_' + channelId + '.txt'));
|
|
|
|
|
logger.level = 'trace';
|
|
|
|
|
const msgRetryCounterCache = new NodeCache();
|
|
|
|
@ -59,7 +58,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
|
|
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
|
|
|
|
|
const externalId = msgIdMap.get(msg.key.id);
|
|
|
|
|
const externalId = externalIdCache.get(msg.key.id);
|
|
|
|
|
|
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
@ -118,7 +117,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
|
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
|
|
|
|
|
const externalId = msgIdMap.get(msg.key.id);
|
|
|
|
|
const externalId = externalIdCache.get(msg.key.id);
|
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
@ -159,7 +158,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
if (ignore) continue;
|
|
|
|
|
|
|
|
|
|
const externalId = msgIdMap.get(msg.key.id);
|
|
|
|
|
const externalId = externalIdCache.get(msg.key.id);
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
externalId,
|
|
|
|
@ -212,7 +211,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
waSocket.sendMessage(jid, { text: content })
|
|
|
|
|
.then(msg => {
|
|
|
|
|
msgIdMap.set(msg.key.id, externalId);
|
|
|
|
|
externalIdCache.set(msg.key.id, externalId);
|
|
|
|
|
})
|
|
|
|
|
.catch(ex => {
|
|
|
|
|
console.error('sendTextMessage.error: ', ex)
|
|
|
|
@ -229,7 +228,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sendImageMessage = async (number, imageUrl) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
try {
|
|
|
|
@ -249,7 +248,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
console.error(`[${whatsAppNo}->${number}]发送图片消息出错: `, ex);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getProfilePicture = async (whatsAppNo) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
|
try {
|
|
|
|
@ -286,7 +285,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
waSocket.sendMessage(
|
|
|
|
|
number + '@s.whatsapp.net', { text: content }
|
|
|
|
|
).then(msg => {
|
|
|
|
|
msgIdMap.set(msg.key.id, externalId);
|
|
|
|
|
externalIdCache.set(msg.key.id, externalId)
|
|
|
|
|
}).catch(ex => {
|
|
|
|
|
console.error('sendMessage.error: ', ex)
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
@ -302,14 +301,14 @@ const createWhatsApp = async phone => {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.image', event => {
|
|
|
|
|
const {to: number, externalId, imageUrl} = event;
|
|
|
|
|
const jid = formatPhoneNumber(event.to);
|
|
|
|
|
waSocket.sendMessage(
|
|
|
|
|
jid, {image: { url: imageUrl }}
|
|
|
|
|
).then(msg => {
|
|
|
|
|
msgIdMap.set(msg.key.id, externalId);
|
|
|
|
|
externalIdCache.set(msg.key.id, externalId);
|
|
|
|
|
}).catch(ex => {
|
|
|
|
|
console.error('sendMessage.error: ', ex)
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|