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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
11 12 13 14 15 16
const Puppet      = require('./puppet')
const PuppetWeb   = require('./puppet-web')

const Message     = require('./message')
const Contact     = require('./contact')
const Group       = require('./group')
17 18

class Wechaty extends EventEmitter {
Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
19
  constructor(options) {
20
    super()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
21 22
    options         = options || {}
    options.puppet  = options.puppet || 'web'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24
    switch (options.puppet) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
25
      case 'web':
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26
        this.puppet = new Puppet.Web({
27
          head: options.head
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
28 29
          , port: options.port
        })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
30 31
        break
      default:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
32
        throw new Error('Puppet unsupport(yet): ' + puppet)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
33 34
        break
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
35
    Contact.attach(this.puppet)
36
    Group.attach(this.puppet)
37 38 39 40 41 42 43 44 45 46 47 48

    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)
    })
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49 50 51
  init()          { return this.puppet.init() }
  currentUser()   { return this.puppet.currentUser() }
  send(message)   { return this.puppet.send(message) }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52
  quit()          { return this.puppet.quit() }
53

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
54 55 56 57
  ding()          {
    // TODO: test through the server & browser
    return 'dong'
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
58

Huan (李卓桓)'s avatar
fix  
Huan (李卓桓) 已提交
59
  getLoginQrImgUrl() { return this.puppet.getLoginQrImgUrl() }
60 61
}

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
64 65 66 67 68 69
Object.assign(Wechaty, {
  Puppet:     Puppet
  , Message:  Message
  , Contact:  Contact
  , Group:    Group
})
70 71

module.exports = Wechaty