|
|
|
@ -30,16 +30,20 @@ setInterval(() => {
|
|
|
|
|
}, 10_000);
|
|
|
|
|
|
|
|
|
|
waEmitter.on('message:updated', event => {
|
|
|
|
|
console.info('msg:event', event);
|
|
|
|
|
console.info('msg:evt:updated', event);
|
|
|
|
|
});
|
|
|
|
|
waEmitter.on('message:received', event => {
|
|
|
|
|
console.info('msg:event', event);
|
|
|
|
|
console.info('msg:evt:received', event);
|
|
|
|
|
});
|
|
|
|
|
waEmitter.on('connection.open', event => {
|
|
|
|
|
console.info('msg:event', event);
|
|
|
|
|
waEmitter.on('connection:open', event => {
|
|
|
|
|
console.info('con:evt.open', event);
|
|
|
|
|
});
|
|
|
|
|
waEmitter.on('connection:close', event => {
|
|
|
|
|
console.info('con:evt.close', event);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const createWhatsApp = async phone => {
|
|
|
|
|
let waSocket = null;
|
|
|
|
|
const channelId = generateId();
|
|
|
|
|
const whatsAppNo = phone;
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info/' + phone + '_' + channelId);
|
|
|
|
@ -75,62 +79,18 @@ const createWhatsApp = async phone => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let waSocket = null;
|
|
|
|
|
|
|
|
|
|
const start = () => {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
waSocket = makeWASocket({
|
|
|
|
|
version,
|
|
|
|
|
logger,
|
|
|
|
|
auth: {
|
|
|
|
|
creds: state.creds,
|
|
|
|
|
/** caching makes the store faster to send/recv messages */
|
|
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
|
|
},
|
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21
|
|
|
|
|
browser: Browsers.macOS('Desktop'),
|
|
|
|
|
msgRetryCounterCache,
|
|
|
|
|
generateHighQualityLinkPreview: false,
|
|
|
|
|
syncFullHistory: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// something about the connection changed
|
|
|
|
|
// maybe it closed, or we received all offline message or connection opened
|
|
|
|
|
waSocket.ev.on('connection.update', async update => {
|
|
|
|
|
console.log('connection update: ', update);
|
|
|
|
|
const { connection, lastDisconnect, qr, isOnline } = update;
|
|
|
|
|
|
|
|
|
|
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.logout(msg => console.error('logout.msg: ', msg));
|
|
|
|
|
console.log('Connection closed. You are logged out.');
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
status: 'offline',
|
|
|
|
|
});
|
|
|
|
|
const getProfilePicture = async (whatsAppNo) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(number);
|
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
status: 'online',
|
|
|
|
|
});
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
status: 'open', whatsAppNo
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
resolve(qr);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('messages.upsert', async upsert => {
|
|
|
|
|
console.log('收到消息:', JSON.stringify(upsert, undefined, 2));
|
|
|
|
|
const handleMessagesUpsert = async upsert => {
|
|
|
|
|
console.info('messages.upsert: ', JSON.stringify(upsert, undefined, 2));
|
|
|
|
|
|
|
|
|
|
if (upsert.type === 'notify') {
|
|
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
@ -156,13 +116,6 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(msg.key.remoteJid);
|
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
|
|
|
|
|
|
|
|
|
@ -177,6 +130,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
//status: formatStatus(msg.update.status),
|
|
|
|
|
status: formatStatus(msg.status),
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: fromWhatsAppNo,
|
|
|
|
@ -216,16 +170,43 @@ const createWhatsApp = async phone => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (upsert.type === 'append') {
|
|
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
|
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
status: formatStatus(msg.status),
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: fromWhatsAppNo,
|
|
|
|
|
type: 'text',
|
|
|
|
|
text: {
|
|
|
|
|
body: text,
|
|
|
|
|
},
|
|
|
|
|
conversation: {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('messages.update', async messageUpdate => {
|
|
|
|
|
console.info('messages.update: ', messageUpdate);
|
|
|
|
|
const handleMessagesUpdate = async messageUpdate => {
|
|
|
|
|
console.info('messages.update: ', JSON.stringify(messageUpdate, undefined, 2));
|
|
|
|
|
|
|
|
|
|
for (const msg of messageUpdate) {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
status: formatStatus(msg.update.status),
|
|
|
|
|
status: formatStatus(msg.update?.status),
|
|
|
|
|
from: msg.key.fromMe ? whatsAppNo : parsePhoneNumber(msg.key.remoteJid),
|
|
|
|
|
to: msg.key.fromMe ? parsePhoneNumber(msg.key.remoteJid) : whatsAppNo,
|
|
|
|
|
conversation: {
|
|
|
|
@ -238,26 +219,74 @@ const createWhatsApp = async phone => {
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('group-participants.update', async GroupMetadata => {
|
|
|
|
|
console.info('group-participants.update: ', GroupMetadata);
|
|
|
|
|
const start = () => {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
waSocket = makeWASocket({
|
|
|
|
|
version,
|
|
|
|
|
logger,
|
|
|
|
|
auth: {
|
|
|
|
|
creds: state.creds,
|
|
|
|
|
/** caching makes the store faster to send/recv messages */
|
|
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
|
|
},
|
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21
|
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab4e2c825c0d774875701ed07e20d05bdb6/WAProto/WAProto.proto
|
|
|
|
|
browser: Browsers.baileys('WEAR_OS'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.macOS('Desktop'),
|
|
|
|
|
msgRetryCounterCache,
|
|
|
|
|
generateHighQualityLinkPreview: 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 => {
|
|
|
|
|
console.log('connection update: ', update);
|
|
|
|
|
const { connection, lastDisconnect, qr, isOnline } = update;
|
|
|
|
|
|
|
|
|
|
// the process function lets you process all events that just occurred
|
|
|
|
|
// efficiently in a batch
|
|
|
|
|
waSocket.ev.process(
|
|
|
|
|
// events is a map for event name => event data
|
|
|
|
|
async events => {
|
|
|
|
|
// credentials updated -- save them
|
|
|
|
|
if (events['creds.update']) {
|
|
|
|
|
await saveCreds();
|
|
|
|
|
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.logout(msg => console.error('logout.msg: ', msg));
|
|
|
|
|
console.log('Connection closed. You are logged out.');
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
status: 'offline',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
status: 'online',
|
|
|
|
|
});
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
status: 'open', whatsAppNo
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
resolve(qr);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('messages.upsert', handleMessagesUpsert);
|
|
|
|
|
waSocket.ev.on('messages.update', handleMessagesUpdate);
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('group-participants.update', async GroupMetadata => {
|
|
|
|
|
console.info('group-participants.update: ', GroupMetadata);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('creds.update', async () => {
|
|
|
|
|
console.info('creds.update');
|
|
|
|
|
await saveCreds();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -270,6 +299,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
start,
|
|
|
|
|
sendTextMessage,
|
|
|
|
|
sendImageMessage,
|
|
|
|
|
getProfilePicture,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|