io-bot.js 1.2 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2
#!/usr/bin/env node

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
3
const { IoBot, log } = require('..')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
4

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
const welcome = `
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
|                                     |___/

=============== Powered by Wechaty ===============
       -------- https://wechaty.io --------

I'm a bot, my super power is download cloud bot from wechaty.io

__________________________________________________


`

23
const profile = process.env.WECHATY_PROFILE || 'io-bot'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24
let token = process.env.WECHATY_TOKEN
25

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26 27 28 29
if (!token) {
  log.error('Bot', 'token not found: please set WECHATY_TOKEN in environment before run io-bot')
  // process.exit(-1)
  token = 'DEMO'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
30
  log.warn('Bot', `set token to "${token}" for demo purpose`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
31 32 33 34 35
} else {
  console.log(welcome)
  log.info('Bot', 'Starting for WECHATY_TOKEN: %s', token)
}

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
36
const ioBot = new IoBot({
37
  profile
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
38 39
  , token
  , log
Huan (李卓桓)'s avatar
v0.2.0  
Huan (李卓桓) 已提交
40
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
41

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
42
ioBot.init()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
43
    .catch(onError.bind(ioBot))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44 45

ioBot.initWeb()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
46 47 48 49 50 51 52
    .catch(onError.bind(ioBot))

function onError(e) {
  log.error('Bot', 'initWeb() fail: %s', e)
  this.quit()
  process.exit(-1)
}
53