ding-dong-bot.ts 2.8 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2 3 4 5 6 7 8
/**
 *
 * Wechaty - Wechat for Bot
 *
 * Connecting ChatBots
 * https://github.com/wechaty/wechaty
 *
 */
9 10

/* tslint:disable:variable-name */
11
const QrcodeTerminal  = require('qrcode-terminal')
12
const finis           = require('finis')
13

14
import {
Huan (李卓桓)'s avatar
clean  
Huan (李卓桓) 已提交
15 16 17
  Config,
  Wechaty,
  log,
M
Mukaiu 已提交
18
  MediaMessage,
19
} from '../'
20 21

const welcome = `
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
22 23
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
24 25 26
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
27
|                                     |___/
28 29 30 31 32 33 34 35 36

=============== Powered by Wechaty ===============
-------- https://github.com/zixia/wechaty --------

I'm a bot, my super power is talk in Wechat.

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
37
Hope you like it, and you are very welcome to
38 39 40 41 42 43 44
upgrade me for more super powers!

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

`

console.log(welcome)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
45
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
46

47
bot
Huan (李卓桓)'s avatar
clean  
Huan (李卓桓) 已提交
48
.on('logout'	, user => log.info('Bot', `${user.name()} logouted`))
49 50 51 52
.on('login'	  , user => {
  log.info('Bot', `${user.name()} logined`)
  bot.say('Wechaty login')
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
53 54 55 56
.on('error'   , e => {
  log.info('Bot', 'error: %s', e)
  bot.say('Wechaty error: ' + e.message)
})
57
.on('scan', (url, code) => {
58
  if (!/201|200/.test(String(code))) {
59
    const loginUrl = url.replace(/\/qrcode\//, '/l/')
60
    QrcodeTerminal.generate(loginUrl)
61
  }
62
  console.log(`${url}\n[${code}] Scan QR Code in above url to login: `)
63
})
64
.on('message', m => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
65
  try {
66 67
    const room = m.room()
    console.log((room ? '[' + room.topic() + ']' : '')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
68
                + '<' + m.from().name() + '>'
L
lijiarui 已提交
69
                + ':' + m.toStringDigest(),
70 71
    )

72
    if (/^(ding|ping|bing|code)$/i.test(m.content()) && !m.self()) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
73 74
      m.say('dong')
      log.info('Bot', 'REPLY: dong')
75

76 77 78 79 80
      const joinWechaty =  `Join Wechaty Developers' Community\n\n` +
                            `Wechaty is used in many ChatBot projects by hundreds of developers.\n\n` +
                            `If you want to talk with other developers, just scan the following QR Code in WeChat with secret code: wechaty,\n\n` +
                            `you can join our Wechaty Developers' Home at once`
      m.say(joinWechaty)
M
Mukaiu 已提交
81
      m.say(new MediaMessage(__dirname + '/../image/BotQrcode.png'))
82
      m.say('Scan now, because other Wechaty developers want to talk with you too!\n\n (secret code: wechaty)')
83
      log.info('Bot', 'REPLY: Image')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
84
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
85 86 87
  } catch (e) {
    log.error('Bot', 'on(message) exception: %s' , e)
  }
88 89
})

90 91 92 93 94 95
bot.init()
.catch(e => {
  log.error('Bot', 'init() fail: %s', e)
  bot.quit()
  process.exit(-1)
})
96

Huan (李卓桓)'s avatar
clean  
Huan (李卓桓) 已提交
97 98
finis((code, signal) => {
  const exitMsg = `Wechaty exit ${code} because of ${signal} `
99 100
  console.log(exitMsg)
  bot.say(exitMsg)
101
})