io-client.ts 1.9 KB
Newer Older
1
#!/usr/bin/env node
2
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
3
 *   Wechaty - https://github.com/chatie/wechaty
4
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
5
 *   @copyright 2016-2018 Huan LI <zixia@zixia.net>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6 7 8 9 10 11 12 13 14 15 16 17
 *
 *   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.
18 19
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
20

21
import {
22
  config,
L
lijiarui 已提交
23
  log,
24
}               from '../src/config'
25

26
import IoClient from '../src/io-client'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
27

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

=============== Powered by Wechaty ===============
37
       -------- https://www.chatie.io --------
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
38

39
My super power: download cloud bot from www.chatie.io
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
40 41 42 43 44

__________________________________________________

`

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
45
let token = config.token
46

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
47
if (!token) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
48
  log.error('Client', 'token not found: please set WECHATY_TOKEN in environment before run io-client')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49
  // process.exit(-1)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
50
  token = config.default.DEFAULT_TOKEN
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
51
  log.warn('Client', `set token to "${token}" for demo purpose`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52 53
}

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
54
console.log(welcome)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
55
log.info('Client', 'Starting for WECHATY_TOKEN: %s', token)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
56

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57 58 59
const client = new IoClient({
  token,
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
60

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
61 62
client.init()
    .catch(onError.bind(client))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
63

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
64 65
client.initWeb()
    .catch(onError.bind(client))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
66

67
function onError(e: Error) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
68
  log.error('Client', 'initWeb() fail: %s', e)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69 70 71
  this.quit()
  process.exit(-1)
}