|
|
|
@ -1,10 +1,8 @@
|
|
|
|
|
import {
|
|
|
|
|
makeWASocket,
|
|
|
|
|
delay,
|
|
|
|
|
WAProto,
|
|
|
|
|
DisconnectReason,
|
|
|
|
|
fetchLatestBaileysVersion,
|
|
|
|
|
getAggregateVotesInPollMessage,
|
|
|
|
|
makeCacheableSignalKeyStore,
|
|
|
|
|
makeInMemoryStore,
|
|
|
|
|
useMultiFileAuthState,
|
|
|
|
@ -13,6 +11,8 @@ import {
|
|
|
|
|
import NodeCache from 'node-cache'
|
|
|
|
|
import P from 'pino'
|
|
|
|
|
|
|
|
|
|
import fs from "fs"
|
|
|
|
|
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs.txt'))
|
|
|
|
|
logger.level = 'trace'
|
|
|
|
|
|
|
|
|
@ -31,6 +31,24 @@ setInterval(() => {
|
|
|
|
|
|
|
|
|
|
// start a connection
|
|
|
|
|
const startSock = async () => {
|
|
|
|
|
|
|
|
|
|
const channelId = '创建时赋值,唯一标识'
|
|
|
|
|
const phone = '手机号'
|
|
|
|
|
const createTimestamp = '创建时间戳'
|
|
|
|
|
const status = 'close, open, connecting'
|
|
|
|
|
|
|
|
|
|
const sendTextMessage = (whatsAppNo, content) => {
|
|
|
|
|
sock.sendMessage(whatsAppNo + '@s.whatsapp.net', { text: content })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sendMediaMessage = (whatsAppNo, content) => {
|
|
|
|
|
sock.sendMessage(whatsAppNo + '@s.whatsapp.net', {
|
|
|
|
|
video: fs.readFileSync("d:\\Workspace\\1.jpg"),
|
|
|
|
|
caption: "hello!",
|
|
|
|
|
gifPlayback: true
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info')
|
|
|
|
|
// fetch latest version of WA Web
|
|
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion()
|
|
|
|
@ -53,6 +71,37 @@ const startSock = async () => {
|
|
|
|
|
getMessage,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
sock.ev.on('connection.update', (update) => {
|
|
|
|
|
const { connection, lastDisconnect, qr } = update
|
|
|
|
|
if(connection === 'close') {
|
|
|
|
|
console.info('链接断了')
|
|
|
|
|
} else if(connection === 'open') {
|
|
|
|
|
console.info('扫码成功')
|
|
|
|
|
} else if(connection === 'connecting') {
|
|
|
|
|
console.info('二维码:', qr)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
sock.ev.on('messages.upsert', (upsert) => {
|
|
|
|
|
console.log('收到消息:', JSON.stringify(upsert, undefined, 2))
|
|
|
|
|
|
|
|
|
|
if (upsert.type === 'notify') {
|
|
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text
|
|
|
|
|
|
|
|
|
|
if (text.indexOf('图片')){
|
|
|
|
|
sendMediaMessage('8617607730395', 'ddddd')
|
|
|
|
|
} else if (text.indexOf('文本')){
|
|
|
|
|
sendMediaMessage('8617607730395', 'ddddd')
|
|
|
|
|
|
|
|
|
|
sendTextMessage('8617607730395', '发送文本:' + new Date().toString())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 不绑定不会影响扫码登录
|
|
|
|
|
// store?.bind(sock.ev)
|
|
|
|
|
|
|
|
|
@ -95,18 +144,6 @@ const startSock = async () => {
|
|
|
|
|
await saveCreds()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events['labels.association']) {
|
|
|
|
|
console.log(events['labels.association'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events['labels.edit']) {
|
|
|
|
|
console.log(events['labels.edit'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events.call) {
|
|
|
|
|
console.log('recv call event', events.call)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// history received
|
|
|
|
|
if (events['messaging-history.set']) {
|
|
|
|
|
const { chats, contacts, messages, isLatest, progress, syncType } = events['messaging-history.set']
|
|
|
|
@ -130,55 +167,6 @@ const startSock = async () => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// messages updated like status delivered, message deleted etc.
|
|
|
|
|
if (events['messages.update']) {
|
|
|
|
|
console.log('messages.update: ', JSON.stringify(events['messages.update'], undefined, 2))
|
|
|
|
|
|
|
|
|
|
for (const { key, update } of events['messages.update']) {
|
|
|
|
|
if (update.pollUpdates) {
|
|
|
|
|
const pollCreation = await getMessage(key)
|
|
|
|
|
if (pollCreation) {
|
|
|
|
|
console.log(
|
|
|
|
|
'got poll update, aggregation: ',
|
|
|
|
|
getAggregateVotesInPollMessage({
|
|
|
|
|
message: pollCreation,
|
|
|
|
|
pollUpdates: update.pollUpdates,
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events['message-receipt.update']) {
|
|
|
|
|
console.log('message-receipt.update: ', events['message-receipt.update'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events['messages.reaction']) {
|
|
|
|
|
console.log('message-receipt.update: ', events['messages.reaction'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events['presence.update']) {
|
|
|
|
|
console.log('presence.update: ', events['presence.update'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events['chats.update']) {
|
|
|
|
|
console.log('chats.update: ', events['chats.update'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events['contacts.update']) {
|
|
|
|
|
for (const contact of events['contacts.update']) {
|
|
|
|
|
if (typeof contact.imgUrl !== 'undefined') {
|
|
|
|
|
const newUrl = contact.imgUrl === null ? null : await sock.profilePictureUrl(contact.id).catch(() => null)
|
|
|
|
|
console.log(`contact ${contact.id} has a new profile pic: ${newUrl}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events['chats.delete']) {
|
|
|
|
|
console.log('chats deleted ', events['chats.delete'])
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|