This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
forked from hubot-archive/hubot-pager-me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
71 lines (61 loc) · 1.81 KB
/
index.coffee
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
fs = require 'fs'
path = require 'path'
module.exports = (robot, scripts) ->
scriptsPath = path.resolve(__dirname, 'src', 'scripts')
fs.exists scriptsPath, (exists) ->
if exists
for script in fs.readdirSync(scriptsPath)
if scripts? and '*' not in scripts
robot.loadFile(scriptsPath, script) if script in scripts
else
robot.loadFile(scriptsPath, script)
if require.main == module
console.log('Hubot Pager Me Loaded as primary module')
# a dummy robot that can not do much
robot = {
functions: []
send: (txt) ->
console.log('Hubot answer:')
console.log(txt)
return
}
pagerduty = require('./src/pagerduty')
if pagerduty.missingEnvironmentForApi(robot)
return
robot.receive = (msg) ->
res = null
filtered = robot.functions.filter((setup) ->
temp = msg.match(setup.regex)
if temp != null
res = temp
return true
)
if res != null
console.log('Found a message match!')
if filtered.length > 1
console.log("Possible issue with RegExp matching: #{filtered.length} matches found!")
return { filtered, res }
robot.respond = (regex, fn) ->
robot.functions.push({ regex, fn })
return
robot.loadFile = (pathToScript, scriptFileName) ->
resolvedPathToScript = path.join(pathToScript, scriptFileName)
fn = require(resolvedPathToScript)
fn(robot)
return
inp = process.argv[process.argv.length - 1]
setTimeout(() ->
# load the hubot scripts
module.exports(robot)
return
, 10)
setTimeout(() ->
# run a custom message parsing
parsed = robot.receive(if (inp.indexOf('coffee') > -1) then "who's oncall" else inp)
if parsed.res == null
console.log('No match')
else
command = parsed.filtered[0]
command.fn(robot)
return
, 500)