|
|
|
@ -17,8 +17,6 @@ const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const NodeCache = require('node-cache');
|
|
|
|
|
const P = require('pino');
|
|
|
|
|
|
|
|
|
|
const msgRetryCounterCache = new NodeCache();
|
|
|
|
|
|
|
|
|
|
waEmitter.on('message:updated', event => {
|
|
|
|
|
console.info('msg:evt:updated', event);
|
|
|
|
|
});
|
|
|
|
@ -37,8 +35,11 @@ const createWhatsApp = async phone => {
|
|
|
|
|
let qrCode = null;
|
|
|
|
|
const channelId = generateId();
|
|
|
|
|
const whatsAppNo = phone;
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs-' + phone + '.txt'));
|
|
|
|
|
logger.level = 'trace';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
@ -54,38 +55,34 @@ const createWhatsApp = async phone => {
|
|
|
|
|
const sendTextMessage = async (number, content, externalId) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
|
|
|
|
|
if (number === whatsAppNo) {
|
|
|
|
|
// whatsAppNo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
waSocket.sendMessage(jid, { text: content })
|
|
|
|
|
.then(msgInfo => {
|
|
|
|
|
resolve({
|
|
|
|
|
messageId: msgInfo?.key?.id ?? generateId(),
|
|
|
|
|
externalId
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(ex => {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
|
messge: `[${whatsAppNo}->${number}]发送文本消息出错`,
|
|
|
|
|
return new Promise(() => {
|
|
|
|
|
waSocket.sendMessage(jid, { text: content })
|
|
|
|
|
.then(msg => {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
externalId,
|
|
|
|
|
status: 'saved',
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: ex
|
|
|
|
|
eventSource: 'sendMessage.promise.then',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
})
|
|
|
|
|
console.error(`[${whatsAppNo}->${number}]发送文本消息出错: `, ex);
|
|
|
|
|
})
|
|
|
|
|
.catch(ex => {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
externalId,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
errro: `发送文本消息出错 ` + ex,
|
|
|
|
|
eventSource: 'sendMessage.promise.catch',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
|
messge: `[${whatsAppNo}->${number}]发送文本消息出错`,
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: ex
|
|
|
|
|
})
|
|
|
|
|
console.error(`[${whatsAppNo}->${number}]发送文本消息出错: `, ex);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sendImageMessage = async (number, imageUrl) => {
|
|
|
|
@ -131,18 +128,6 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
const messageType = Object.keys(msg.message)[0];
|
|
|
|
|
console.log('messageType', messageType);
|
|
|
|
|
// if (messageType === 'imageMessage') {
|
|
|
|
|
// const buffer = await downloadMediaMessage(
|
|
|
|
|
// msg,
|
|
|
|
|
// 'buffer',
|
|
|
|
|
// {},
|
|
|
|
|
// {
|
|
|
|
|
// logger,
|
|
|
|
|
// reuploadRequest: waSocket.updateMediaMessage,
|
|
|
|
|
// },
|
|
|
|
|
// );
|
|
|
|
|
// await writeFile('d:/my-download.jpeg', buffer);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
|
|
|
|
@ -253,7 +238,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleCredsUpdate = async messageUpdate => {
|
|
|
|
|
const handleCredsUpdate = async () => {
|
|
|
|
|
await saveCreds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -266,13 +251,13 @@ const createWhatsApp = async phone => {
|
|
|
|
|
creds: state.creds,
|
|
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
|
|
},
|
|
|
|
|
connectTimeoutMs: 1000*10,
|
|
|
|
|
defaultQueryTimeoutMs: 1000*10,
|
|
|
|
|
keepAliveIntervalMs: 1000*10,
|
|
|
|
|
retryRequestDelayMs: 1000*10,
|
|
|
|
|
connectTimeoutMs: 1000*60*1,
|
|
|
|
|
defaultQueryTimeoutMs: 1000*60*1,
|
|
|
|
|
keepAliveIntervalMs: 1000*60*1,
|
|
|
|
|
//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.macOS('SAFARI'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.baileys('WEAR_OS'),//Browsers.baileys('WEAR_OS'),//
|
|
|
|
|
browser: Browsers.baileys('WEAR_OS'),//Browsers.macOS('SAFARI'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.baileys('WEAR_OS'),//
|
|
|
|
|
msgRetryCounterCache,
|
|
|
|
|
generateHighQualityLinkPreview: false,
|
|
|
|
|
syncFullHistory: false,
|
|
|
|
@ -288,7 +273,6 @@ const createWhatsApp = async phone => {
|
|
|
|
|
if((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
|
|
start();
|
|
|
|
|
} else {
|
|
|
|
|
console.log('Connection closed: ', lastDisconnect);
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
|
whatsAppNo, channelId,
|
|
|
|
@ -297,7 +281,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
|
// 扫码成功后向这个群发消息,后续就能使用 API 发送了。
|
|
|
|
|
// 扫码成功后向这个群('120363363417115199@g.us')发消息,后续就能使用 API 发送了。
|
|
|
|
|
sendTextMessage('120363363417115199@g.us', whatsAppNo + ' 登录成功:' + new Date().toString());
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
status: 'open', whatsAppNo, channelId,
|
|
|
|
|