ding-dong-bot.ts 3.8 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'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24
// import { PuppetPuppeteer }  from 'wechaty-puppet-puppeteer'
25

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

29
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
30 31 32
 *
 * 0. Declare your bot!
 *
33
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
34 35 36 37
const bot = new Wechaty({
  profile : 'padchat-demo',
  // puppet  : new PuppetPupeteer(),
})
38

39
const welcome = `
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
40 41
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
42 43 44
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
45
|                                     |___/
46 47

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
51
I'm a bot, my superpower is talk in Wechat.
52 53 54 55

If you send me a 'ding', I will reply you a 'dong'!
__________________________________________________

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
56
Hope you like it, and you are very welcome to
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57
upgrade me to more superpowers!
58 59 60 61 62 63

Please wait... I'm trying to login in...

`

console.log(welcome)
64

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
74 75 76
  const qrcodeImageUrl = [
    'https://api.qrserver.com/v1/create-qr-code/?data=',
    encodeURIComponent(qrcode),
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
77
    '&size=220x220&margin=20',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
78
  ].join('')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
79

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

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
88 89 90
function onLogout (user: Contact) {
  console.log(`${user.name()} logouted`)
}
91

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
92 93 94 95
function onError (e: Error) {
  console.error('Bot error:', e)
  if (bot.logonoff()) {
    bot.say('Wechaty error: ' + e.message).catch(console.error)
96
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
97
}
98

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
99 100 101 102 103 104 105 106 107 108 109
/**
 *
 * 2. The most important handler is for:
 *    dealing with Messages.
 *
 */
async function onMessage (msg: Message) {
  console.log(msg.toString())

  if (msg.age() > 60) {
    console.log('Message TOO OLD(than 1 minute), discarded.')
110 111
    return
  }
112

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
113 114 115
  if (!/^(ding|ping|bing|code)$/i.test(msg.text()) /*&& !msg.self()*/) {
    console.log('Message NOT MATCH, discarded.')
    return
116 117
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
118
  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
119
   * 2.1. reply 'dong'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
120 121 122 123 124
   */
  await msg.say('dong')
  console.log('REPLY: dong')

  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
125
   * 2.2. reply image(qrcode image)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
126 127 128 129 130 131 132
   */
  const fileBox = FileBox.fromUrl('https://chatie.io/wechaty/images/bot-qr-code.png')

  await msg.say(fileBox)
  console.log('REPLY: %s', fileBox.toString())

  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
133
   * 2.3. reply 'scan now!'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
134 135 136 137 138 139
   */
  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(''))
140 141
}

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
/**
 *
 * 3. Register all event handlers
 *    that we had previous defined.
 *
 */
bot
.on('logout', onLogout)
.on('login',  onLogin)
.on('scan',   onScan)
.on('error',  onError)
.on('message', onMessage)

/**
 *
 * 4. Start the bot!
 *
 */
bot.start()
.catch(async e => {
  console.error('Bot start() fail:', e)
  await bot.stop()
  process.exit(-1)
})

/**
 *
 * 5. You are all set. ;-]
 *
 */