README.md 5.8 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2
# Wechaty
Wechaty is Wechat for Bot.
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
3

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
4
It's a library/framework for easy creating wechat bot in 10 lines of code.
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
5

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/zixia/wechaty?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
7

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
8

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
9 10 11 12
# Why
My daily life/work is depends on wechat too heavy. 
* I have almost 14,000 wechat friends at May 2014, before new rule of 5000 friends max limit is set by wechat team.
* I have almost 400 wechat groups that almost all of them have more than 400 members.
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
13

Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
14
So I need a tireless bot working for me on wechat 24/7, moniting/filtering the most important message. for example: highlights the messages which contains my name(especialy in a noisy group). ;-)
Huan (李卓桓)'s avatar
links  
Huan (李卓桓) 已提交
15

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
16 17 18 19 20
# Installation and Usage
The recommended installation method is a local NPM install for your project:
```bash
$ npm install --save wechaty
```
Huan (李卓桓)'s avatar
links  
Huan (李卓桓) 已提交
21

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
22
# Example
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
23
Wechaty is very easy to use. The following 10 lines code will implement a bot who can auto reply message for you:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24
```javascript
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
25
const Wechaty = require('wechaty')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26
const bot = new Wechaty()
Huan (李卓桓)'s avatar
links  
Huan (李卓桓) 已提交
27

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
28 29 30
bot.init()
.then(bot.getLoginQrImgUrl.bind(bot.puppet))
.then(url => console.log(`Scan qrcode in url to login: \n${url}`))
Huan (李卓桓)'s avatar
links  
Huan (李卓桓) 已提交
31

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
32 33
bot.on('message', m => {
  console.log('RECV: ' + m.get('content'))  // 1. print received message
Huan (李卓桓)'s avatar
links  
Huan (李卓桓) 已提交
34

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  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.

# API

## Class Wechaty
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
53
Main bot class.
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
54 55

```javascript
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
56
const bot = new Wechaty()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57 58 59 60 61 62
```

### Wechaty.init()
Initialize the bot, return Promise.
```javascript
bot.init()
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
63 64 65
.then(() => {
  // do other staff with bot here
}
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
```

### Wechaty.getLoginQrImgUrl()
Get the login QrCode image url. Must be called after init().  

Return a Promise, for url link.

```javascript
bot.getLoginQrImgUrl()
.then(url => {
  // show url
})
```
### Event: `message`
Emit when there's a new message.
```javascript
bot.on('message', callback)
```
Callback will get a instance of Message Class. (see `Class Message`)

### Event: `login` & `logout`

Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
88
To-Be-Supported
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
89 90

## Class Message
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
91
All messages will be encaped in Message.
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

### Message.ready()
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.

```javascript
message.ready()
.then(() => {
  // Here we can be sure all the data is ready for use.
})
```
### Message.get(prop)
Get prop from a message. 

Supported prop list:
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
108

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
1. `id` :String
1. `from` :Contact
1. `to` :Contact
1. `content` :String
1. `group` :Group
1. `date` :Date

```javascript
message.get('content')
```

### Message.set(prop, value)
Set prop to value for a message.

Supported prop list: the same as `get(prop)`

```javascript
message.set('content', 'Hello, World!')
```
## Class Contact

### Contact.ready()
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.

```javascript
contact.ready()
.then(() => {
  // Here we can be sure all the data is ready for use.
})
```
### Contact.get(prop)
Get prop from a contact. 

Supported prop list:
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
145

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
1. `id` :String
1. `weixin` :String
1. `name` :String
1. `remark` :String
1. `sex` :Number
1. `province` :String
1. `city` :String
1. `signature` :String

```javascript
contact.get('name')
```

## Class Group

### Group.ready()
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.

```javascript
group.ready()
.then(() => {
  // Here we can be sure all the data is ready for use.
})
```

### Group.get(prop)
Get prop from a group. 

Supported prop list:
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
177

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
178 179 180 181 182 183 184 185 186 187 188
1. `id` :String
1. `name` :String
1. `members` :Array
    1. `contact` :Contact
    1. `name` :String

```javascript
group.get('members').length
```

# Test
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
189
Wechaty use [TAP protocol](http://testanything.org/) to test itself by [tape](https://github.com/substack/tape).
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
190

Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
191
To test Wechaty, run:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
192 193 194 195
```bash
$ npm test
```

Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
196 197
Know more about tape: [Why I use Tape Instead of Mocha & So Should You](https://medium.com/javascript-scene/why-i-use-tape-instead-of-mocha-so-should-you-6aa105d8eaf4#.qxrrf2938)

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
198 199 200
# Version History

## v0.0.5 (2016/5/11)
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
201 202 203 204 205
1. receive & send message 
1. show contacts info
1. show groups info
1. 1st usable version
1. start coding from 1st May 2016
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

# Todo List
1. Deal with friend request
1. Manage contacts(send friend request/delete contact etc.)
1. You are welcome to issue your needs.

# Known Issues & Support
1. phantomjs not work(no socket.io connect from browser)
2. firefox need to use unstable mode(or inject will be blocked almost forever)

Github Issue - https://github.com/zixia/wechaty/issues

# Contributing
* Lint: eslint
    ```bash
    $ npm lint
    ```
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
223
* Create a issue, then send me a pull request(with unit test please).
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237

Author
-----------------
Zhuohuan LI <zixia@zixia.net> (http://linkedin.com/in/zixia)

<a href="http://stackoverflow.com/users/1123955/zixia">
<img src="http://stackoverflow.com/users/flair/1123955.png" width="208" height="58" alt="profile for zixia at Stack Overflow, Q&amp;A for professional and enthusiast programmers" title="profile for zixia at Stack Overflow, Q&amp;A for professional and enthusiast programmers">
</a>

Copyright & License
-------------------
* Code & Docs 2016© zixia
* Code released under the ISC license
* Docs released under Creative Commons