1.初始化
npm init npm install wechaty
2.创建js文件
const { Wechaty } = require('wechaty') // import { Wechaty } from 'wechaty' Wechaty.instance() // Global Instance .on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrcode)}`)) .on('login', user => console.log(`User ${user} logined`)) .on('message', message => console.log(`Message: ${message}`)) .start()
3.运行
node mybot.js
4.扫描二维码接入微信
复制命令行下的地址在浏览器中打开,使用微信扫码登录
5,完成
后续此微信发送或者接收的消息都会在命令行中显示
6.优化,命令行终端内显示二维码
npm --save i qrcode-terminal
scan部分代码进行修改
//··· const QrcodeTerminal = require('qrcode-terminal') //··· .on('scan', (url, code) => { let loginUrl = url.replace('qrcode', 'l') QrcodeTerminal.generate(loginUrl) console.log(url) }) //···
参考链接:https://github.com/Chatie/wechaty
https://segmentfault.com/a/1190000014584597