ding-dong-bot.ts 4.2 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
/**
2
 *   Wechaty - https://github.com/wechaty/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 20
import {
  Contact,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
21
  FileBox,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
22
  Message,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23
  ScanStatus,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24
  Wechaty,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
25
}               from '../src/' // from 'wechaty'
26

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

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

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

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

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

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
78 79 80 81 82 83
    // Generate a QR Code online via
    // http://goqr.me/api/doc/create-qr-code/
    const qrcodeImageUrl = [
      'https://api.qrserver.com/v1/create-qr-code/?data=',
      encodeURIComponent(qrcode),
    ].join('')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
84

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

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
93
function onLogin (user: Contact) {
94
  console.info(`${user.name()} login`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
95 96
  bot.say('Wechaty login').catch(console.error)
}
97

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
98
function onLogout (user: Contact) {
99
  console.info(`${user.name()} logouted`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
100
}
101

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

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

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

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

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

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

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

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

  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
152
   * 3. reply 'scan now!'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
153 154 155 156 157 158
   */
  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(''))
159 160
}

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

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

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

`
189
console.info(welcome)