wechaty.js 2.1 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 10
const log           = require('npmlog')
const EventEmitter  = require('events')
11

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

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

class Wechaty extends EventEmitter {
Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
20
  constructor(options) {
21
    super()
22 23
    this.options = options || {}
    this.options.puppet = this.options.puppet || 'web'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24 25
    
    this.VERSION = require('../package.json').version
26 27 28
  }
  toString() { return 'Class Wechaty(' + this.puppet + ')'}
  init() {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
29
    log.verbose('Wechaty', 'init() with version: %s', this.VERSION)
30 31 32 33 34 35
    this.initPuppet()
    this.initEventHook()
    return this.puppet.init()
  }
  initPuppet() {
    switch (this.options.puppet) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
36
      case 'web':
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
37
        this.puppet = new Puppet.Web({
38 39
          head:   this.options.head
          , port: this.options.port
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
40
        })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
41 42
        break
      default:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
43
        throw new Error('Puppet unsupport(yet): ' + this.options.puppet)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44
    }
45 46 47 48 49 50 51
    return Promise.resolve(this.puppet)
  }
  initEventHook() {
    // scan qrCode
    this.puppet.on('scan', (e) => {
      this.emit('scan', e)
    })
52 53 54 55 56 57 58 59 60
    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)
    })
61
    return Promise.resolve()
62 63
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
64
  currentUser()   { return this.puppet.currentUser() }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
65
  quit()          { return this.puppet.quit() }
66 67
  send(message)   { return this.puppet.say(message) }
  reply(message, reply) { return this.puppet.reply(message, reply) }
68

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69 70 71 72
  ding()          {
    // TODO: test through the server & browser
    return 'dong'
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
73

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
74 75 76 77
  /**
   * @deprecated
   * use on('scan', ({code, url}) => {}) instead.
   */
Huan (李卓桓)'s avatar
fix  
Huan (李卓桓) 已提交
78
  getLoginQrImgUrl() { return this.puppet.getLoginQrImgUrl() }
79 80
}

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
83 84 85 86 87 88
Object.assign(Wechaty, {
  Puppet:     Puppet
  , Message:  Message
  , Contact:  Contact
  , Group:    Group
})
89 90

module.exports = Wechaty