ding-dong-bot.ts 3.9 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 20 21 22
import {
  Contact,
  Message,
  Wechaty,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23
}           from '../src/' // from 'wechaty'
24

25
import { FileBox }  from 'file-box'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26
import { generate } from 'qrcode-terminal'
27

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

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

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

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
67 68
/**
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69
 * 5. Define Event Handler Functions for:
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
70 71 72 73
 *  `scan`, `login`, `logout`, `error`, and `message`
 *
 */
function onScan (qrcode: string, status: number) {
74
  generate(qrcode, { small: true })
75

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

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
86
function onLogin (user: Contact) {
87
  console.info(`${user.name()} login`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
88 89
  bot.say('Wechaty login').catch(console.error)
}
90

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
95 96
function onError (e: Error) {
  console.error('Bot error:', e)
97
  /*
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
98 99
  if (bot.logonoff()) {
    bot.say('Wechaty error: ' + e.message).catch(console.error)
100
  }
101
  */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
102
}
103

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

  if (msg.age() > 60) {
114
    console.info('Message discarded because its TOO OLD(than 1 minute)')
115 116
    return
  }
117

118 119 120
  if (msg.type() !== bot.Message.Type.Text
    || !/^(ding|ping|bing|code)$/i.test(msg.text())
    /* && !msg.self() */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
121
  ) {
122
    console.info('Message discarded because it does not match ding/ping/bing/code')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
123
    return
124 125
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
126
  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
127
   * 1. reply 'dong'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
128 129
   */
  await msg.say('dong')
130
  console.info('REPLY: dong')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
131 132

  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
133
   * 2. reply image(qrcode image)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
134 135 136 137
   */
  const fileBox = FileBox.fromUrl('https://chatie.io/wechaty/images/bot-qr-code.png')

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

  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
141
   * 3. reply 'scan now!'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
142 143 144 145 146 147
   */
  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(''))
148 149
}

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
150 151
/**
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
152
 * 7. Output the Welcome Message
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
153 154
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
155 156 157 158 159 160 161
const welcome = `
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
|                                     |___/
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
162

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
163 164 165
=============== Powered by Wechaty ===============
-------- https://github.com/chatie/wechaty --------
          Version: ${bot.version(true)}
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
166

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
167 168 169 170 171 172 173 174 175 176 177
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...

`
178
console.info(welcome)