This repository has been archived by the owner on May 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
hubot.js
108 lines (96 loc) · 3.43 KB
/
hubot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const Path = require('path');
const _ = require('lodash');
const env = require('node-env-file');
const {createRobotAdapter} = require('./lib/sbot');
const botServer = require('./lib/api');
const {injector, loadBinders} = require('./lib/binders');
loadBinders();
const {OPTION_ENV_PATH} = injector.getInstance('$$constants');
const {envs} = injector.getInstance('$$utils');
if (process.platform !== 'win32') {
process.on('SIGTERM', () => process.exit(0));
}
// ignore SIGHUP signal
process.on('SIGHUP', () => console.error('Received SIGHUP signal from OS, ignoring'));
// uncaught exception handle
process.on('uncaughtException', err => {
console.error(`Uncaught exception:`);
console.error(err);
});
// unhandled rejection
process.on('unhandledRejection', (reason, p) => {
console.error(`Unhandled rejection: `);
console.log(p);
});
env(OPTION_ENV_PATH);
let adapterEnvFile = envs('ADAPTER_ENV_FILE');
env(adapterEnvFile);
let proxy = envs('HTTP_PROXY_ENDPOINT');
if (proxy) {
process.env.http_proxy = process.env.HTTP_PROXY = proxy;
if (!process.env.HTTPS_PROXY && !process.env.https_proxy) {
process.env.https_proxy = process.env.HTTPS_PROXY = proxy;
}
}
let adapterName = envs('ADAPTER_NAME');
let specifiedScripts = envs('SBOT_SCRIPTS') || '';
let externalModules = envs('SBOT_HUBOT_MODULES') || '';
function loadBot() {
let robot = injector.getInstance('$$sbot');
// Warning in development
if (envs('NODE_ENV') !== 'production') {
robot.logger.warn('Running bot in development mode. To run in production set NODE_ENV=production.');
}
// Create an adapter for robot
let adapter = createRobotAdapter(adapterName, robot);
// Associate adapter with robot (really bad coupling from Hubot's design)
robot.loadAdapter(adapter);
robot._middlewares.loadMiddlewares(robot);
robot.logger.info(`Running hubot version ${robot.version}`);
// start bot server
robot.sbotApp = botServer.use(robot);
// load scripts after connected
robot.adapter.once('connected', () => {
// Load Default scripts in path
let srcPath = `${__dirname}/src`;
robot.load(srcPath);
let scriptsPath = `${__dirname}/scripts`;
robot.load(scriptsPath);
// Load scripts from an additional specified path
let specifiedScriptsPaths = _.compact(_.split(specifiedScripts, ' '));
if (_.isEmpty(specifiedScriptsPaths)) {
specifiedScriptsPaths = [];
}
_.each(specifiedScriptsPaths, specifiedPath => {
let specified = '';
if (specifiedPath[0] === '/') {
specified = specifiedPath;
} else {
specified = Path.resolve('.', specifiedPath);
}
if (specified !== srcPath && specified !== scriptsPath) {
robot.load(specified);
}
});
// Removed hubot-scripts.json usage since it is deprecated
// https://github.com/github/hubot-scripts
let externalModulesPaths = _.compact(_.split(externalModules, ' '));
if (_.isEmpty(externalModulesPaths)) {
externalModulesPaths = [];
}
if (externalModulesPaths && (externalModulesPaths.length > 0)) {
robot.loadExternalScripts(externalModulesPaths);
}
// must bind nlp module after all scripts is loaded
robot.nlp = injector.getInstance('$$nlp');
// must bind help module after all scripts is loaded
robot.helper = injector.getInstance('$$helper');
});
try {
robot.run();
} catch (e) {
robot.logger.error(`Running with error: ${e.message}`);
robot.logger.debug(e);
}
}
loadBot();