Wechaty is Wechat for Bot.
Easy creating wechat robot code in 10 lines.
My daily life/work depends on too much wechat.
- I almost have 14,000 wechat friends till May 2014, before wechat restricting total number of friends to 5,000.
- I almost have 400 wechat groups that most of them have more than 400 members.
Can you image that? I'm dying...
So a tireless bot working for me 24x7 on wechat, moniting/filtering the most important messages is badly needed. For example: highlights discusstion which contains the KEYWORDS I want to follow up(especialy in a noisy group). ;-)
Wechaty is unbelievable easy to use: 10 lines of javascript is enough for your first wechat robot.
The following shortest 10 lines code will implement a roger bot who can auto reply message(roger) for you:
const Wechaty = require('wechaty')
const bot = new Wechaty()
bot.init()
.then(bot.getLoginQrImgUrl.bind(bot.puppet))
.then(url => console.log(`Scan qrcode in url to login: \n${url}`))
bot.on('message', m => {
console.log('RECV: ' + m.get('content')) // 1. print received message
const reply = new Wechaty.Message() // 2. create reply message
.set('to', m.get('from')) // 1) set receipt
.set('content', 'roger.') // 2) set content
bot.send(reply) // 3. do reply!
.then(() => console.log('REPLY: roger.')) // 4. print reply message
})
Notice that you need to wait a moment while bot trying to get the login QRCode from Wechat. As soon as the bot got login QRCode url, he will print url out. You need to scan the qrcode in wechat, and confirm login.
After that, bot will on duty. (roger-bot source can be found at here)
There's another basic usage demo bot named ding-dong-bot, who can reply dong
when receive a message ding
.
To Be Wroten.
Plan to glued with Machine Learning/Deep Learning/Neural Network/Natural Language Processing.
The recommended installation method is a local NPM install for your project:
$ npm install --save wechaty
ECMAScript2015/ES6. I develop and test wechaty under nodejs6.0.
Main bot class.
const bot = new Wechaty()
Initialize the bot, return Promise.
bot.init()
.then(() => {
// do other staff with bot here
}
Get the login QrCode image url. Must be called after init().
Return a Promise, for url link.
bot.getLoginQrImgUrl()
.then(url => {
// show url
})
Emit when there's a new message.
bot.on('message', callback)
Callback will get a instance of Message Class. (see Class Message
)
To-Be-Supported
All messages will be encaped in Message.
A message may be not fully initialized yet. Call ready()
to confirm we get all the data needed.
Return a Promise, will be resolved when all data is ready.
message.ready()
.then(() => {
// Here we can be sure all the data is ready for use.
})
Get prop from a message.
Supported prop list:
id
:Stringfrom
:Contactto
:Contactcontent
:Stringgroup
:Groupdate
:Date
message.get('content')
Set prop to value for a message.
Supported prop list: the same as get(prop)
message.set('content', 'Hello, World!')
A Contact may be not fully initialized yet. Call ready()
to confirm we get all the data needed.
Return a Promise, will be resolved when all data is ready.
contact.ready()
.then(() => {
// Here we can be sure all the data is ready for use.
})
Get prop from a contact.
Supported prop list:
id
:Stringweixin
:Stringname
:Stringremark
:Stringsex
:Numberprovince
:Stringcity
:Stringsignature
:String
contact.get('name')
A group may be not fully initialized yet. Call ready()
to confirm we get all the data needed.
Return a Promise, will be resolved when all data is ready.
group.ready()
.then(() => {
// Here we can be sure all the data is ready for use.
})
Get prop from a group.
Supported prop list:
id
:Stringname
:Stringmembers
:Arraycontact
:Contactname
:String
group.get('members').length
Wechaty use TAP protocol to test itself by tape.
To test Wechaty, run:
$ npm test
Know more about tape: Why I use Tape Instead of Mocha & So Should You
- receive & send message
- show contacts info
- show groups info
- 1st usable version
- start coding from 1st May 2016
- Deal with friend request
- Manage contacts(send friend request/delete contact etc.)
Everybody is welcome to issue your needs.
- phantomjs not work(no socket.io connect from browser)
- firefox need to use unstable mode(or inject will be blocked almost forever)
Github Issue - https://github.com/zixia/wechaty/issues
-
Lint: eslint
$ npm lint
-
Create a issue, fork, then send a pull request(with unit test please).
Zhuohuan LI zixia@zixia.net (http://linkedin.com/in/zixia)
- Code & Docs 2016© zixia
- Code released under the ISC license
- Docs released under Creative Commons