|
|
@ -31,7 +31,7 @@ waEmitter.on('connection:close', event => {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const createWhatsApp = async phone => {
|
|
|
|
const createWhatsApp = async phone => {
|
|
|
|
let waSocket = null;
|
|
|
|
let socketRef = null;
|
|
|
|
let qrCode = null;
|
|
|
|
let qrCode = null;
|
|
|
|
let connectionStatus = 'offline'
|
|
|
|
let connectionStatus = 'offline'
|
|
|
|
const channelId = generateId();
|
|
|
|
const channelId = generateId();
|
|
|
@ -39,38 +39,14 @@ const createWhatsApp = async phone => {
|
|
|
|
// 储存键值对 msgId-externalId
|
|
|
|
// 储存键值对 msgId-externalId
|
|
|
|
// TODO 什么时候清理旧的?
|
|
|
|
// TODO 什么时候清理旧的?
|
|
|
|
const msgIdMap = new Map();
|
|
|
|
const msgIdMap = new Map();
|
|
|
|
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 sendTextMessage = async (number, content, externalId) => {
|
|
|
|
const sendTextMessage = async (number, content, externalId) => {
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
|
|
|
|
|
|
|
return new Promise(() => {
|
|
|
|
return new Promise(() => {
|
|
|
|
waSocket.sendMessage(jid, { text: content })
|
|
|
|
socketRef.sendMessage(jid, { text: content })
|
|
|
|
.then(msg => {
|
|
|
|
.then(msg => {
|
|
|
|
msgIdMap.set(msg.key.id, externalId);
|
|
|
|
msgIdMap.set(msg.key.id, externalId);
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
|
|
|
externalId,
|
|
|
|
|
|
|
|
status: 'saved',
|
|
|
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
|
|
|
to: number,
|
|
|
|
|
|
|
|
eventSource: 'sendMessage.promise.then',
|
|
|
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(ex => {
|
|
|
|
.catch(ex => {
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
@ -81,7 +57,7 @@ const createWhatsApp = async phone => {
|
|
|
|
from: whatsAppNo,
|
|
|
|
from: whatsAppNo,
|
|
|
|
to: number,
|
|
|
|
to: number,
|
|
|
|
errro: `发送文本消息出错 ` + ex,
|
|
|
|
errro: `发送文本消息出错 ` + ex,
|
|
|
|
eventSource: 'sendMessage.promise.catch',
|
|
|
|
eventSource: 'WA-01-HK.sendMessage.promise.catch',
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -91,7 +67,7 @@ const createWhatsApp = async phone => {
|
|
|
|
const sendImageMessage = async (number, imageUrl) => {
|
|
|
|
const sendImageMessage = async (number, imageUrl) => {
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const msgInfo = await waSocket.sendMessage(jid, {
|
|
|
|
const msgInfo = await socketRef.sendMessage(jid, {
|
|
|
|
image: { url: imageUrl },
|
|
|
|
image: { url: imageUrl },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
return {
|
|
|
@ -111,7 +87,7 @@ const createWhatsApp = async phone => {
|
|
|
|
const getProfilePicture = async (whatsAppNo) => {
|
|
|
|
const getProfilePicture = async (whatsAppNo) => {
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(number);
|
|
|
|
const ppUrl = await socketRef.profilePictureUrl(number);
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
} catch (ex) {
|
|
|
|
} catch (ex) {
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
@ -161,7 +137,9 @@ const createWhatsApp = async phone => {
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
name: msg.pushName,
|
|
|
|
name: msg.pushName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
eventSource: 'messages.upsert.notify',
|
|
|
|
whatsAppNo,
|
|
|
|
|
|
|
|
fromMe: msg.key.fromMe,
|
|
|
|
|
|
|
|
eventSource: 'WA-01-HK.messages.upsert.notify',
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -183,7 +161,9 @@ const createWhatsApp = async phone => {
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
name: msg.pushName,
|
|
|
|
name: msg.pushName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
eventSource: 'messages.upsert.notify',
|
|
|
|
whatsAppNo,
|
|
|
|
|
|
|
|
fromMe: msg.key.fromMe,
|
|
|
|
|
|
|
|
eventSource: 'WA-01-HK.messages.upsert.notify',
|
|
|
|
createTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
createTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -215,7 +195,9 @@ const createWhatsApp = async phone => {
|
|
|
|
id: parsePhoneNumber(msg.participant),
|
|
|
|
id: parsePhoneNumber(msg.participant),
|
|
|
|
name: msg.pushName,
|
|
|
|
name: msg.pushName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
eventSource: 'messages.upsert.append',
|
|
|
|
whatsAppNo,
|
|
|
|
|
|
|
|
fromMe: msg.key.fromMe,
|
|
|
|
|
|
|
|
eventSource: 'WA-01-HK.messages.upsert.append',
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -244,32 +226,49 @@ const createWhatsApp = async phone => {
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
name: msg.pushName,
|
|
|
|
name: msg.pushName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
eventSource: 'messages.updated',
|
|
|
|
whatsAppNo,
|
|
|
|
|
|
|
|
fromMe: msg.key.fromMe,
|
|
|
|
|
|
|
|
eventSource: 'WA-01-HK.messages.updated',
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleCredsUpdate = async () => {
|
|
|
|
const handleConnection = async (socket) => {
|
|
|
|
await saveCreds();
|
|
|
|
socketRef = socket;
|
|
|
|
|
|
|
|
socketRef.sendMessage('8613317835586@s.whatsapp.net', { text: '原始连接上了:' + new Date().toString()})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const start = () => {
|
|
|
|
const start = async () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
|
|
|
waSocket = makeWASocket({
|
|
|
|
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 waSocket = makeWASocket({
|
|
|
|
version,
|
|
|
|
version,
|
|
|
|
logger,
|
|
|
|
logger,
|
|
|
|
auth: {
|
|
|
|
auth: {
|
|
|
|
creds: state.creds,
|
|
|
|
creds: state.creds,
|
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
connectTimeoutMs: 1000*60*10,
|
|
|
|
// connectTimeoutMs: 1000*60*10,
|
|
|
|
defaultQueryTimeoutMs: 1000*60*1,
|
|
|
|
// defaultQueryTimeoutMs: 1000*60*1,
|
|
|
|
keepAliveIntervalMs: 1000*60*60,
|
|
|
|
// keepAliveIntervalMs: 1000*60*60,
|
|
|
|
//retryRequestDelayMs: 1000*25,
|
|
|
|
//retryRequestDelayMs: 1000*25,
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab4e2c825c0d774875701ed07e20d05bdb6/WAProto/WAProto.proto
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab4e2c825c0d774875701ed07e20d05bdb6/WAProto/WAProto.proto
|
|
|
|
browser: Browsers.macOS('SAFARI'),//Browsers.macOS('SAFARI'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.baileys('WEAR_OS'),//
|
|
|
|
browser: Browsers.ubuntu('IOS_PHONE'),//Browsers.macOS('SAFARI'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.baileys('WEAR_OS'),//
|
|
|
|
msgRetryCounterCache,
|
|
|
|
msgRetryCounterCache,
|
|
|
|
generateHighQualityLinkPreview: false,
|
|
|
|
generateHighQualityLinkPreview: false,
|
|
|
|
syncFullHistory: false,
|
|
|
|
syncFullHistory: false,
|
|
|
@ -277,57 +276,73 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
|
|
store?.bind(waSocket.ev);
|
|
|
|
store?.bind(waSocket.ev);
|
|
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('connection.update', async update => {
|
|
|
|
waSocket.ev.process(
|
|
|
|
console.log('connection update: ', update);
|
|
|
|
// events is a map for event name => event data
|
|
|
|
const { connection, lastDisconnect, qr } = update;
|
|
|
|
async(events) => {
|
|
|
|
|
|
|
|
// something about the connection changed
|
|
|
|
if (connection === 'close') {
|
|
|
|
// maybe it closed, or we received all offline message or connection opened
|
|
|
|
if((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
|
if(events['connection.update']) {
|
|
|
|
start();
|
|
|
|
const update = events['connection.update']
|
|
|
|
} else {
|
|
|
|
const { connection, qr, lastDisconnect } = update
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:qr', {
|
|
|
|
|
|
|
|
createTimestamp: Date.now(),
|
|
|
|
|
|
|
|
status: 'offline',
|
|
|
|
|
|
|
|
version: '0.111111',
|
|
|
|
|
|
|
|
channelId: channelId,
|
|
|
|
|
|
|
|
phone: phone,
|
|
|
|
|
|
|
|
qrCode: qr,
|
|
|
|
|
|
|
|
socket: waSocket
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 第一次二维码时效后退出,不需要等待更新二维码
|
|
|
|
|
|
|
|
waSocket.logout(() => '二维码已过期');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (events['creds.update']) {
|
|
|
|
|
|
|
|
await saveCreds()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
waSocket.ev.on('creds.update', async () => await saveCreds());
|
|
|
|
whatsAppNo, channelId,
|
|
|
|
|
|
|
|
eventSource: 'connection.update.close',
|
|
|
|
|
|
|
|
status: 'offline',
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
|
|
|
|
if (connectionStatus === 'open') {
|
|
|
|
|
|
|
|
sendTextMessage('120363363417115199@g.us', whatsAppNo + ' 在线:' + new Date().toString());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 扫码成功后向这个群('120363363417115199@g.us')发消息,后续就能使用 API 发送了。
|
|
|
|
|
|
|
|
sendTextMessage('120363363417115199@g.us', whatsAppNo + ' 登录成功:' + new Date().toString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
connectionStatus = 'open';
|
|
|
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
|
|
|
status: 'open', whatsAppNo, channelId,
|
|
|
|
|
|
|
|
eventSource: 'connection.update.open',
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
|
|
|
// 第一次一分钟,后面是 20 秒更新一次
|
|
|
|
|
|
|
|
if (qrCode === null) {
|
|
|
|
|
|
|
|
qrCode = qr;
|
|
|
|
|
|
|
|
resolve(qr);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 第一次二维码时效后退出,不需要等待更新二维码
|
|
|
|
|
|
|
|
waSocket.logout(() => '二维码已过期');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('creds.update', handleCredsUpdate);
|
|
|
|
|
|
|
|
waSocket.ev.on('messages.upsert', handleMessagesUpsert);
|
|
|
|
waSocket.ev.on('messages.upsert', handleMessagesUpsert);
|
|
|
|
waSocket.ev.on('messages.update', handleMessagesUpdate);
|
|
|
|
waSocket.ev.on('messages.update', handleMessagesUpdate);
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
createTimestamp: Date.now(),
|
|
|
|
createTimestamp: Date.now(),
|
|
|
|
status: 'offline',
|
|
|
|
status: 'offline',
|
|
|
|
version: waVersion,
|
|
|
|
version: 'no-promise-version',
|
|
|
|
channelId: channelId,
|
|
|
|
channelId: channelId,
|
|
|
|
phone: phone,
|
|
|
|
phone: phone,
|
|
|
|
start,
|
|
|
|
start,
|
|
|
|