self-testing-bot.ts 8.9 KB
Newer Older
ruiruibupt's avatar
ruiruibupt 已提交
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/**
 *   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.
 *
 */
// import * as fs    from 'fs'
import * as path  from 'path'

/* tslint:disable:variable-name */
import * as QrcodeTerminal  from 'qrcode-terminal'
// import finis                from 'finis'
import { FileBox }          from 'file-box'

/**
 * Change `import { ... } from '../'`
 * to     `import { ... } from 'wechaty'`
 * when you are runing with Docker or NPM instead of Git Source.
 */
import {
  Wechaty,
  log,
  // Contact,
  // Room,
}               from '../src/'

const BOT_QR_CODE_IMAGE_FILE = path.resolve(
  __dirname,
  '../docs/images/bot-qr-code.png',
)

44 45
const bot = Wechaty.instance()

ruiruibupt's avatar
ruiruibupt 已提交
46 47 48 49 50 51 52 53 54 55
const welcome = `
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
|                                     |___/

=============== Powered by Wechaty ===============
-------- https://github.com/chatie/wechaty --------
56
                Version: ${bot.version(true)}
ruiruibupt's avatar
ruiruibupt 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

I'm a bot, my superpower is talk in Wechat.

If you send me a 'ding', I will reply you a 'dong'!
__________________________________________________

Hope you like it, and you are very welcome to
upgrade me to more superpowers!

Please wait... I'm trying to login in...

`

console.log(welcome)

bot
.on('logout'	, user => log.info('Bot', `${user.name()} logouted`))
.on('login'	  , user => {
  log.info('Bot', `${user.name()} login`)
  bot.say('Wechaty login').catch(console.error)
})
78 79 80 81 82 83 84 85
.on('scan', (qrData, status, data) => {
  QrcodeTerminal.generate(qrData, { small: true }, (asciiArt: string) => {
    console.log(asciiArt)
    console.log(`[${status}] Scan QR Code above url to log in: `)
    if (data) {
      console.log(data)
    }
  })
ruiruibupt's avatar
ruiruibupt 已提交
86 87 88
})
.on('message', async msg => {
  try {
89
    console.log(msg.toString()) // on(message) exception: Error: no file
ruiruibupt's avatar
ruiruibupt 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    const text = msg.text()
    const from = msg.from()

    const nameList: string[] = []
    console.log('msg text:', text.substr(0, 20))

    // Room.findAll()
    if (/^testRoom$/.test(text)) {
      console.log('before findAll')
      const roomList = await bot.Room.findAll()
      await from.say('roomList: ' + (roomList && roomList.length))
      for (const room of roomList) {
        nameList.push(room.topic())
      }
      from.say(nameList.join() || 'not found')
      return
    }

    // Contact.findAll()
    if (/^testContact$/.test(text)) {
      const contactList = await bot.Contact.findAll()
      for (let i = 0; i < 5; i ++) {
        const contact = contactList[i]
        console.log(`begin to test num ${i} contact`)
        console.log(contact)
        nameList.push(contact.name())
      }
      from.say(nameList.join())
      return
    }

    if (/^fcontact$/.test(text)) {
      console.log('begin to check msg forward contact')
      // const contact = await bot.Contact.find({
      //   name: /李佳芮/,
      // })
      const contact = await bot.Contact.load('qq512436430')
      if (!contact) {
        console.error('contact not found')
        return
      }
      msg.forward(contact)
      return
    }

    if (/^froom$/.test(text)) {
      console.log('begin to check msg forward room')
      const room = bot.Room.load('6350854677@chatroom')
      msg.forward(room)
      return
    }

    if (/^geta$/.test(text)) {
      console.log('begin to check get contact alias')
      from.say(from.alias() || 'no alias')
      return
    }

    if (/^seta$/.test(text)) {
      console.log('begin to check set contact alias')
      await from.alias('wechaty-alias')
      setTimeout(() => {
        from.say(from.alias() || 'no alais')
      }, 3 * 1000)
      return
    }

    if (/^avatar$/.test(text)) {
      console.log('begin to check get contact avatar')
      const file = await from.avatar()
      from.say(file)
      return
    }

    if (/^delroom$/.test(text)) {
      console.log('begin to check roomDel')
      const room = bot.Room.load('6350854677@chatroom')
      const contact = bot.Contact.load('qq512436430')
      await room.del(contact)
      from.say('room.del: ' + room + ', ' + contact)
      return
    }

    if (/^addroom$/.test(text)) {
      console.log('begin to check roomAdd')
      const room = bot.Room.load('6350854677@chatroom')
      const contact = bot.Contact.load('qq512436430')
      await room.add(contact)
      from.say('room.add: ' + room + ', ' + contact)

      return
    }

    if (/^topicroom$/.test(text)) {
      console.log('begin to check roomTopic')
      const room = bot.Room.load('6350854677@chatroom')
      await room.topic('change-to-wechaty')
      from.say('room.topic: ' + room)

      return
    }

    if (/^quitroom$/.test(text)) {
      console.log('begin to check roomQuit')
      const room = bot.Room.load('6350854677@chatroom')
      await room.quit()

      from.say('room.quit: ' + room)

      return
    }

    if (/^(ding|ping|bing|code)$/i.test(msg.text()) /*&& !msg.self()*/) {
      /**
       * 1. reply 'dong'
       */
      log.info('Bot', 'REPLY: dong')
      msg.say('dong')

      const joinWechaty =  `Join Wechaty Developers' Community\n\n` +
                            `Wechaty is used in many ChatBot projects by hundreds of developers.\n\n` +
                            `If you want to talk with other developers, just scan the following QR Code in WeChat with secret code: wechaty,\n\n` +
                            `you can join our Wechaty Developers' Home at once`
      await msg.say(joinWechaty)

      /**
       * 2. reply qrcode image
       */
218
      const fileBox = FileBox.fromFile(BOT_QR_CODE_IMAGE_FILE)
ruiruibupt's avatar
ruiruibupt 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      // const fileBox = FileBox.packStream(
      //   fs.createReadStream(BOT_QR_CODE_IMAGE_FILE),
      //   BOT_QR_CODE_IMAGE_FILE,
      // )

      log.info('Bot', 'REPLY: %s', fileBox)
      await msg.say(fileBox)

      /**
       * 3. reply 'scan now!'
       */
      await msg.say('Scan now, because other Wechaty developers want to talk with you too!\n\n(secret code: wechaty)')

    }
  } catch (e) {
    log.error('Bot', 'on(message) exception: %s' , e)
    console.error(e)
  }
})

