puppet.ts 2.4 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
2 3
 * Wechat for Bot. and for human who can talk with bot/robot
 *
4
 * Interface for Puppet
5
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6
 * Class Puppet
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
7 8
 *
 * Licenst: ISC
9
 * https://github.com/wechaty/wechaty
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
10 11
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
12

13
import { EventEmitter } from 'events'
14

15 16 17 18
import Contact  from './contact'
import Message  from './message'
import Room     from './room'
import log      from './brolog-env'
19

20 21 22 23 24
type ContactGetterFunc = {
  (id: string): Promise<any>
}

abstract class Puppet extends EventEmitter {
25 26 27
  public user: Contact
  public abstract getContact(id: string): Promise<any>

28
  protected userId: string
29 30 31 32

  private _targetState:   string
  private _currentState:  string

33 34
  constructor() {
    super()
35 36

    /*
37
     * @deprecated
38 39 40
     * connected / disconnected
     * connecting / disconnecting
     */
41 42 43 44 45 46 47
    // this._readyState = 'disconnected'

    this.targetState('dead')
    this.currentState('dead')
  }

  // targetState : 'live' | 'dead'
48
  public targetState(newState?) {
49 50 51 52 53
    if (newState) {
      log.verbose('Puppet', 'targetState(%s)', newState)
      this._targetState = newState
    }
    return this._targetState
54 55
  }

56
  // currentState : 'birthing' | 'killing'
57
  public currentState(newState?) {
58
    if (newState) {
59 60
      log.verbose('Puppet', 'currentState(%s)', newState)
      this._currentState = newState
61
    }
62
    return this._currentState
63 64
  }

65
  public abstract self(message?: Message): boolean | Contact
66

67 68 69 70 71 72
  // public user(contact?: Contact) {
  //   if (contact) {
  //     this._user = contact
  //   }
  //   return this._user
  // }
73

74 75
  public abstract send(message: Message): Promise<any>
  public abstract reply(message: Message, reply): Promise<any>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
76

77 78 79 80
  public abstract reset(reason?: string)
  public abstract logout(): Promise<any>
  public abstract quit(): Promise<any>
  public abstract ding(data?: string): Promise<any>
81 82 83 84 85 86 87 88 89 90 91

  public abstract friendRequestSend(contact: Contact, hello?: string): Promise<any>
  public abstract friendRequestAccept(contact: Contact, ticket: string): Promise<any>

  public abstract roomAdd(room: Room, contact: Contact): Promise<number>
  public abstract roomDel(room: Room, contact: Contact): Promise<number>
  public abstract roomTopic(room: Room, topic: string): Promise<string>
  public abstract roomCreate(contactList: Contact[], topic?: string): Promise<Room>
  public abstract roomFind(filter: Function): Promise<Room[]>

  public abstract contactFind(filter: Function): Promise<Contact[]>
92 93
}

94
export default Puppet
95 96 97 98 99 100
export {
    Contact
  , Message
  , Puppet
  , Room
}