1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | require('dotenv').config()
|
24 |
|
25 | import {
|
26 | config,
|
27 | log,
|
28 | } from '../src/config'
|
29 |
|
30 | import {
|
31 | IoClient,
|
32 | IoClientOptions,
|
33 | } from '../src/io-client'
|
34 | import { Wechaty } from '../src/wechaty'
|
35 |
|
36 | const welcome = `
|
37 | | __ __ _ _
|
38 | | \\ \\ / /__ ___| |__ __ _| |_ _ _
|
39 | | \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|
40 | | \\ V V / __/ (__| | | | (_| | |_| |_| |
|
41 | | \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
|
42 | | |___/
|
43 |
|
44 | =============== Powered by Wechaty ===============
|
45 | -------- https://www.chatie.io --------
|
46 |
|
47 | My super power: download cloud bot from www.chatie.io
|
48 |
|
49 | __________________________________________________
|
50 |
|
51 | `
|
52 |
|
53 | async function main () {
|
54 | const token = config.token
|
55 |
|
56 | if (!token) {
|
57 | throw new Error('token not found: please set WECHATY_TOKEN in environment before run io-client')
|
58 | }
|
59 |
|
60 | console.info(welcome)
|
61 | log.info('Client', 'Starting for WECHATY_TOKEN: %s', token)
|
62 |
|
63 | const wechaty = new Wechaty({ name: token })
|
64 |
|
65 | const WECHATY_HOSTIE_PORT = 'WECHATY_HOSTIE_PORT'
|
66 | const port = parseInt(process.env[WECHATY_HOSTIE_PORT] || '0')
|
67 |
|
68 | const options: IoClientOptions = {
|
69 | token,
|
70 | wechaty,
|
71 | }
|
72 | if (port) {
|
73 | options.port = port
|
74 | }
|
75 |
|
76 | const client = new IoClient(options)
|
77 |
|
78 | client.start()
|
79 | .catch(onError.bind(client))
|
80 | }
|
81 |
|
82 | async function onError (
|
83 | this : IoClient,
|
84 | e : Error,
|
85 | ) {
|
86 | log.error('Client', 'start() fail: %s', e)
|
87 | await this.quit()
|
88 | process.exit(-1)
|
89 | }
|
90 |
|
91 | main()
|
92 | .catch(console.error)
|