puppet.ts 2.7 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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
15 16 17
import {
  Sayable
}               from './config'
18 19 20 21
import Contact  from './contact'
import Message  from './message'
import Room     from './room'
import log      from './brolog-env'
22

23 24 25
// type ContactGetterFunc = {
//   (id: string): Promise<any>
// }
26

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
27
export abstract class Puppet extends EventEmitter implements Sayable {
28 29
  public userId:  string  | null
  public user:    Contact | null
30 31
  public abstract getContact(id: string): Promise<any>

32 33 34
  private _targetState:   string
  private _currentState:  string

35 36
  constructor() {
    super()
37 38

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

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

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

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
67
  public abstract async init(): Promise<this>
68
  public abstract self(message?: Message): boolean | Contact | null
69

70 71 72 73 74 75
  // public user(contact?: Contact) {
  //   if (contact) {
  //     this._user = contact
  //   }
  //   return this._user
  // }
76

77
  public abstract send(message: Message): Promise<any>
78 79 80
  public abstract say(content: string)

  // @deprecated
81
  public abstract reply(message: Message, reply): Promise<any>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
82

83 84 85
  public abstract reset(reason?: string)
  public abstract logout(): Promise<any>
  public abstract quit(): Promise<any>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
86
  public abstract ding(): Promise<string>
87

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
88 89 90
  /**
   * FriendRequest
   */
91 92 93
  public abstract friendRequestSend(contact: Contact, hello?: string): Promise<any>
  public abstract friendRequestAccept(contact: Contact, ticket: string): Promise<any>

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
94 95 96
  /**
   * Room
   */
97 98 99 100
  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>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
101
  public abstract roomFind(filterFunc: string): Promise<Room[]>
102

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
103 104 105
  /**
   * Contact
   */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
106
  public abstract contactFind(filterFunc: string): Promise<Contact[]>
107 108
}

109
export default Puppet