|
|
@ -12,7 +12,7 @@ const {
|
|
|
|
const { writeFile } = require('fs/promises');
|
|
|
|
const { writeFile } = require('fs/promises');
|
|
|
|
const waEmitter = require('../emitter');
|
|
|
|
const waEmitter = require('../emitter');
|
|
|
|
|
|
|
|
|
|
|
|
const { formatPhoneNumber, parsePhoneNumber, formatStatus, formatTimestamp } = require('./common');
|
|
|
|
const { formatPhoneNumber, parsePhoneNumber, formatStatus, formatTimestamp } = require('./helper');
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
const NodeCache = require('node-cache');
|
|
|
|
const NodeCache = require('node-cache');
|
|
|
|
const P = require('pino');
|
|
|
|
const P = require('pino');
|
|
|
@ -51,31 +51,35 @@ const createWhatsApp = async phone => {
|
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion();
|
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion();
|
|
|
|
const waVersion = version.join('.') + ', ' + (isLatest ? 'latest' : 'out');
|
|
|
|
const waVersion = version.join('.') + ', ' + (isLatest ? 'latest' : 'out');
|
|
|
|
|
|
|
|
|
|
|
|
const sendTextMessage = async (whatsAppNo, content) => {
|
|
|
|
const sendTextMessage = async (number, content) => {
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await waSocket.sendMessage(number, { text: content });
|
|
|
|
return await waSocket.sendMessage(jid, { text: content });
|
|
|
|
} catch (ex) {
|
|
|
|
} catch (ex) {
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
messge: '发送文本消息出错',
|
|
|
|
messge: '发送文本消息出错',
|
|
|
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
|
|
|
to: number,
|
|
|
|
error: ex
|
|
|
|
error: ex
|
|
|
|
})
|
|
|
|
})
|
|
|
|
console.error('发送文本消息出错: ', ex);
|
|
|
|
console.error(`${whatsAppNo}-${number}发送文本消息出错: `, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const sendImageMessage = async (whatsAppNo, imageUrl) => {
|
|
|
|
const sendImageMessage = async (number, imageUrl) => {
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await waSocket.sendMessage(number, {
|
|
|
|
return await waSocket.sendMessage(jid, {
|
|
|
|
image: { url: imageUrl },
|
|
|
|
image: { url: imageUrl },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (ex) {
|
|
|
|
} catch (ex) {
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
messge: '发送图片消息出错',
|
|
|
|
messge: '发送图片消息出错',
|
|
|
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
|
|
|
to: number,
|
|
|
|
error: ex
|
|
|
|
error: ex
|
|
|
|
})
|
|
|
|
})
|
|
|
|
console.error('发送图片消息出错: ', ex);
|
|
|
|
console.error(`${whatsAppNo}-${number}发送图片消息出错: `, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -94,8 +98,13 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
|
|
if (upsert.type === 'notify') {
|
|
|
|
if (upsert.type === 'notify') {
|
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
const messageType = Object.keys(msg.message)[0];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 没有类型的消息,先忽略
|
|
|
|
|
|
|
|
if (!msg.message) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const messageType = Object.keys(msg.message)[0];
|
|
|
|
console.log('messageType', messageType);
|
|
|
|
console.log('messageType', messageType);
|
|
|
|
if (messageType === 'imageMessage') {
|
|
|
|
if (messageType === 'imageMessage') {
|
|
|
|
// download the message
|
|
|
|
// download the message
|
|
|
@ -130,7 +139,6 @@ const createWhatsApp = async phone => {
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
id: msg.key.id,
|
|
|
|
id: msg.key.id,
|
|
|
|
//status: formatStatus(msg.update.status),
|
|
|
|
|
|
|
|
status: formatStatus(msg.status),
|
|
|
|
status: formatStatus(msg.status),
|
|
|
|
from: whatsAppNo,
|
|
|
|
from: whatsAppNo,
|
|
|
|
to: fromWhatsAppNo,
|
|
|
|
to: fromWhatsAppNo,
|
|
|
@ -145,6 +153,7 @@ const createWhatsApp = async phone => {
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
name: msg.pushName,
|
|
|
|
name: msg.pushName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
eventSource: 'messages.upsert.notify',
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -165,6 +174,7 @@ const createWhatsApp = async phone => {
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
name: msg.pushName,
|
|
|
|
name: msg.pushName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
eventSource: 'messages.upsert.notify',
|
|
|
|
createTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
createTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -192,6 +202,7 @@ const createWhatsApp = async phone => {
|
|
|
|
id: parsePhoneNumber(msg.participant),
|
|
|
|
id: parsePhoneNumber(msg.participant),
|
|
|
|
name: msg.pushName,
|
|
|
|
name: msg.pushName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
eventSource: 'messages.upsert.append',
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -216,6 +227,7 @@ const createWhatsApp = async phone => {
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
name: msg.pushName,
|
|
|
|
name: msg.pushName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
eventSource: 'messages.updated',
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -228,7 +240,6 @@ const createWhatsApp = async phone => {
|
|
|
|
logger,
|
|
|
|
logger,
|
|
|
|
auth: {
|
|
|
|
auth: {
|
|
|
|
creds: state.creds,
|
|
|
|
creds: state.creds,
|
|
|
|
/** caching makes the store faster to send/recv messages */
|
|
|
|
|
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21
|
|
|
@ -238,36 +249,26 @@ const createWhatsApp = async phone => {
|
|
|
|
generateHighQualityLinkPreview: false,
|
|
|
|
generateHighQualityLinkPreview: false,
|
|
|
|
syncFullHistory: false,
|
|
|
|
syncFullHistory: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 不绑定不会影响扫码登录
|
|
|
|
|
|
|
|
store?.bind(waSocket.ev);
|
|
|
|
|
|
|
|
// something about the connection changed
|
|
|
|
|
|
|
|
// maybe it closed, or we received all offline message or connection opened
|
|
|
|
|
|
|
|
waSocket.ev.on('connection.update', async update => {
|
|
|
|
waSocket.ev.on('connection.update', async update => {
|
|
|
|
console.log('connection update: ', update);
|
|
|
|
console.log('connection update: ', update);
|
|
|
|
const { connection, lastDisconnect, qr, isOnline } = update;
|
|
|
|
const { connection, lastDisconnect, qr } = update;
|
|
|
|
|
|
|
|
|
|
|
|
if (connection === 'close') {
|
|
|
|
if (connection === 'close') {
|
|
|
|
console.log('链接断开:', lastDisconnect);
|
|
|
|
|
|
|
|
if (lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
|
|
|
|
|
console.log('正在重连:');
|
|
|
|
|
|
|
|
start();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// waSocket.end(error => console.error('end.error: ', error));
|
|
|
|
// waSocket.end(error => console.error('end.error: ', error));
|
|
|
|
// waSocket.logout(msg => console.error('logout.msg: ', msg));
|
|
|
|
// waSocket.logout(msg => console.error('logout.msg: ', msg));
|
|
|
|
console.log('Connection closed. You are logged out.');
|
|
|
|
console.log('Connection closed: ', lastDisconnect);
|
|
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
whatsAppNo,
|
|
|
|
whatsAppNo,
|
|
|
|
status: 'offline',
|
|
|
|
status: 'offline',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
status: 'open', whatsAppNo
|
|
|
|
status: 'open', whatsAppNo
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
|
|
|
// 第一次一分钟,后面是 20 秒更新一次
|
|
|
|
resolve(qr);
|
|
|
|
resolve(qr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -283,6 +284,9 @@ const createWhatsApp = async phone => {
|
|
|
|
console.info('creds.update');
|
|
|
|
console.info('creds.update');
|
|
|
|
await saveCreds();
|
|
|
|
await saveCreds();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 不绑定不会影响扫码登录
|
|
|
|
|
|
|
|
store?.bind(waSocket.ev);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|