|
|
|
@ -11,6 +11,7 @@ const {
|
|
|
|
|
} = require('@whiskeysockets/baileys');
|
|
|
|
|
const { writeFile } = require('fs/promises');
|
|
|
|
|
const waEmitter = require('../emitter');
|
|
|
|
|
const serverConfig = require('../../config').server;
|
|
|
|
|
|
|
|
|
|
const { formatPhoneNumber, parsePhoneNumber, formatStatus, formatTimestamp } = require('./helper');
|
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
@ -31,7 +32,7 @@ waEmitter.on('connection:close', event => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const createWhatsApp = async phone => {
|
|
|
|
|
let socketRef = null;
|
|
|
|
|
// let waSocket = null;
|
|
|
|
|
let qrCode = null;
|
|
|
|
|
let connectionStatus = 'offline'
|
|
|
|
|
const channelId = generateId();
|
|
|
|
@ -39,60 +40,20 @@ const createWhatsApp = async phone => {
|
|
|
|
|
// 储存键值对 msgId-externalId
|
|
|
|
|
// TODO 什么时候清理旧的?
|
|
|
|
|
const msgIdMap = new Map();
|
|
|
|
|
|
|
|
|
|
const sendTextMessage = async (number, content, externalId) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
|
|
|
|
|
return new Promise(() => {
|
|
|
|
|
socketRef.sendMessage(jid, { text: content })
|
|
|
|
|
.then(msg => {
|
|
|
|
|
msgIdMap.set(msg.key.id, externalId);
|
|
|
|
|
})
|
|
|
|
|
.catch(ex => {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
externalId,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
errro: `发送文本消息出错 ` + ex,
|
|
|
|
|
eventSource: 'WA-01-HK.sendMessage.promise.catch',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sendImageMessage = async (number, imageUrl) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
try {
|
|
|
|
|
const msgInfo = await socketRef.sendMessage(jid, {
|
|
|
|
|
image: { url: imageUrl },
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
messageId: msgInfo?.key?.id ?? generateId()
|
|
|
|
|
};
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
|
messge: `[${whatsAppNo}->${number}]发送图片消息出错`,
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: ex
|
|
|
|
|
})
|
|
|
|
|
console.error(`[${whatsAppNo}->${number}]发送图片消息出错: `, ex);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getProfilePicture = async (whatsAppNo) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await socketRef.profilePictureUrl(number);
|
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs-' + phone + '_' + channelId + '.txt'));
|
|
|
|
|
logger.level = 'trace';
|
|
|
|
|
const msgRetryCounterCache = new NodeCache();
|
|
|
|
|
const storeFilename = './baileys_auth_info/baileys_store_' + phone + '_' + channelId + '.json'
|
|
|
|
|
const store = makeInMemoryStore({ logger });
|
|
|
|
|
store?.readFromFile(storeFilename);
|
|
|
|
|
// save every 10s
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
store?.writeToFile(storeFilename);
|
|
|
|
|
}, 10_000);
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info/' + phone + '_' + channelId);
|
|
|
|
|
// fetch latest version of WA Web
|
|
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion();
|
|
|
|
|
const waVersion = version.join('.') + ', ' + (isLatest ? 'latest' : 'out');
|
|
|
|
|
|
|
|
|
|
const handleMessagesUpsert = async upsert => {
|
|
|
|
|
console.info('messages.upsert: ', JSON.stringify(upsert, undefined, 2));
|
|
|
|
@ -139,7 +100,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
},
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
fromMe: msg.key.fromMe,
|
|
|
|
|
eventSource: 'WA-01-HK.messages.upsert.notify',
|
|
|
|
|
eventSource: serverConfig.name + '.messages.upsert.notify',
|
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
@ -163,7 +124,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
},
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
fromMe: msg.key.fromMe,
|
|
|
|
|
eventSource: 'WA-01-HK.messages.upsert.notify',
|
|
|
|
|
eventSource: serverConfig.name + '.messages.upsert.notify',
|
|
|
|
|
createTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -197,7 +158,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
},
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
fromMe: msg.key.fromMe,
|
|
|
|
|
eventSource: 'WA-01-HK.messages.upsert.append',
|
|
|
|
|
eventSource: serverConfig.name + '.messages.upsert.append',
|
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -228,32 +189,16 @@ const createWhatsApp = async phone => {
|
|
|
|
|
},
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
fromMe: msg.key.fromMe,
|
|
|
|
|
eventSource: 'WA-01-HK.messages.updated',
|
|
|
|
|
eventSource: serverConfig.name + '.messages.updated',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleConnection = async (socket) => {
|
|
|
|
|
socketRef = socket;
|
|
|
|
|
socketRef.sendMessage('8613317835586@s.whatsapp.net', { text: '原始连接上了:' + new Date().toString()})
|
|
|
|
|
const handleCredsUpdate = async () => {
|
|
|
|
|
await saveCreds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const start = async () => {
|
|
|
|
|
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs-' + phone + '_' + channelId + '.txt'));
|
|
|
|
|
logger.level = 'trace';
|
|
|
|
|
const msgRetryCounterCache = new NodeCache();
|
|
|
|
|
const storeFilename = './baileys_auth_info/baileys_store_' + phone + '_' + channelId + '.json'
|
|
|
|
|
const store = makeInMemoryStore({ logger });
|
|
|
|
|
store?.readFromFile(storeFilename);
|
|
|
|
|
// save every 10s
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
store?.writeToFile(storeFilename);
|
|
|
|
|
}, 10_000);
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info/' + phone + '_' + channelId);
|
|
|
|
|
// fetch latest version of WA Web
|
|
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion();
|
|
|
|
|
const start = () => {
|
|
|
|
|
|
|
|
|
|
const waSocket = makeWASocket({
|
|
|
|
|
version,
|
|
|
|
@ -268,7 +213,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
//retryRequestDelayMs: 1000*25,
|
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21
|
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab4e2c825c0d774875701ed07e20d05bdb6/WAProto/WAProto.proto
|
|
|
|
|
browser: Browsers.ubuntu('IOS_PHONE'),//Browsers.macOS('SAFARI'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.baileys('WEAR_OS'),//
|
|
|
|
|
browser: Browsers.macOS('SAFARI'),//Browsers.macOS('SAFARI'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.baileys('WEAR_OS'),//
|
|
|
|
|
msgRetryCounterCache,
|
|
|
|
|
generateHighQualityLinkPreview: false,
|
|
|
|
|
syncFullHistory: false,
|
|
|
|
@ -276,79 +221,136 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
store?.bind(waSocket.ev);
|
|
|
|
|
|
|
|
|
|
waSocket.ev.process(
|
|
|
|
|
// events is a map for event name => event data
|
|
|
|
|
async(events) => {
|
|
|
|
|
// something about the connection changed
|
|
|
|
|
// maybe it closed, or we received all offline message or connection opened
|
|
|
|
|
if(events['connection.update']) {
|
|
|
|
|
const update = events['connection.update']
|
|
|
|
|
const { connection, qr, lastDisconnect } = update
|
|
|
|
|
const sendTextMessage = async (number, content, externalId) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
waSocket.sendMessage(jid, { text: content })
|
|
|
|
|
.then(msg => {
|
|
|
|
|
msgIdMap.set(msg.key.id, externalId);
|
|
|
|
|
})
|
|
|
|
|
.catch(ex => {
|
|
|
|
|
console.error('sendTextMessage.error: ', ex)
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
externalId,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: `发送文本消息出错 ` + ex,
|
|
|
|
|
eventSource: serverConfig.name + '.sendMessage.promise.catch',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sendImageMessage = async (number, imageUrl) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
try {
|
|
|
|
|
const msgInfo = await waSocket.sendMessage(jid, {
|
|
|
|
|
image: { url: imageUrl },
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
messageId: msgInfo?.key?.id ?? generateId()
|
|
|
|
|
};
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
|
messge: `[${whatsAppNo}->${number}]发送图片消息出错`,
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: ex
|
|
|
|
|
})
|
|
|
|
|
console.error(`[${whatsAppNo}->${number}]发送图片消息出错: `, ex);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (connection === 'close') {
|
|
|
|
|
if((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
|
|
start();
|
|
|
|
|
} else {
|
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
|
whatsAppNo, channelId,
|
|
|
|
|
eventSource: 'WA-01-HK.connection.update.close',
|
|
|
|
|
status: 'offline',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
|
handleConnection(waSocket)
|
|
|
|
|
connectionStatus = 'open';
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
status: 'open', whatsAppNo, channelId,
|
|
|
|
|
eventSource: 'WA-01-HK.connection.update.open',
|
|
|
|
|
socket: waSocket,
|
|
|
|
|
sendTextMessage,
|
|
|
|
|
sendImageMessage,
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
// 第一次一分钟,后面是 20 秒更新一次
|
|
|
|
|
if (qrCode === null) {
|
|
|
|
|
qrCode = qr;
|
|
|
|
|
console.info('qr: ', qr)
|
|
|
|
|
const getProfilePicture = async (whatsAppNo) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(number);
|
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
waSocket.ev.on('connection.update', async update => {
|
|
|
|
|
console.log('connection update: ', update);
|
|
|
|
|
const { connection, lastDisconnect, qr } = update;
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:qr', {
|
|
|
|
|
createTimestamp: Date.now(),
|
|
|
|
|
status: 'offline',
|
|
|
|
|
version: '0.111111',
|
|
|
|
|
channelId: channelId,
|
|
|
|
|
phone: phone,
|
|
|
|
|
qrCode: qr,
|
|
|
|
|
socket: waSocket
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
// 第一次二维码时效后退出,不需要等待更新二维码
|
|
|
|
|
waSocket.logout(() => '二维码已过期');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (connection === 'close') {
|
|
|
|
|
if((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
|
|
start();
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
|
whatsAppNo, channelId,
|
|
|
|
|
eventSource: serverConfig.name + '.connection.update.close',
|
|
|
|
|
status: 'offline',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
|
connectionStatus = 'open';
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
status: 'open', whatsAppNo, channelId,
|
|
|
|
|
eventSource: serverConfig.name + '.connection.update.open',
|
|
|
|
|
});
|
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.message', event => {
|
|
|
|
|
// const jid = formatPhoneNumber(event.to);
|
|
|
|
|
const {to: number, externalId, content} = event;
|
|
|
|
|
console.info('request.' + whatsAppNo + '.send.message:', event)
|
|
|
|
|
waSocket.sendMessage(
|
|
|
|
|
number + '@s.whatsapp.net',
|
|
|
|
|
{ text: content}
|
|
|
|
|
).then(msg => {
|
|
|
|
|
msgIdMap.set(msg.key.id, externalId);
|
|
|
|
|
}).catch(ex => {
|
|
|
|
|
console.error('sendMessage.error: ', ex)
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
externalId,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: `发送文本消息出错 ` + ex,
|
|
|
|
|
eventSource: serverConfig.name + '.sendMessage.catch',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
//sendTextMessage(number, content, externalId);
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
// 第一次一分钟,后面是 20 秒更新一次
|
|
|
|
|
if (qrCode === null) {
|
|
|
|
|
qrCode = qr;
|
|
|
|
|
console.info('qr: ', qr);
|
|
|
|
|
waEmitter.emit('creds:update', {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
qr, whatsAppNo,
|
|
|
|
|
server:serverConfig.name,
|
|
|
|
|
eventSource: 'creds.update',
|
|
|
|
|
createTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// 第一次二维码时效后退出,不需要等待更新二维码
|
|
|
|
|
waSocket.logout(() => '二维码已过期');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events['creds.update']) {
|
|
|
|
|
await saveCreds()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('creds.update', async () => await saveCreds());
|
|
|
|
|
waSocket.ev.on('messages.upsert', handleMessagesUpsert);
|
|
|
|
|
waSocket.ev.on('messages.update', handleMessagesUpdate);
|
|
|
|
|
waSocket.ev.on('creds.update', handleCredsUpdate);
|
|
|
|
|
waSocket.ev.on('messages.upsert', handleMessagesUpsert);
|
|
|
|
|
waSocket.ev.on('messages.update', handleMessagesUpdate);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
createTimestamp: Date.now(),
|
|
|
|
|
status: 'offline',
|
|
|
|
|
version: 'no-promise-version',
|
|
|
|
|
version: waVersion,
|
|
|
|
|
channelId: channelId,
|
|
|
|
|
phone: phone,
|
|
|
|
|
start,
|
|
|
|
|
sendTextMessage,
|
|
|
|
|
sendImageMessage,
|
|
|
|
|
getProfilePicture,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|