wechaty.js 1.4 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
bug fix  
Huan (李卓桓) 已提交
26
        this.puppet = new Puppet.Web({port: options.port})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
27 28 29 30 31
        break
      default:
        throw new Error('Puppet unknown: ' + puppet)
        break
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
32
    Contact.attach(this.puppet)
33
    Group.attach(this.puppet)
34 35 36 37 38 39 40 41 42 43 44 45

    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 (李卓桓) 已提交
46 47 48
  init()          { return this.puppet.init() }
  currentUser()   { return this.puppet.currentUser() }
  send(message)   { return this.puppet.send(message) }
49

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
50
  ding()          { return 'dong' }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
51 52

  getLoginQrImgUrl() { return puppet.getLoginQrImgUrl() }
53 54
}

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57 58 59 60 61 62
Object.assign(Wechaty, {
  Puppet:     Puppet
  , Message:  Message
  , Contact:  Contact
  , Group:    Group
})
63 64

module.exports = Wechaty