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

26
import { FileBox }  from 'file-box'
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 75
function onScan (qrcode: string, status: ScanStatus) {
  generate(qrcode)
76

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
84 85 86
  console.info('%s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

`
185
console.info(welcome)