|
|
import {
|
|
|
makeWASocket,
|
|
|
WAProto,
|
|
|
DisconnectReason,
|
|
|
fetchLatestBaileysVersion,
|
|
|
makeCacheableSignalKeyStore,
|
|
|
makeInMemoryStore,
|
|
|
useMultiFileAuthState,
|
|
|
downloadMediaMessage
|
|
|
} from '@whiskeysockets/baileys'
|
|
|
|
|
|
import { writeFile } from 'fs/promises'
|
|
|
|
|
|
import NodeCache from 'node-cache'
|
|
|
import P from 'pino'
|
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs.txt'))
|
|
|
logger.level = 'trace'
|
|
|
|
|
|
// external map to store retry counts of messages when decryption/encryption fails
|
|
|
// keep this out of the socket itself, so as to prevent a message decryption/encryption loop across socket restarts
|
|
|
const msgRetryCounterCache = new NodeCache()
|
|
|
|
|
|
// the store maintains the data of the WA connection in memory
|
|
|
// can be written out to a file & read from it
|
|
|
const store = makeInMemoryStore({ logger })
|
|
|
store?.readFromFile('./baileys_store_multi.json')
|
|
|
// save every 10s
|
|
|
setInterval(() => {
|
|
|
store?.writeToFile('./baileys_store_multi.json')
|
|
|
}, 10_000)
|
|
|
|
|
|
// start a connection
|
|
|
const startSock = async () => {
|
|
|
|
|
|
const channelId = '创建时赋值,唯一标识'
|
|
|
const phone = '手机号'
|
|
|
const createTimestamp = '创建时间戳'
|
|
|
const status = 'close, open, connecting, online'
|
|
|
const waVersion = 'v2.3000.1017531287, latest(out)'
|
|
|
|
|
|
const sendTextMessage = (whatsAppNo, content) => {
|
|
|
sock.sendMessage(whatsAppNo + '@s.whatsapp.net', { text: content })
|
|
|
}
|
|
|
|
|
|
const sendImageMessage = (whatsAppNo, imageUrl) => {
|
|
|
sock.sendMessage(whatsAppNo + '@s.whatsapp.net', {
|
|
|
image: { url: imageUrl},
|
|
|
})
|
|
|
}
|
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info')
|
|
|
// fetch latest version of WA Web
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion()
|
|
|
console.log(`using WA v${version.join('.')}, isLatest: ${isLatest}`)
|
|
|
|
|
|
const sock = makeWASocket({
|
|
|
version,
|
|
|
logger,
|
|
|
auth: {
|
|
|
creds: state.creds,
|
|
|
/** caching makes the store faster to send/recv messages */
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
},
|
|
|
msgRetryCounterCache,
|
|
|
generateHighQualityLinkPreview: true,
|
|
|
})
|
|
|
|
|
|
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', async (upsert) => {
|
|
|
console.log('收到消息:', JSON.stringify(upsert, undefined, 2))
|
|
|
|
|
|
if (upsert.type === 'notify') {
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
|
|
const messageType = Object.keys(msg.message)[0]
|
|
|
|
|
|
console.log('messageType', messageType)
|
|
|
if (messageType === 'imageMessage') {
|
|
|
// download the message
|
|
|
const buffer = await downloadMediaMessage(
|
|
|
msg,
|
|
|
'buffer',
|
|
|
{ },
|
|
|
{
|
|
|
logger,
|
|
|
// pass this so that baileys can request a reupload of media
|
|
|
// that has been deleted
|
|
|
reuploadRequest: sock.updateMediaMessage
|
|
|
}
|
|
|
)
|
|
|
// save to file
|
|
|
await writeFile('d:/my-download.jpeg', buffer)
|
|
|
|
|
|
console.log('writeFile', messageType)
|
|
|
}
|
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text
|
|
|
|
|
|
if (text.indexOf('图片') > -1){
|
|
|
sendImageMessage('8617607730395', 'https://images.asiahighlights.com/allpicture/2022/07/8a7d9ced5936463bb904c82a_cut_750x850_349.webp')
|
|
|
} else if (text.indexOf('文本') > -1){
|
|
|
sendTextMessage('8617607730395', '文本消息' + new Date().toString())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
|
|
|
// 不绑定不会影响扫码登录
|
|
|
store?.bind(sock.ev)
|
|
|
|
|
|
// the process function lets you process all events that just occurred
|
|
|
// efficiently in a batch
|
|
|
sock.ev.process(
|
|
|
// events is a map for event name => event data
|
|
|
async (events) => {
|
|
|
// something about the connection changed
|
|
|
// maybe it closed, or we received all offline message or connection opened
|
|
|
if (events['connection.update']) {
|
|
|
const update = events['connection.update']
|
|
|
const { connection, lastDisconnect, qr } = update
|
|
|
if (connection === 'close') {
|
|
|
console.log('链接断开:', lastDisconnect)
|
|
|
if (lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
startSock()
|
|
|
} else {
|
|
|
sock.end((error) => console.error('end.error: ', error))
|
|
|
sock.logout((msg) => console.error('logout.msg: ', msg))
|
|
|
console.log('Connection closed. You are logged out.')
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 扫码成功,可以发送消息
|
|
|
if (update.connection === 'open') {
|
|
|
await sock.sendMessage('8617607730395' + '@s.whatsapp.net', { text: 'OPEN: ' + new Date().toString() })
|
|
|
}
|
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
if (update.connection === 'connecting') {
|
|
|
// qr
|
|
|
}
|
|
|
|
|
|
console.log('connection update', update)
|
|
|
}
|
|
|
|
|
|
// credentials updated -- save them
|
|
|
if (events['creds.update']) {
|
|
|
await saveCreds()
|
|
|
}
|
|
|
|
|
|
// history received
|
|
|
if (events['messaging-history.set']) {
|
|
|
const { chats, contacts, messages, isLatest, progress, syncType } = events['messaging-history.set']
|
|
|
if (syncType === WAProto.HistorySync.HistorySyncType.ON_DEMAND) {
|
|
|
console.log('received on-demand history sync, messages=', messages)
|
|
|
}
|
|
|
console.log(`recv ${chats.length} chats, ${contacts.length} contacts, ${messages.length} msgs (is latest: ${isLatest}, progress: ${progress}%), type: ${syncType}`)
|
|
|
}
|
|
|
|
|
|
// received a new message
|
|
|
if (events['messages.upsert']) {
|
|
|
const upsert = events['messages.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
|
|
|
console.log('收到 notify:', text)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
)
|
|
|
|
|
|
return sock
|
|
|
}
|
|
|
|
|
|
startSock()
|