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
# Why
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
10 11 12
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.
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
13

Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
14 15
Can you image that? I'm dying...

Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
16
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). ;-)
Huan (李卓桓)'s avatar
links  
Huan (李卓桓) 已提交
17

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
18 19 20 21 22
# 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 (李卓桓) 已提交
23

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24
# Example
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
25
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 (李卓桓) 已提交
26
```javascript
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
27
const Wechaty = require('wechaty')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
28
const bot = new Wechaty()
Huan (李卓桓)'s avatar
links  
Huan (李卓桓) 已提交
29

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
30 31 32
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 (李卓桓) 已提交
33

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  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 (李卓桓) 已提交
55
Main bot class.
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
56 57

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

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

### 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 (李卓桓) 已提交
90
To-Be-Supported
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
91 92

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

### 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 (李卓桓) 已提交
110

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
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 145 146
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 (李卓桓) 已提交
147

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
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 177 178
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 (李卓桓) 已提交
179

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
180 181 182 183 184 185 186 187 188 189 190
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 (李卓桓) 已提交
191
Wechaty use [TAP protocol](http://testanything.org/) to test itself by [tape](https://github.com/substack/tape).
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
192

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

Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
198 199
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 (李卓桓) 已提交
200 201 202
# Version History

## v0.0.5 (2016/5/11)
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
203 204 205 206 207
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 (李卓桓) 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

# 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 (李卓桓) 已提交
225
* Create a issue, then send me a pull request(with unit test please).
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239

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