wechaty.js 2.2 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 6
 * Class Wechaty
 * 
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'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
27 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 35 36 37 38
    this.initPuppet()
    this.initEventHook()
    return this.puppet.init()
  }
  initPuppet() {
    switch (this.options.puppet) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
39
      case 'web':
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
40
        this.puppet = new Puppet.Web({
41 42
          head:   this.options.head
          , port: this.options.port
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
43
        })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44 45
        break
      default:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
46
        throw new Error('Puppet unsupport(yet): ' + this.options.puppet)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
47
    }
48 49 50 51 52 53 54
    return Promise.resolve(this.puppet)
  }
  initEventHook() {
    // scan qrCode
    this.puppet.on('scan', (e) => {
      this.emit('scan', e)
    })
55 56 57 58 59 60 61 62 63
    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)
    })
64
    return Promise.resolve()
65 66
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
67
  currentUser()   { return this.puppet.currentUser() }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
68
  quit()          { return this.puppet.quit() }
Huan (李卓桓)'s avatar
v0.0.9  
Huan (李卓桓) 已提交
69 70
  
  send(message)   { return this.puppet.send(message) }
71
  reply(message, reply) { return this.puppet.reply(message, reply) }
72

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
73 74 75 76
  ding()          {
    // TODO: test through the server & browser
    return 'dong'
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
77

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

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

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
94 95 96
/**
 * Expose `Wechaty`.
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
97
module.exports = Wechaty.default = Wechaty.Wechaty = Wechaty