ding-dong-bot.ts 4.7 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',
P
padlocal 已提交
37

38 39 40 41 42
  /**
   * You can specify different puppet for different IM protocols.
   * Learn more from https://wechaty.js.org/docs/puppet-providers/
   */
  // puppet: 'wechaty-puppet-whatsapp'
P
padlocal 已提交
43

44 45 46 47 48 49 50 51 52 53
  /**
   * You can use wechaty puppet provider 'wechaty-puppet-service'
   *   which can connect to Wechaty Puppet Services
   *   for using more powerful protocol.
   * Learn more about services (and TOKEN)from https://wechaty.js.org/docs/puppet-services/
   */
  // puppet: 'wechaty-puppet-service'
  // puppetOptions: {
  //   token: 'xxx',
  // }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
54
})
55

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
56 57 58 59 60 61
/**
 *
 * 2. Register event handlers for Bot
 *
 */
bot
62 63 64 65 66
  .on('logout', onLogout)
  .on('login',  onLogin)
  .on('scan',   onScan)
  .on('error',  onError)
  .on('message', onMessage)
67

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
68 69 70 71 72 73
/**
 *
 * 3. Start the bot!
 *
 */
bot.start()
74 75 76 77 78
  .catch(async e => {
    console.error('Bot start() fail:', e)
    await bot.stop()
    process.exit(-1)
  })
79

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
80 81 82 83 84
/**
 *
 * 4. You are all set. ;-]
 *
 */
85

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
86 87
/**
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
88
 * 5. Define Event Handler Functions for:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
89 90 91
 *  `scan`, `login`, `logout`, `error`, and `message`
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
92
function onScan (qrcode: string, status: ScanStatus) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
93 94
  if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
    generate(qrcode)
95

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
96
    const qrcodeImageUrl = [
97
      'https://wechaty.js.org/qrcode/',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
98 99
      encodeURIComponent(qrcode),
    ].join('')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
100

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
101 102 103 104
    console.info('onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)
  } else {
    console.info('onScan: %s(%s)', ScanStatus[status], status)
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
105 106

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
109
function onLogin (user: Contact) {
110
  console.info(`${user.name()} login`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
111
}
112

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
113
function onLogout (user: Contact) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
114
  console.info(`${user.name()} logged out`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
115
}
116

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
117 118
function onError (e: Error) {
  console.error('Bot error:', e)
119
  /*
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
120 121
  if (bot.logonoff()) {
    bot.say('Wechaty error: ' + e.message).catch(console.error)
122
  }
123
  */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
124
}
125

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
126 127
/**
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
128
 * 6. The most important handler is for:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
129 130 131 132
 *    dealing with Messages.
 *
 */
async function onMessage (msg: Message) {
133
  console.info(msg.toString())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
134

135 136 137 138 139
  if (msg.self()) {
    console.info('Message discarded because its outgoing')
    return
  }

140 141
  if (msg.age() > 2 * 60) {
    console.info('Message discarded because its TOO OLD(than 2 minutes)')
142 143
    return
  }
144

145 146
  if (msg.type() !== bot.Message.Type.Text
    || !/^(ding|ping|bing|code)$/i.test(msg.text())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
147
  ) {
148
    console.info('Message discarded because it does not match ding/ping/bing/code')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
149
    return
150 151
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
152
  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
153
   * 1. reply 'dong'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
154 155
   */
  await msg.say('dong')
156
  console.info('REPLY: dong')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
157 158

  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
159
   * 2. reply image(qrcode image)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
160
   */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
161
  const fileBox = FileBox.fromUrl('https://wechaty.github.io/wechaty/images/bot-qr-code.png')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
162 163

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

  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
167
   * 3. reply 'scan now!'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
168 169 170 171 172 173
   */
  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(''))
174 175
}

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
176 177
/**
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
178
 * 7. Output the Welcome Message
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
179 180
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
181 182 183 184 185 186 187
const welcome = `
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
|                                     |___/
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
188

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
189
=============== Powered by Wechaty ===============
190
-------- https://github.com/wechaty/wechaty --------
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
191
          Version: ${bot.version(true)}
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
192

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
193 194 195 196 197 198 199 200 201 202 203
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...

`
204
console.info(welcome)