|
|
|
@ -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,7 +79,147 @@ const createWhatsApp = async phone => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let waSocket = null;
|
|
|
|
|
const getProfilePicture = async (whatsAppNo) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(number);
|
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleMessagesUpsert = async upsert => {
|
|
|
|
|
console.info('messages.upsert: ', JSON.stringify(upsert, undefined, 2));
|
|
|
|
|
|
|
|
|
|
if (upsert.type === 'notify') {
|
|
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
|
const messageType = Object.keys(msg.message)[0];
|
|
|
|
|
|
|
|
|
|
console.log('messageType', messageType);
|
|
|
|
|
if (messageType === 'imageMessage') {
|
|
|
|
|
// download the message
|
|
|
|
|
const buffer = await downloadMediaMessage(
|
|
|
|
|
msg,
|
|
|
|
|
'buffer',
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
logger,
|
|
|
|
|
// pass this so that baileys can request a reupload of media
|
|
|
|
|
// that has been deleted
|
|
|
|
|
reuploadRequest: waSocket.updateMediaMessage,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
// save to file
|
|
|
|
|
await writeFile('d:/my-download.jpeg', buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
|
|
|
|
|
|
|
|
|
|
if (text.indexOf('图片') > -1) {
|
|
|
|
|
sendImageMessage(fromWhatsAppNo, 'https://images.asiahighlights.com/allpicture/2022/07/8a7d9ced5936463bb904c82a_cut_750x850_349.webp');
|
|
|
|
|
} else if (text.indexOf('文本') > -1) {
|
|
|
|
|
sendTextMessage(fromWhatsAppNo, '这是文本消息:' + new Date().toString());
|
|
|
|
|
} else if (text.indexOf('发群') > -1) {
|
|
|
|
|
sendTextMessage('120363363417115199@g.us', '这是群消息:' + new Date().toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
type: 'text',
|
|
|
|
|
text: {
|
|
|
|
|
body: text,
|
|
|
|
|
},
|
|
|
|
|
conversation: {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
waEmitter.emit('message:received', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
status: '',
|
|
|
|
|
direction: 'inbound',
|
|
|
|
|
from: fromWhatsAppNo,
|
|
|
|
|
to: whatsAppNo,
|
|
|
|
|
type: 'text',
|
|
|
|
|
text: {
|
|
|
|
|
body: text,
|
|
|
|
|
},
|
|
|
|
|
conversation: {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
createTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} 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),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
from: msg.key.fromMe ? whatsAppNo : parsePhoneNumber(msg.key.remoteJid),
|
|
|
|
|
to: msg.key.fromMe ? parsePhoneNumber(msg.key.remoteJid) : whatsAppNo,
|
|
|
|
|
conversation: {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const start = () => {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
@ -88,12 +232,15 @@ const createWhatsApp = async phone => {
|
|
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
|
|
},
|
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21
|
|
|
|
|
browser: Browsers.macOS('Desktop'),
|
|
|
|
|
// 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 => {
|
|
|
|
@ -129,135 +276,17 @@ const createWhatsApp = async phone => {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('messages.upsert', async upsert => {
|
|
|
|
|
console.log('收到消息:', JSON.stringify(upsert, undefined, 2));
|
|
|
|
|
|
|
|
|
|
if (upsert.type === 'notify') {
|
|
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
|
const messageType = Object.keys(msg.message)[0];
|
|
|
|
|
|
|
|
|
|
console.log('messageType', messageType);
|
|
|
|
|
if (messageType === 'imageMessage') {
|
|
|
|
|
// download the message
|
|
|
|
|
const buffer = await downloadMediaMessage(
|
|
|
|
|
msg,
|
|
|
|
|
'buffer',
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
logger,
|
|
|
|
|
// pass this so that baileys can request a reupload of media
|
|
|
|
|
// that has been deleted
|
|
|
|
|
reuploadRequest: waSocket.updateMediaMessage,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
// save to file
|
|
|
|
|
await writeFile('d:/my-download.jpeg', buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (text.indexOf('图片') > -1) {
|
|
|
|
|
sendImageMessage(fromWhatsAppNo, 'https://images.asiahighlights.com/allpicture/2022/07/8a7d9ced5936463bb904c82a_cut_750x850_349.webp');
|
|
|
|
|
} else if (text.indexOf('文本') > -1) {
|
|
|
|
|
sendTextMessage(fromWhatsAppNo, '这是文本消息:' + new Date().toString());
|
|
|
|
|
} else if (text.indexOf('发群') > -1) {
|
|
|
|
|
sendTextMessage('120363363417115199@g.us', '这是群消息:' + new Date().toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
updateTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
waEmitter.emit('message:received', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
status: '',
|
|
|
|
|
direction: 'inbound',
|
|
|
|
|
from: fromWhatsAppNo,
|
|
|
|
|
to: whatsAppNo,
|
|
|
|
|
type: 'text',
|
|
|
|
|
text: {
|
|
|
|
|
body: text,
|
|
|
|
|
},
|
|
|
|
|
conversation: {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
createTime: formatTimestamp(msg.messageTimestamp),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('messages.update', async messageUpdate => {
|
|
|
|
|
console.info('messages.update: ', messageUpdate);
|
|
|
|
|
|
|
|
|
|
for (const msg of messageUpdate) {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
status: formatStatus(msg.update.status),
|
|
|
|
|
from: msg.key.fromMe ? whatsAppNo : parsePhoneNumber(msg.key.remoteJid),
|
|
|
|
|
to: msg.key.fromMe ? parsePhoneNumber(msg.key.remoteJid) : whatsAppNo,
|
|
|
|
|
conversation: {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 不绑定不会影响扫码登录
|
|
|
|
|
store?.bind(waSocket.ev);
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
waSocket.ev.on('creds.update', async () => {
|
|
|
|
|
console.info('creds.update');
|
|
|
|
|
await saveCreds();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -270,6 +299,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
start,
|
|
|
|
|
sendTextMessage,
|
|
|
|
|
sendImageMessage,
|
|
|
|
|
getProfilePicture,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|