1年前使用过padlocal,不过是收费的每月200元的费用对于个人开发者还是不值得投入,只可以使用7天免费的注册token体验,对于初学者7天时间可能不够用,之后使用老张的微信机器人项目,稳定使用半年之久后项目被整合磁盘误删除,目前又尝试找到一个可用的微信机器人,大多数的个人微信都可以成功登录(测试请使用小号扫码)!
wechaty-puppet-wechat,由于协议自身的限制,在获取wxid、roomid存在天然缺陷
代码全部整合到index.js文件中,可以进行简单测试
由于文档的原因,限制了很多好的想法,比如我记得之前是可以自动登录的,现在开启后会频繁触发消息回复,项目对触发代码已经做了注释,对于入门开发者可以简单进行测试体验
关于自动登录频繁回复导致被踢出群聊的我,一开始我也不清楚这个问题,那个群聊的机器人开发者也在掘金,在这里为你道歉!
以下是效果图
node版本 16+的应该就可以,我使用的版本为v16.19.0
代码 index.js
const {
WechatyBuilder,
ScanStatus
} = require("wechaty"); // wechaty
const qrcodeTerminal = require("qrcode-terminal"); // 二维码生成
const log = require("log-output-color"); // 彩色日志
const bot = WechatyBuilder.build({
// name:"缓存json文件前缀", // 不需要缓存登录
puppetOptions: {
uos: false,
},
// puppet:"wechaty-puppet-wechat"
});
bot
.on("scan", (qrcode, status) => {
qrcodeTerminal.generate(qrcode, {
small: true
}) // show qrcode on console
console.log(`Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`)
if (status === ScanStatus.Confirmed) {
console.log('自动登录!')
return
}
})
.on("login", (user) => {
console.log(`用户 ${user.name()} 登录成功`)
})
.on("message", (message) => {
const contact = message.talker() // 内容对象
const text = message.text() // 接受文本
const room = message.room() // 房间对象
const name = contact.payload.name; // 名称
const alias = contact.payload.alias; // 备注名称
const type = message.type(); // 消息类型
if (message.self() || room) {
return
}
if (type == 7 && text) {
log.silly('==============')
log.warn(text) // 消息记录
}
// 测试使用小号
if (type == 7 && name == '蔚蓝' && text) { // 此处修改为自己的昵称
// 添加对话模型
const interactModel = {
'ding': 'dong!',
'你好': '你也好啊!',
'你是': '我是机器人呀!'
}
// 匹配关键词回复
if (Object.keys(interactModel).includes(text)) {
message.say(interactModel[text])
} else {
// 判断条件没写好不要随意打开 自测使用缓存自动登录会导致多次自动发送消息
// message.say('你说的是什么?我听不懂...')
}
}
})
.on("logout", (user) => {
console.log(`用户 ${user.name()} 退出成功`)
})
.on('error', (error) => {
console.error('错误:', error)
})
.start()
{
"name": "wechaty-2023",
"version": "1.0.0",
"description": "rujie",
"main": "index.js",
"scripts": {
"dev": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"log-output-color": "^0.0.1",
"qrcode-terminal": "^0.12.0",
"wechaty": "^1.19.10",
"wechaty-puppet-wechat": "^1.18.4"
}
}