@juzi/wechaty
Version:
Wechaty is a RPA SDK for Chatbot Makers.
154 lines (147 loc) • 7.36 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.gateway = void 0;
/* eslint-disable sort-keys */
const cmd_ts_1 = require("cmd-ts");
const wechaty_puppet_1 = require("@juzi/wechaty-puppet");
const wechaty_token_1 = require("wechaty-token");
const get_port_1 = __importDefault(require("get-port"));
const config_js_1 = require("../config.js");
const io_client_js_1 = require("../io-client.js");
const wechaty_builder_js_1 = require("../wechaty-builder.js");
const puppet_config_js_1 = require("../puppet-config.js");
async function onError(e) {
wechaty_puppet_1.log.error('Gateway', 'Error: %s', e);
await this.quit();
process.exit(-1);
}
const gateway = (0, cmd_ts_1.command)({
name: 'gateway',
description: 'Publish your DIY token as a Wechaty Puppet Service. (you can generate a token by running `wechaty token generate`)',
args: {
token: (0, cmd_ts_1.option)({
description: 'Specify a TOKEN for your DIY Wechaty Puppet Service (WPS). It is the standard token service for TypeScript and Polyglot Wechaty (like Python, Rust, Go, etc.). If it not specified, it will be generated automatically.',
long: 'token',
short: 't',
type: (0, cmd_ts_1.optional)(cmd_ts_1.string),
}),
port: (0, cmd_ts_1.option)({
description: 'Specify a Port for your DIY Wechaty Puppet Service (WPS). If it not specified, it will select a free port automatically. This port MUST be public accessiable via the internet.',
long: 'port',
short: 'p',
type: (0, cmd_ts_1.optional)(cmd_ts_1.number),
}),
puppet: (0, cmd_ts_1.option)({
description: 'Specify a Wechaty Puppet Provider (WPP). If it not specified, it will use the environment variable `WECHATY_PUPPET`.',
long: 'puppet',
short: 'P',
type: (0, cmd_ts_1.optional)(cmd_ts_1.string),
}),
puppetToken: (0, cmd_ts_1.option)({
description: 'Specify the WEchaty Puppet Provider TOKEN. If it not specified, it will use the environment variable `WECHATY_PUPPET_XXX_TOKEN` (`XXX` is your WPP name).',
long: 'puppet-token',
short: 'T',
type: (0, cmd_ts_1.optional)(cmd_ts_1.string),
}),
},
handler: async (args) => {
wechaty_puppet_1.log.info('Gateway', 'Wechaty version %s', config_js_1.VERSION);
let { token, port, puppet, puppetToken, } = args;
/**
* Token
*/
if (!token) {
wechaty_puppet_1.log.info('Gateway', 'no `--token` specified, generating random Wechaty Puppet Service TOKEN ...');
token = wechaty_token_1.WechatyToken.generate('insecure');
}
wechaty_puppet_1.log.info('Gateway', 'Wechaty Puppet Service TOKEN: %s', token);
/**
* Puppet Service Server Port
*/
if (!port) {
wechaty_puppet_1.log.info('Gateway', 'no `--port` specified, selecting random free Wechaty Puppet Service Server Port ...');
port = await (0, get_port_1.default)({
port: 8788,
});
}
wechaty_puppet_1.log.info('Gateway', 'Wechaty Puppet Service Server Port: %d', port);
/**
* Puppet Provider
*/
if (!puppet) {
wechaty_puppet_1.log.info('Gateway', 'no `--puppet` specified, reading from environment variable `WECHATY_PUPPET` for Wechaty Puppet Provider ...');
puppet = process.env['WECHATY_PUPPET'];
if (!puppet) {
wechaty_puppet_1.log.info('Gateway', `environment variable "WECHATY_PUPPET" is not available. Using system default puppet "${puppet_config_js_1.OFFICIAL_PUPPET_DEFAULT}"`);
puppet = puppet_config_js_1.OFFICIAL_PUPPET_DEFAULT;
}
}
if (!(0, puppet_config_js_1.isPuppetModuleName)(puppet)) {
console.error(`The puppet name "${puppet}" is not a valid puppet name.`);
console.error(`Valid puppet names are: ${Object.keys(puppet_config_js_1.OFFICIAL_PUPPET_DEPENDENCIES).join(', ')}`);
return 1;
}
wechaty_puppet_1.log.info('Gateway', 'Wechaty Puppet Provider: %s', puppet);
/**
* Puppet Token
*/
if (!puppetToken) {
wechaty_puppet_1.log.info('Gateway', 'no `--puppet-token` specified, reading from environment variable `WECHATY_PUPPET_TOKEN` for your Wechaty Puppet Provider ...');
puppetToken = process.env['WECHATY_PUPPET_TOKEN'];
if (!puppetToken) {
wechaty_puppet_1.log.info('Gateway', 'WECHATY_PUPPET_TOKEN environment variable not available. Token disabled.');
}
}
wechaty_puppet_1.log.info('Gateway', 'Wechaty Puppet Provider Token: %s', puppetToken ? `"${puppetToken}"` : 'disabled');
const wechaty = wechaty_builder_js_1.WechatyBuilder.build({
name: 'Gateway',
puppet,
puppetOptions: {
token: puppetToken,
},
});
const options = {
port,
token,
wechaty,
};
const client = new io_client_js_1.IoClient(options);
client.start()
.catch(onError.bind(client));
const wechatyToken = new wechaty_token_1.WechatyToken(token);
let address;
while (true) {
wechaty_puppet_1.log.info('Gateway', 'Registering Wechaty Puppet Token Gateway Service Discovery ...');
await new Promise(resolve => setTimeout(resolve, 1000));
address = await wechatyToken.discover();
if (address) {
wechaty_puppet_1.log.info('Gateway', 'Registering Wechaty Puppet Token Gateway Service Discovery ... success');
break;
}
wechaty_puppet_1.log.info('Gateway', 'Registering Wechaty Puppet Token Gateway Service Discovery ... timeout');
}
console.info(`
Your have successfully ran Wechaty Token Gateway with the below settings:
1. token: ${token}
2. port: ${port}
3. puppet: ${puppet}
4. puppetToken: ${puppetToken || ''}
The Wechaty Puppet Token Gateway Service Address is ${address.host}:${address.port}, please make sure it can be public accessed via the internet.
You can save the below command to restore the token gateway in future:
$ wechaty gateway --token ${token} --port ${port} --puppet ${puppet} --puppet-token ${puppetToken || ''}
You can set the below environment variables for your Wechaty program to use the DIY token provided by this gateway:
__________________________________________________________________________
$ export WECHATY_PUPPET=wechaty-puppet-service
$ export WECHATY_PUPPET_SERVICE_TOKEN=${token}
$ npm start
Please file an issue if you found any bug or have feature request at https://github.com/wechaty/wechaty/issues
Join the Wechaty Community Gitter Channel at https://gitter.im/wechaty/wechaty if you have any questions.
`);
return 0;
},
});
exports.gateway = gateway;
//# sourceMappingURL=gateway.js.map