contact-self.ts 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 *   Wechaty - https://github.com/chatie/wechaty
 *
 *   @copyright 2016-2018 Huan LI <zixia@zixia.net>
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *   @ignore
 */
import { FileBox } from 'file-box'

import {
  log,
}           from '../config'

import {
  Contact,
}           from './contact'

30 31 32 33 34 35 36 37 38 39 40
/**
 * Bot itself will be encapsulated as a ContactSelf.
 *
 * > Tips: this class is extends Contact
 * @example
 * const bot = new Wechaty()
 * await bot.start()
 * bot.on('login', (user: ContactSelf) => {
 *   console.log(`user ${user} login`)
 * })
 */
41 42 43 44 45 46 47 48 49 50
export class ContactSelf extends Contact {
  constructor (
    id: string,
  ) {
    super(id)
  }

  public async avatar ()              : Promise<FileBox>
  public async avatar (file: FileBox) : Promise<void>

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  /**
   * GET / SET bot avatar
   *
   * @param {FileBox} [file]
   * @returns {(Promise<void | FileBox>)}
   *
   * @example <caption> GET the avatar for bot, return {Promise<FileBox>}</caption>
   * // Save avatar to local file like `1-name.jpg`
   *
   * bot.on('login', (user: ContactSelf) => {
   *   console.log(`user ${user} login`)
   *   const file = await user.avatar()
   *   const name = file.name
   *   await file.toFile(name, true)
   *   console.log(`Save bot avatar: ${contact.name()} with avatar file: ${name}`)
   * })
   *
   * @example <caption>SET the avatar for a bot</caption>
   * import { FileBox }  from 'file-box'
   * bot.on('login', (user: ContactSelf) => {
   *   console.log(`user ${user} login`)
   *   const fileBox = FileBox.fromUrl('https://chatie.io/wechaty/images/bot-qr-code.png')
   *   await user.avatar(fileBox)
   *   console.log(`Change bot avatar successfully!`)
   * })
   *
   */
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  public async avatar (file?: FileBox): Promise<void | FileBox> {
    log.verbose('Contact', 'avatar(%s)', file ? file.name : '')

    if (!file) {
      const filebox = await super.avatar()
      return filebox
    }

    if (this.id !== this.puppet.selfId()) {
      throw new Error('set avatar only available for user self')
    }

    await this.puppet.contactAvatar(this.id, file)
  }

93 94 95 96 97 98 99 100 101 102 103 104 105 106
  /**
   * Get bot qrcode
   *
   * @returns {Promise<string>}
   *
   * @example
   * import { generate } from 'qrcode-terminal'
   * bot.on('login', (user: ContactSelf) => {
   *   console.log(`user ${user} login`)
   *   const qrcode = await user.qrcode()
   *   console.log(`Following is the bot qrcode!`)
   *   generate(qrcode, { small: true })
   * })
   */
107 108 109 110 111 112 113
  public async qrcode (): Promise<string> {
    log.verbose('Contact', 'qrcode()')

    if (this.id !== this.puppet.selfId()) {
      throw new Error('only can get qrcode for the login userself')
    }

114 115
    const qrcodeValue = await this.puppet.contactSelfQrcode()
    return qrcodeValue
116 117
  }

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  /**
   * Change bot name
   *
   * @param name The new name that the bot will change to
   *
   * @example
   * bot.on('login', async user => {
   *   console.log(`user ${user} login`)
   *   const oldName = user.name()
   *   try {
   *     await user.setName(`${oldName}-${new Date().getTime()}`)
   *   } catch (e) {
   *     console.error('change name failed', e)
   *   }
   * })
   */
  public name (): string
  public name (name: string): Promise<void>

  public name (name?: string): string | Promise<void> {
    log.verbose('ContactSelf', 'name(%s)', name)

    if (typeof name === 'undefined') {
      return super.name()
    }

    if (this.id !== this.puppet.selfId()) {
      throw new Error('only can set name for user self')
    }

    return this.puppet.contactSelfName(name)
  }

  /**
   * Change bot signature
   *
   * @param signature The new signature that the bot will change to
   *
   * @example
   * bot.on('login', async user => {
   *   console.log(`user ${user} login`)
   *   try {
   *     await user.signature(`Signature changed by wechaty on ${new Date()}`)
   *   } catch (e) {
   *     console.error('change signature failed', e)
   *   }
   * })
   */
  public async signature (signature: string): Promise<void> {
    log.verbose('ContactSelf', 'signature()')

    if (this.id !== this.puppet.selfId()) {
      throw new Error('only can change signature for user self')
    }

    return this.puppet.contactSelfSignature(signature)
  }
175
}