UNPKG

2.39 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 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
82async 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
91main()
92 .catch(console.error)