bot.on('error', async e => {
  log.error('Bot', 'error: %s', e)
  if (bot.logonoff()) {
    await bot.say('Wechaty error: ' + e.message).catch(console.error)
  }
  // await bot.stop()
})

// let killChrome: NodeJS.SignalsListener

bot.start()
.then(() => {
  const listenerList = process.listeners('SIGINT')
  for (const listener of listenerList) {
    if (listener.name === 'killChrome') {
      process.removeListener('SIGINT', listener)
      // killChrome = listener
    }
  }
})
.catch(e => {
  log.error('Bot', 'start() fail: %s', e)
  bot.stop()
  process.exit(-1)
})

// let quiting = false
// finis((code, signal) => {
//   log.info('Bot', 'finis(%s, %s)', code, signal)

//   if (!bot.logonoff()) {
//     log.info('Bot', 'finis() bot had been already stopped')
//     doExit(code)
//   }

//   if (quiting) {
//     log.warn('Bot', 'finis() already quiting... return and wait...')
//     return
//   }

//   quiting = true
//   let done = false
//   // let checkNum = 0

//   const exitMsg = `Wechaty will exit ${code} because of ${signal} `

//   log.info('Bot', 'finis() broadcast quiting message for bot')
//   bot.say(exitMsg)
//       // .then(() => bot.stop())
//       .catch(e => log.error('Bot', 'finis() catch rejection: %s', e))
//       .then(() => done = true)

//   setImmediate(checkForExit)

//   function checkForExit() {
//     // if (checkNum++ % 100 === 0) {
//     log.info('Bot', 'finis() checkForExit() checking done: %s', done)
//     // }
//     if (done) {
//       log.info('Bot', 'finis() checkForExit() done!')
//       setTimeout(() => doExit(code), 1000)  // delay 1 second
//       return
//     }
//     // death loop to wait for `done`
//     // process.nextTick(checkForExit)
//     // setImmediate(checkForExit)
//     setTimeout(checkForExit, 100)
//   }
// })

// function doExit(code: number): void {
//   log.info('Bot', 'doExit(%d)', code)
//   if (killChrome) {
//     killChrome('SIGINT')
//   }
//   process.exit(code)
// }

// process.on('SIGINT', function() {
//   console.log('Nice SIGINT-handler')
//   const listeners = process.listeners('SIGINT')
//   for (let i = 0; i < listeners.length; i++) {
//       console.log(listeners[i].toString())
//   }
// })