|
|
|
@ -35,13 +35,15 @@ setInterval(() => {
|
|
|
|
|
const createWhatsApp = async (phone) => {
|
|
|
|
|
const channelId = generateId()
|
|
|
|
|
const whatsAppNo = phone
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info_' + phone + '_' + channelId)
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info/' + phone + '_' + channelId)
|
|
|
|
|
// fetch latest version of WA Web
|
|
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion()
|
|
|
|
|
const waVersion = version.join('.') + ', ' + (isLatest ? 'latest' : 'out')
|
|
|
|
|
// const waEmitter = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
const formatPhoneNumber = (number) => {
|
|
|
|
|
|
|
|
|
|
if (number === null || number === undefined) return ''
|
|
|
|
|
|
|
|
|
|
if (number.indexOf('@g.us') > -1) {
|
|
|
|
|
return number
|
|
|
|
|
} else if (number.indexOf('@broadcast') > -1) {
|
|
|
|
@ -52,6 +54,9 @@ const createWhatsApp = async (phone) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parsePhoneNumber = (number) => {
|
|
|
|
|
|
|
|
|
|
if (number === null || number === undefined) return ''
|
|
|
|
|
|
|
|
|
|
if (number.indexOf('@s.whatsapp.net') > -1) {
|
|
|
|
|
return number.split('@')[0]
|
|
|
|
|
} else {
|
|
|
|
@ -59,9 +64,24 @@ const createWhatsApp = async (phone) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const on = (eventName, listener) => {
|
|
|
|
|
waEmitter.on(eventName, listener);
|
|
|
|
|
}
|
|
|
|
|
// status: sent read delivered failed
|
|
|
|
|
// 2 sent, 3 delivered, 4 read, 0 error
|
|
|
|
|
// Time: 2008-07-07 15:37:07
|
|
|
|
|
|
|
|
|
|
const formatStatus = (number) => {
|
|
|
|
|
if (number === 2) return 'sent'
|
|
|
|
|
else if (number === 3) return 'delivered'
|
|
|
|
|
else if (number === 4) return 'read'
|
|
|
|
|
else if (number === 0) return 'error'
|
|
|
|
|
else return 'played'
|
|
|
|
|
}
|
|
|
|
|
const formatTimestamp = (timestamp) => {
|
|
|
|
|
if (timestamp === null) return ''
|
|
|
|
|
|
|
|
|
|
const datetime = new Date(timestamp * 1000)
|
|
|
|
|
|
|
|
|
|
return datetime.getFullYear() + '-' + (datetime.getMonth() + 1) + '-' + datetime.getDay() + ' ' + datetime.getHours() + ':' + datetime.getMinutes() + ':' + datetime.getSeconds()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sendTextMessage = (whatsAppNo, content) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo)
|
|
|
|
@ -158,25 +178,6 @@ const createWhatsApp = async (phone) => {
|
|
|
|
|
sendTextMessage('120363335516526642@g.us', '这是群消息:' + new Date().toString())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// status: sent read delivered failed
|
|
|
|
|
// 2 sent, 3 delivered, 4 read, 0 error
|
|
|
|
|
// Time: 2008-07-07 15:37:07
|
|
|
|
|
|
|
|
|
|
const formatStatus = (number) => {
|
|
|
|
|
if (number === 2) return 'sent'
|
|
|
|
|
else if (number === 3) return 'delivered'
|
|
|
|
|
else if (number === 4) return 'read'
|
|
|
|
|
else if (number === 0) return 'error'
|
|
|
|
|
else return 'played'
|
|
|
|
|
}
|
|
|
|
|
const formatTimestamp = (timestamp) => {
|
|
|
|
|
if (timestamp === null) return ''
|
|
|
|
|
|
|
|
|
|
const datetime = new Date(msg.messageTimestamp * 1000)
|
|
|
|
|
|
|
|
|
|
return datetime.getFullYear() + '-' + (datetime.getMonth() + 1) + '-' + datetime.getDay() + ' ' + datetime.getHours() + ':' + datetime.getMinutes() + ':' + datetime.getSeconds()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
"id": msg.key.id,
|
|
|
|
@ -188,9 +189,8 @@ const createWhatsApp = async (phone) => {
|
|
|
|
|
"text": {
|
|
|
|
|
"body": text
|
|
|
|
|
},
|
|
|
|
|
conversation: {
|
|
|
|
|
id: msg.key.participant,
|
|
|
|
|
type: '不区分',
|
|
|
|
|
participant: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
name: msg.pushName
|
|
|
|
|
},
|
|
|
|
|
"createTime": formatTimestamp(msg.messageTimestamp),
|
|
|
|
@ -210,9 +210,8 @@ const createWhatsApp = async (phone) => {
|
|
|
|
|
"text": {
|
|
|
|
|
"body": text
|
|
|
|
|
},
|
|
|
|
|
conversation: {
|
|
|
|
|
id: msg.key.participant,
|
|
|
|
|
type: '不区分',
|
|
|
|
|
participant: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
name: msg.pushName
|
|
|
|
|
},
|
|
|
|
|
"createTime": formatTimestamp(msg.messageTimestamp),
|
|
|
|
@ -250,7 +249,7 @@ const createWhatsApp = async (phone) => {
|
|
|
|
|
version: waVersion,
|
|
|
|
|
channelId : channelId,
|
|
|
|
|
phone: phone,
|
|
|
|
|
start, on,sendTextMessage,sendImageMessage
|
|
|
|
|
start, sendTextMessage, sendImageMessage
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|