wechaty.js 2.7 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
const EventEmitter  = require('events')
12

13 14
const log         = require('./npmlog-env')

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

const Message     = require('./message')
const Contact     = require('./contact')
20
const Room        = require('./room')
21 22

class Wechaty extends EventEmitter {
Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
23
  constructor(options) {
24
    super()
25
    this.options = options || {}
26 27 28
    this.options.puppet     = this.options.puppet   || process.env.WECHATY_PUPPET || 'web'
    this.options.head       = this.options.head     || process.env.WECHATY_HEAD || false
    this.options.session    = this.options.session  || process.env.WECHATY_SESSION // no session, no session restore
29

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
30
    this.VERSION = require('../package.json').version
31 32 33
  }
  toString() { return 'Class Wechaty(' + this.puppet + ')'}
  init() {
34 35 36 37 38
    log.info('Wechaty', 'v%s initializing...', this.VERSION)
    log.verbose('Wechaty', 'puppet: %s' , this.options.puppet)
    log.verbose('Wechaty', 'head: %s'   , this.options.head)
    log.verbose('Wechaty', 'session: %s', this.options.session)

39 40
    this.initPuppet()
    this.initEventHook()
41

42
    return this.puppet.init()
43 44 45
    .then(r => {
      return this // for chaining
    })
46 47 48
  }
  initPuppet() {
    switch (this.options.puppet) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49
      case 'web':
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
50
        this.puppet = new Puppet.Web({
51 52
          head:   this.options.head
          , port: this.options.port
53
          , session: this.options.session
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
54
        })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
55 56
        break
      default:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57
        throw new Error('Puppet unsupport(yet): ' + this.options.puppet)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
58
    }
59 60 61 62 63 64 65
    return Promise.resolve(this.puppet)
  }
  initEventHook() {
    // scan qrCode
    this.puppet.on('scan', (e) => {
      this.emit('scan', e)
    })
66 67 68 69 70 71 72 73 74
    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)
    })
75 76 77 78 79 80 81 82 83 84

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

85
    return Promise.resolve()
86 87
  }

88 89
  quit()   { return this.puppet.quit() }

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
93 94 95 96
  ding()          {
    // TODO: test through the server & browser
    return 'dong'
  }
97 98
}

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

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

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