ding-dong-bot.ts 4.2 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
/**
2
 *   Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
3
 *
4 5
 *   @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
 *                   Wechaty Contributors <https://github.com/wechaty>.
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6
 *
7 8 9 10 11 12 13 14 15 16 17
 *   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 (李卓桓) 已提交
18 19
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
20 21
import {
  Contact,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
22
  FileBox,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23
  Message,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24
  ScanStatus,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
25
  Wechaty,
26
}               from '../src/mod' // from 'wechaty'
27

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
28
import { generate } from 'qrcode-terminal'
29

30
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
31
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
32
 * 1. Declare your Bot!
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
33
 *
34
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
35
const bot = new Wechaty({
36
  name : 'ding-dong-bot',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
37
})
38

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
39 40 41 42 43 44
/**
 *
 * 2. Register event handlers for Bot
 *
 */
bot
45 46 47 48 49
  .on('logout', onLogout)
  .on('login',  onLogin)
  .on('scan',   onScan)
  .on('error',  onError)
  .on('message', onMessage)
50

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
51 52 53 54 55 56
/**
 *
 * 3. Start the bot!
 *
 */
bot.start()
57 58 59 60 61
  .catch(async e => {
    console.error('Bot start() fail:', e)
    await bot.stop()
    process.exit(-1)
  })
62

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
63 64 65 66 67
/**
 *
 * 4. You are all set. ;-]
 *
 */
68

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69 70
/**
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
71
 * 5. Define Event Handler Functions for:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
72 73 74
 *  `scan`, `login`, `logout`, `error`, and `message`
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
75
function onScan (qrcode: string, status: ScanStatus) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
76 77
  if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
    generate(qrcode)
78

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
79
    const qrcodeImageUrl = [
80
      'https://wechaty.js.org/qrcode/',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
81 82
      encodeURIComponent(qrcode),
    ].join('')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
83

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
84 85 86 87
    console.info('onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)
  } else {
    console.info('onScan: %s(%s)', ScanStatus[status], status)
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
88 89

  // console.info(`[${ScanStatus[status]}(${status})] ${qrcodeImageUrl}\nScan QR Code above to log in: `)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
90
}
91

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
92
function onLogin (user: Contact) {
93
  console.info(`${user.name()} login`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
94
}
95

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
96
function onLogout (user: Contact) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
97
  console.info(`${user.name()} logged out`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
98
}
99

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
100 101
function onError (e: Error) {
  console.error('Bot error:', e)
102
  /*
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
103 104
  if (bot.logonoff()) {
    bot.say('Wechaty error: ' + e.message).catch(console.error)
105
  }
106
  */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
107
}
108

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
109 110
/**
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
111
 * 6. The most important handler is for:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
112 113 114 115
 *    dealing with Messages.
 *
 */
async function onMessage (msg: Message) {
116
  console.info(msg.toString())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
117

118 119 120 121 122
  if (msg.self()) {
    console.info('Message discarded because its outgoing')
    return
  }

123 124
  if (msg.age() > 2 * 60) {
    console.info('Message discarded because its TOO OLD(than 2 minutes)')
125 126
    return
  }
127

128 129
  if (msg.type() !== bot.Message.Type.Text
    || !/^(ding|ping|bing|code)$/i.test(msg.text())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
130
  ) {
131
    console.info('Message discarded because it does not match ding/ping/bing/code')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
132
    return
133 134
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
135
  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
136
   * 1. reply 'dong'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
137 138
   */
  await msg.say('dong')
139
  console.info('REPLY: dong')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
140 141

  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
142
   * 2. reply image(qrcode image)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
143
   */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
144
  const fileBox = FileBox.fromUrl('https://wechaty.github.io/wechaty/images/bot-qr-code.png')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
145 146

  await msg.say(fileBox)
147
  console.info('REPLY: %s', fileBox.toString())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
148 149

  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
150
   * 3. reply 'scan now!'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
151 152 153 154 155 156
   */
  await msg.say([
    'Join Wechaty Developers Community\n\n',
    'Scan now, because other Wechaty developers want to talk with you too!\n\n',
    '(secret code: wechaty)',
  ].join(''))
157 158
}

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
159 160
/**
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
161
 * 7. Output the Welcome Message
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
162 163
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
164 165 166 167 168 169 170
const welcome = `
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
|                                     |___/
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
171

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
172
=============== Powered by Wechaty ===============
173
-------- https://github.com/wechaty/wechaty --------
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
174
          Version: ${bot.version(true)}
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
175

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
176 177 178 179 180 181 182 183 184 185 186
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...

`
187
console.info(welcome)