wechaty.js 2.3 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2 3 4
/**
 *
 * wechaty: Wechat for Bot. and for human who talk to bot/robot
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
5
 * Class Wechaty
6
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
7 8 9 10
 * Licenst: ISC
 * https://github.com/zixia/wechaty
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
11

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
12 13
const log           = require('npmlog')
const EventEmitter  = require('events')
14

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
15 16 17 18 19 20
const Puppet      = require('./puppet')
const PuppetWeb   = require('./puppet-web')

const Message     = require('./message')
const Contact     = require('./contact')
const Group       = require('./group')
21 22

class Wechaty extends EventEmitter {
Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
23
  constructor(options) {
24
    super()
25 26
    this.options = options || {}
    this.options.puppet = this.options.puppet || 'web'
27

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
28
    this.VERSION = require('../package.json').version
29 30 31
  }
  toString() { return 'Class Wechaty(' + this.puppet + ')'}
  init() {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
32
    log.info('Wechaty', 'init() with version: %s', this.VERSION)
33 34
    this.initPuppet()
    this.initEventHook()
35

36
    return this.puppet.init()
37 38 39
    .then(r => {
      return this // for chaining
    })
40 41 42
  }
  initPuppet() {
    switch (this.options.puppet) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
43
      case 'web':
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44
        this.puppet = new Puppet.Web({
45 46
          head:   this.options.head
          , port: this.options.port
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
47
        })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
48 49
        break
      default:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
50
        throw new Error('Puppet unsupport(yet): ' + this.options.puppet)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
51
    }
52 53 54 55 56 57 58
    return Promise.resolve(this.puppet)
  }
  initEventHook() {
    // scan qrCode
    this.puppet.on('scan', (e) => {
      this.emit('scan', e)
    })
59 60 61 62 63 64 65 66 67
    this.puppet.on('message', (e) => {
      this.emit('message', e)
    })
    this.puppet.on('login', (e) => {
      this.emit('login', e)
    })
    this.puppet.on('logout', (e) => {
      this.emit('logout', e)
    })
68 69 70 71 72 73 74 75 76 77

    /**
     * TODO: support more events:
     * 1. error
     * 2. send
     * 3. reply
     * 4. quit
     * 5. ...
     */

78
    return Promise.resolve()
79 80
  }

81 82
  quit()   { return this.puppet.quit() }

Huan (李卓桓)'s avatar
v0.0.9  
Huan (李卓桓) 已提交
83
  send(message)   { return this.puppet.send(message) }
84
  reply(message, reply) { return this.puppet.reply(message, reply) }
85

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
86 87 88 89
  ding()          {
    // TODO: test through the server & browser
    return 'dong'
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
90

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
91 92 93 94
  /**
   * @deprecated
   * use on('scan', ({code, url}) => {}) instead.
   */
Huan (李卓桓)'s avatar
fix  
Huan (李卓桓) 已提交
95
  getLoginQrImgUrl() { return this.puppet.getLoginQrImgUrl() }
96 97
}

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
98
Puppet.Web = PuppetWeb
99

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
100 101 102 103 104 105
Object.assign(Wechaty, {
  Puppet:     Puppet
  , Message:  Message
  , Contact:  Contact
  , Group:    Group
})
106

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
107 108 109
/**
 * Expose `Wechaty`.
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
110
module.exports = Wechaty.default = Wechaty.Wechaty = Wechaty