|
|
|
@ -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: {
|
|
|
|
@ -223,13 +223,12 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
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 => {
|
|
|
|
|
console.error('sendTextMessage.error: ', ex)
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
externalId,
|
|
|
|
@ -237,12 +236,11 @@ const createWhatsApp = async phone => {
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
errro: `发送文本消息出错 ` + ex,
|
|
|
|
|
error: `发送文本消息出错 ` + ex,
|
|
|
|
|
eventSource: serverConfig.name + '.sendMessage.promise.catch',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sendImageMessage = async (number, imageUrl) => {
|
|
|
|
@ -297,8 +295,28 @@ const createWhatsApp = async phone => {
|
|
|
|
|
});
|
|
|
|
|
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()})
|
|
|
|
|
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),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
//sendTextMessage(number, content, externalId);
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|