JSPM

wechaty

0.0.7
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7717
  • Score
    100M100P100Q146629F
  • License ISC

Wechat for Bot. (Personal Account, NOT Official Account)

Package Exports

  • wechaty

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (wechaty) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Wechaty

Wechaty Circle CI Build Status

Wechaty is Wechat for Bot.(Personal Account Robot, NOT Official Account)

Easy creating wechat robot code in 10 lines.

Join the chat at https://gitter.im/zixia/wechaty node npm version Downloads

Why

My daily life/work depends on too much wechat.

  • I almost have 14,000 wechat friends till May 2014, before wechat restricts a 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 message is badly needed. For example: highlights discusstion which contains the KEYWORDS I want to follow up(especially in a noisy group). ;-)

Examples

Wechaty is super easy to use: 10 lines of javascript is enough for your first wechat robot.

1. Basic: 10 lines

The following 10 lines of code will implement a bot that will reply a message automatically to 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 trys to get the login QRCode from Wechat. As soon as the bot gets login QRCode url, he will print url out. You need to scan the qrcode on wechat, and confirm login.

After that, bot will be on duty. (roger-bot source can be found at here)

2. Advanced: 50 lines

There's another basic usage demo bot named ding-dong-bot, who can reply dong when bot receives a message ding.

3. Hardcore: 100 lines

To Be Written.

Plan to glue with Machine Learning/Deep Learning/Neural Network/Natural Language Processing.

Installation

The recommended installation method is a local NPM install for your project:

$ npm install --save wechaty

Requirement

ECMAScript2015/ES6. I develop and test wechaty under nodejs6.0.

API Refference

Class Wechaty

Main bot class.

const bot = new Wechaty()

Wechaty.init()

Initialize the bot, return Promise.

bot.init()
.then(() => {
  // do other staff with bot here
}

Wechaty.getLoginQrImgUrl()

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

Return a Promise, for url link.

bot.getLoginQrImgUrl()
.then(url => {
  // show url
})

Event: message

Emit when there's a new message.

bot.on('message', callback)

Callback will get an instance of Message Class. (see Class Message)

Event: login & logout

To-Be-Supported

Class Message

All messages will be encaped in Message.

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.

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:

  1. id :String
  2. from :Contact
  3. to :Contact
  4. content :String
  5. group :Group
  6. date :Date
message.get('content')

Message.set(prop, value)

Set prop to value for a message.

Supported prop list: the same as get(prop)

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.

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:

  1. id :String
  2. weixin :String
  3. name :String
  4. remark :String
  5. sex :Number
  6. province :String
  7. city :String
  8. signature :String
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.

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:

  1. id :String
  2. name :String
  3. members :Array
    1. contact :Contact
    2. name :String
group.get('members').length

Test

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

Version History

v0.0.5 (2016/5/11)

  1. Receive & send message
  2. Show contacts info
  3. Show groups info
  4. 1st usable version
  5. Start coding from May 1st 2016

Todo List

  1. Deal with friend request
  2. Manage contacts(send friend request/delete contact etc.)

Everybody is welcome to issue your needs.

Known Issues & Support

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

Contributing

  • Lint: eslint

    $ npm lint
  • Create an issue, fork, then send a pull request(with unit test please).

Author

Zhuohuan LI zixia@zixia.net (http://linkedin.com/in/zixia)

profile for zixia at Stack Overflow, Q&A for professional and enthusiast programmers
  • Code & Docs 2016© zixia
  • Code released under the ISC license
  • Docs released under Creative Commons