wechaty微信机器人通过循环查询通讯录里的某一个成员监控机器人是否掉线
//微信登录
const chatWx = async (req, res) => {
try {
// 扫码
const onScan = (qrcode, status) => {
let qrcodeImageUrl
if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
// 在控制台显示二维码
qrTerminal.generate(qrcode, { small: true })
qrcodeImageUrl = [
'https://api.qrserver.com/v1/create-qr-code/?data=',
encodeURIComponent(qrcode),
].join('')
console.log('onScan:', qrcodeImageUrl, ScanStatus[status], status)
} else {
log.info('onScan: %s(%s)', ScanStatus[status], status)
}
console.log(
`Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(
qrcode
)}`
)
console.log(qrcode)
res.send({
code: 200,
data: {
qrcode1: `https://wechaty.js.org/qrcode/${encodeURIComponent(
qrcode
)}`,
qrcode,
qrcodeImageUrl,
},
message: '成功',
})
return
}
// 登录
const onLogin = (user) => {
console.log(`${user} has logged in`)
const date = new Date()
console.log(`Current time:${date}`)
console.log(`Automatic robot chat mode has been activated`)
}
// 登出
const onLogout = (user) => {
console.log(`${user} has logged out.手动退出微信机器人。`)
customSendEmail({
subject: '微信机器人退出登录',
html: `通过logout事件监控到微信机器人退出登录`,
})
}
// 收到好友请求
const onFriendShip = async (friendship) => {
if (friendship.type() === 2) {
await friendship.accept()
// if want to send msg , you need to delay sometimes
await new Promise((r) => setTimeout(r, 1000))
await friendship.contact().say(
`免费向微信机器人提问的格式:
gpt,xxx
speech,xxx(文本转语音)
付费后向微信机器人提问的格式:
gpt4,xxx
claude3,xxx
mistral,xxx
mj,xxx(AI绘画)
gemini,xxx
文心一言v4,xxx
upload
uploadForMJ
whisper,xxx(xxx为音频或视频链接,语音转文本)
可以加微信机器人私聊
网站:
https://chat.xutongbao.top
`
)
console.log('after accept')
let contact = friendship.contact()
let alias = (await contact.alias()) || (await contact.name()) // 发消息人昵称
customSendEmail({
subject: '微信机器人加入新朋友',
html: `微信机器人加入新朋友:${alias}`,
})
}
}
const onMessage = async (msg) => {
await handleMessage({ msg, bot })
}
// const onError = (error) => {
// console.error('Bot error:', error)
// customSendEmail({
// subject: '微信机器人报错',
// html: `微信机器人报错:${error}`,
// })
// }
let bot
// eslint-disable-next-line
if (global.isLocal) {
const CHROME_BIN = process.env.CHROME_BIN
? { endpoint: process.env.CHROME_BIN }
: {}
// eslint-disable-next-line
const puppet = new PuppetPadlocal({
token: 'xxx',
})
bot = WechatyBuilder.build({
name: 'WechatEveryDay',
puppet: 'wechaty-puppet-wechat4u', // 如果有token,记得更换对应的puppet 免费
//puppet, //需要花钱
//puppet: 'wechaty-puppet-wechat', // 如果 wechaty-puppet-wechat 存在问题,也可以尝试使用上面的 wechaty-puppet-wechat4u ,记得安装 wechaty-puppet-wechat4u
puppetOptions: {
uos: true,
...CHROME_BIN,
},
})
} else {
bot = WechatyBuilder.build()
}
// 扫码
bot.on('scan', onScan)
// 登录
bot.on('login', onLogin)
// 登出
bot.on('logout', onLogout)
// 收到消息
bot.on('message', onMessage)
// 添加好友
bot.on('friendship', onFriendShip)
// bot.on('error', onError)
// bot.on('heartbeat', async (data) => {
// // do something
// console.log('心跳', data, bot.state._isActive)
// // if (bot.state._isActive) {
// // console.log('心跳在线', bot.state._isActive)
// // isHearbeatWatchOpen = true
// // }
// // if (isHearbeatWatchOpen && bot.state._isActive === false) {
// // console.log('心跳报警', bot.state._isActive)
// // }
// // 获取通讯录列表
// // let contacts = await bot.Contact.findAll()
// try {
// const contact = await bot.Contact.find({name: '徐同保'})
// console.log(contact)
// if (contact && contact.name() === '徐同保') {
// console.log('正常')
// } else {
// console.log('退出')
// }
// } catch (error) {
// console.log(error)
// }
// // 打印通讯录列表
// // console.log(contacts.map((c) => c.name()))
// })
bot.on('ready', async () => {
console.log('机器人已经准备就绪')
try {
// const contacts = await bot.Contact.findAll()
// console.log(contacts)
const contact = await bot.Contact.find({ name: '家里' })
console.log(contact.name(), Date())
if (contact && contact.name() === '家里') {
console.log('正常')
const loop = async () => {
const contact = await bot.Contact.find({ name: '家里' })
if (contact && contact.name() === '家里') {
console.log('微信机器人心跳正常', Date())
await sleep(1000 * 60 * 10)
await loop()
} else {
console.log('微信机器人,心跳报警,来至ready事件里的监控函数')
customSendEmail({
subject: '微信机器人,心跳报警,来至ready事件里的监控函数',
html: `微信机器人,心跳报警,来至ready事件里的监控函数`,
})
}
}
loop()
} else {
console.log('微信机器人监控未能成功开启2')
customSendEmail({
subject: '微信机器人监控未能成功开启2',
html: `微信机器人监控未能成功开启2`,
})
}
} catch (error) {
console.log(error)
console.log('微信机器人监控未能成功开启1')
customSendEmail({
subject: '微信机器人监控未能成功开启1',
html: `微信机器人监控未能成功开启1`,
})
}
})
// 启动微信机器人
bot
.start()
.then(() => console.log('Start to log in wechat...'))
.catch((e) => console.error(e))
} catch (error) {
console.log(error)
}
}
https://wechaty.js.org/docs/api/contact
人工智能学习网站