@juzi/wechaty
Version:
Wechaty is a RPA SDK for Chatbot Makers.
112 lines • 5.45 kB
JavaScript
;
/**
* Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
*
* @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
* Wechaty Contributors <https://github.com/wechaty>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/// <reference path="./io-peer/json-rpc-peer.d.ts" />
Object.defineProperty(exports, "__esModule", { value: true });
exports.VERSION = exports.GIT_COMMIT_HASH = exports.config = exports.log = exports.isProduction = exports.qrcodeValueToImageUrl = exports.PUPPET_PAYLOAD_SYNC_MAX_RETRY = exports.PUPPET_PAYLOAD_SYNC_GAP = exports.AT_SEPARATOR_REGEX = exports.FOUR_PER_EM_SPACE = exports.qrCodeForChatie = exports.CHATIE_OFFICIAL_ACCOUNT_ID = exports.Config = void 0;
const wechaty_puppet_1 = require("@juzi/wechaty-puppet");
Object.defineProperty(exports, "log", { enumerable: true, get: function () { return wechaty_puppet_1.log; } });
const file_box_1 = require("file-box");
const puppet_config_js_1 = require("./puppet-config.js");
const package_json_js_1 = require("./package-json.js");
Object.defineProperty(exports, "GIT_COMMIT_HASH", { enumerable: true, get: function () { return package_json_js_1.GIT_COMMIT_HASH; } });
const VERSION = package_json_js_1.packageJson.version || '0.0.0';
exports.VERSION = VERSION;
/**
* to handle unhandled exceptions
*/
if (wechaty_puppet_1.log.level() === 'verbose' || wechaty_puppet_1.log.level() === 'silly') {
wechaty_puppet_1.log.info('Config', 'registering process.on("unhandledRejection") for development/debug');
/**
* Refer to https://nodejs.org/api/process.html#process_event_unhandledrejection
* the reason is in type: Error | any
*/
process.on('unhandledRejection', (reason, promise) => {
wechaty_puppet_1.log.error('Config', '###########################');
wechaty_puppet_1.log.error('Config', 'Wechaty unhandledRejection: %s %s', reason.stack || reason, promise);
wechaty_puppet_1.log.error('Config', '###########################');
promise.catch(err => {
wechaty_puppet_1.log.error('Config', 'process.on(unhandledRejection) promise.catch(%s)', err.message);
console.error('Config', err); // I don't know if log.error has similar full trace print support like console.error
});
});
process.on('uncaughtException', function (error) {
const origin = arguments[1]; // to compatible with node 12 or below version typings
wechaty_puppet_1.log.error('Config', '###########################');
wechaty_puppet_1.log.error('Config', 'Wechaty uncaughtException: %s %s', error.stack, origin);
wechaty_puppet_1.log.error('Config', '###########################');
});
}
const DEFAULT_SETTING = package_json_js_1.packageJson['wechaty'];
class Config {
default = DEFAULT_SETTING;
apihost = process.env['WECHATY_APIHOST'] || DEFAULT_SETTING['DEFAULT_APIHOST'];
serviceIp = process.env['WECHATY_PUPPET_SERVICE_IP'] || '';
systemPuppetName() {
return (process.env['WECHATY_PUPPET'] || puppet_config_js_1.OFFICIAL_PUPPET_DEFAULT).toLowerCase();
}
name = process.env['WECHATY_NAME'];
// DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
token = process.env['WECHATY_TOKEN'];
debug = !!(process.env['WECHATY_DEBUG']);
httpPort = process.env['PORT']
|| process.env['WECHATY_PORT']
|| DEFAULT_SETTING['DEFAULT_PORT'];
docker = !!(process.env['WECHATY_DOCKER']);
constructor() {
wechaty_puppet_1.log.verbose('Config', 'constructor()');
this.validApiHost(this.apihost);
}
validApiHost(apihost) {
if (/^[a-zA-Z0-9.\-_]+:?[0-9]*$/.test(apihost)) {
return true;
}
throw new Error('validApiHost() fail for ' + apihost);
}
}
exports.Config = Config;
exports.CHATIE_OFFICIAL_ACCOUNT_ID = 'gh_051c89260e5d';
function qrCodeForChatie() {
const CHATIE_OFFICIAL_ACCOUNT_QRCODE = 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5';
return file_box_1.FileBox.fromQRCode(CHATIE_OFFICIAL_ACCOUNT_QRCODE);
}
exports.qrCodeForChatie = qrCodeForChatie;
// http://jkorpela.fi/chars/spaces.html
// String.fromCharCode(8197)
exports.FOUR_PER_EM_SPACE = String.fromCharCode(0x2005);
// mobile: \u2005, PC、mac: \u0020
exports.AT_SEPARATOR_REGEX = /[\u2005\u0020]/;
exports.PUPPET_PAYLOAD_SYNC_GAP = 3 * 1000;
exports.PUPPET_PAYLOAD_SYNC_MAX_RETRY = 4;
function qrcodeValueToImageUrl(qrcodeValue) {
return [
'https://wechaty.js.org/qrcode/',
encodeURIComponent(qrcodeValue),
].join('');
}
exports.qrcodeValueToImageUrl = qrcodeValueToImageUrl;
function isProduction() {
return process.env['NODE_ENV'] === 'production'
|| process.env['NODE_ENV'] === 'prod';
}
exports.isProduction = isProduction;
const config = new Config();
exports.config = config;
//# sourceMappingURL=config.js.map