|
|
|
@ -32,7 +32,7 @@ waEmitter.on('connection:close', event => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const createWhatsApp = async phone => {
|
|
|
|
|
let waSocket = null;
|
|
|
|
|
// let waSocket = null;
|
|
|
|
|
let qrCode = null;
|
|
|
|
|
let connectionStatus = 'offline'
|
|
|
|
|
const channelId = generateId();
|
|
|
|
@ -200,7 +200,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
}
|
|
|
|
|
const start = () => {
|
|
|
|
|
|
|
|
|
|
waSocket = makeWASocket({
|
|
|
|
|
const waSocket = makeWASocket({
|
|
|
|
|
version,
|
|
|
|
|
logger,
|
|
|
|
|
auth: {
|
|
|
|
@ -221,102 +221,120 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
store?.bind(waSocket.ev);
|
|
|
|
|
|
|
|
|
|
const sendTextMessage = async (number, content, externalId) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
|
|
|
|
|
return new Promise(() => {
|
|
|
|
|
waSocket.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: 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}]发送图片消息出错`,
|
|
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
console.error(`[${whatsAppNo}->${number}]发送图片消息出错: `, ex);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getProfilePicture = async (whatsAppNo) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(number);
|
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
waSocket.ev.on('connection.update', async update => {
|
|
|
|
|
console.log('connection update: ', update);
|
|
|
|
|
const { connection, lastDisconnect, qr } = update;
|
|
|
|
|
|
|
|
|
|
if (connection === 'close') {
|
|
|
|
|
if((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
|
|
start();
|
|
|
|
|
} else {
|
|
|
|
|
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.emit('connection:close', {
|
|
|
|
|
whatsAppNo, channelId,
|
|
|
|
|
eventSource: serverConfig.name + '.connection.update.close',
|
|
|
|
|
status: 'offline',
|
|
|
|
|
});
|
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.message', event => {
|
|
|
|
|
// const jid = formatPhoneNumber(event.to);
|
|
|
|
|
const externalId = event.externalId;
|
|
|
|
|
waSocket.sendMessage(event.to + '@s.whatsapp.net', { text: serverConfig.name + '.request.send.message: ' + event.content + new Date().toString()})
|
|
|
|
|
}
|
|
|
|
|
} 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('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),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
// 第一次一分钟,后面是 20 秒更新一次
|
|
|
|
|
if (qrCode === null) {
|
|
|
|
|
qrCode = qr;
|
|
|
|
|
console.info('qr: ', qr);
|
|
|
|
|
} else {
|
|
|
|
|
// 第一次二维码时效后退出,不需要等待更新二维码
|
|
|
|
|
waSocket.logout(() => '二维码已过期');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//sendTextMessage(number, content, externalId);
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
// 第一次一分钟,后面是 20 秒更新一次
|
|
|
|
|
if (qrCode === null) {
|
|
|
|
|
qrCode = qr;
|
|
|
|
|
console.info('qr: ', qr);
|
|
|
|
|
} else {
|
|
|
|
|
// 第一次二维码时效后退出,不需要等待更新二维码
|
|
|
|
|
waSocket.logout(() => '二维码已过期');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('creds.update', handleCredsUpdate);
|
|
|
|
|
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 {
|
|
|
|
|