ding-dong-bot.ts 4.3 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
/**
2
 *   Wechaty - https://github.com/chatie/wechaty
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
3
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
4
 *   @copyright 2016-2018 Huan LI <zixia@zixia.net>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
5
 *
6 7 8 9 10 11 12 13 14 15 16
 *   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.
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
17 18
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
19
import * as path      from 'path'
20 21

/* tslint:disable:variable-name */
22
const QrcodeTerminal  = require('qrcode-terminal')
23
const finis           = require('finis')
24

25 26 27 28 29
/**
 * Change `import { ... } from '../'`
 * to     `import { ... } from 'wechaty'`
 * when you are runing with Docker or NPM instead of Git Source.
 */
30
import {
Huan (李卓桓)'s avatar
clean  
Huan (李卓桓) 已提交
31 32
  Wechaty,
  log,
33
}               from '../src/'
34

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
35 36 37 38 39
const BOT_QR_CODE_IMAGE_FILE = path.join(
  __dirname,
  '../docs/images/bot-qr-code.png',
)

40
const welcome = `
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
41 42
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
43 44 45
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
46
|                                     |___/
47 48

=============== Powered by Wechaty ===============
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49
-------- https://github.com/chatie/wechaty --------
50

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
51
I'm a bot, my superpower is talk in Wechat.
52 53 54 55

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
56
Hope you like it, and you are very welcome to
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57
upgrade me to more superpowers!
58 59 60 61 62 63

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

`

console.log(welcome)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
64
const bot = Wechaty.instance()
65

66
bot
Huan (李卓桓)'s avatar
clean  
Huan (李卓桓) 已提交
67
.on('logout'	, user => log.info('Bot', `${user.name()} logouted`))
68
.on('login'	  , user => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69
  log.info('Bot', `${user.name()} login`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
70
  bot.say('Wechaty login').catch(console.error)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
71
})
72
.on('scan', (url, code) => {
73
  if (!/201|200/.test(String(code))) {
74
    const loginUrl = url.replace(/\/qrcode\//, '/l/')
75
    QrcodeTerminal.generate(loginUrl)
76
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
77
  console.log(`${url}\n[${code}] Scan QR Code above url to log in: `)
78
})
79
.on('message', async m => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
80
  try {
81
    const room = m.room()
82 83 84
    console.log(
      (room ? `${room}` : '')
      + `${m.from()}:${m}`,
85 86
    )

87
    if (/^(ding|ping|bing|code)$/i.test(m.content()) && !m.self()) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
88 89
      m.say('dong')
      log.info('Bot', 'REPLY: dong')
90

91 92 93 94
      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`
95
      await m.say(joinWechaty)
Huan (李卓桓)'s avatar
wip...  
Huan (李卓桓) 已提交
96
      await m.say(new bot.Message(BOT_QR_CODE_IMAGE_FILE))
97
      await m.say('Scan now, because other Wechaty developers want to talk with you too!\n\n(secret code: wechaty)')
98
      log.info('Bot', 'REPLY: Image')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
99
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
100 101 102
  } catch (e) {
    log.error('Bot', 'on(message) exception: %s' , e)
  }
103 104
})

105
bot.start()
106
.catch(e => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
107
  log.error('Bot', 'start() fail: %s', e)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
108
  bot.stop()
109 110
  process.exit(-1)
})
111

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
112 113
bot.on('error', async e => {
  log.error('Bot', 'error: %s', e)
114
  if (bot.logonoff()) {
115 116
    await bot.say('Wechaty error: ' + e.message).catch(console.error)
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
117
  // await bot.stop()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
118 119
})

120
let quiting = false
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
121
finis(async (code, signal) => {
122 123 124 125 126 127 128
  if (quiting) {
    log.warn('Bot', 'finis(%s, %s) called when quiting... just wait...', code, signal)
    return
  }
  quiting = true
  log.info('Bot', 'finis(%s, %s)', code, signal)
  const exitMsg = `Wechaty will exit ${code} because of ${signal} `
129
  if (bot.logonoff()) {
130
    log.info('Bot', 'finis() stoping bot')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
131
    await bot.say(exitMsg).catch(console.error)
132 133
  } else {
    log.info('Bot', 'finis() bot had been already stopped')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
134
  }
135 136 137 138 139 140 141 142 143 144 145 146
  setTimeout(async () => {
    log.info('Bot', 'finis() setTimeout() going to exit with %d', code)
    try {
      if (bot.logonoff()) {
        await bot.stop()
      }
    } catch (e) {
      log.error('Bot', 'finis() setTimeout() exception: %s', e)
    } finally {
      process.exit(code)
    }
  }, 3 * 1000)
147
})