puppet.ts 2.7 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
2
 * Wechat for Bot. Connecting ChatBots
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
3
 *
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>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
68 69 70 71
  /**
   * @deprecated
   * use Message.self() instead
   */
72
  public abstract self(message?: Message): boolean
73

74 75 76 77 78 79
  // public user(contact?: Contact) {
  //   if (contact) {
  //     this._user = contact
  //   }
  //   return this._user
  // }
80

81 82
  public abstract send(message: Message): Promise<void>
  public abstract say(content: string): Promise<void>
83 84

  // @deprecated
85 86
  public abstract reply(message: Message, reply): Promise<void>

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
87
  public abstract reset(reason?: string): void
88 89
  public abstract logout(): Promise<void>
  public abstract quit(): Promise<void>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
90

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
91
  public abstract ding(): Promise<string>
92

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
93 94 95
  /**
   * FriendRequest
   */
96 97 98
  public abstract friendRequestSend(contact: Contact, hello?: string): Promise<any>
  public abstract friendRequestAccept(contact: Contact, ticket: string): Promise<any>

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
99 100 101
  /**
   * Room
   */
102 103 104 105
  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 (李卓桓) 已提交
106
  public abstract roomFind(filterFunc: string): Promise<Room[]>
107

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
108 109 110
  /**
   * Contact
   */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
111
  public abstract contactFind(filterFunc: string): Promise<Contact[]>
112 113
}

114
export default Puppet