UNPKG

2.78 kBPlain TextView Raw
1#!/usr/bin/env node
2/**
3 * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
4 *
5 * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
6 * Wechaty Contributors <https://github.com/wechaty>.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22/* eslint-disable import/first */
23require('dotenv').config()
24
25import {
26 config,
27 log,
28} from '../src/config'
29
30import {
31 IoClient,
32 IoClientOptions,
33} from '../src/io-client'
34import { Wechaty } from '../src/wechaty'
35
36const welcome = `
37| __ __ _ _
38| \\ \\ / /__ ___| |__ __ _| |_ _ _
39| \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
40| \\ V V / __/ (__| | | | (_| | |_| |_| |
41| \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
42| |___/
43
44=============== Powered by Wechaty ===============
45 -------- https://www.chatie.io --------
46
47My super power: download cloud bot from www.chatie.io
48
49__________________________________________________
50
51`
52
53async 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 * https://github.com/wechaty/wechaty/issues/2122
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
95async 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
104main()
105 .catch(console.error)