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 | let port
|
66 | if (process.env['WECHATY_HOSTIE_PORT']) {
|
67 | |
68 |
|
69 |
|
70 | log.warn('Wechaty', [
|
71 | '',
|
72 | 'WECHATY_HOSTIE_PORT is deprecated.',
|
73 | 'Use WECHATY_PUPPET_SERVER_PORT instead.',
|
74 | 'See: https://github.com/wechaty/wechaty/issues/2122',
|
75 | ].join(' '))
|
76 | port = parseInt(process.env['WECHATY_HOSTIE_PORT'])
|
77 | } else if (process.env['WECHATY_PUPPET_SERVER_PORT']) {
|
78 | port = parseInt(process.env['WECHATY_PUPPET_SERVER_PORT'])
|
79 | }
|
80 |
|
81 | const options: IoClientOptions = {
|
82 | token,
|
83 | wechaty,
|
84 | }
|
85 | if (port) {
|
86 | options.port = port
|
87 | }
|
88 |
|
89 | const client = new IoClient(options)
|
90 |
|
91 | client.start()
|
92 | .catch(onError.bind(client))
|
93 | }
|
94 |
|
95 | async function onError (
|
96 | this : IoClient,
|
97 | e : Error,
|
98 | ) {
|
99 | log.error('Client', 'start() fail: %s', e)
|
100 | await this.quit()
|
101 | process.exit(-1)
|
102 | }
|
103 |
|
104 | main()
|
105 | .catch(console.error)
|