-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_bot.js
138 lines (125 loc) · 4.33 KB
/
command_bot.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var Botkit = require('botkit');
var request = require('request');
var controller = Botkit.slackbot();
var process = require('child_process');
var bot = controller.spawn({
token: 'xoxb-85944343712-c3P4Zm71lr8HdVs06VmSXXwu',
retries: 500
});
function startRTM() {
bot.startRTM(function(err) {
if (err) {
throw new Error(err);
}
});
}
controller.hears('^hello','direct_message,direct_mention,mention',function(bot, message) {
bot.reply(message, 'Hello <@'+message.user+'>');
});
controller.hears(['call me (.*)', 'my name is (.*)'], 'direct_message,direct_mention,mention', function(bot, message) {
var name = message.match[1];
controller.storage.users.get(message.user, function(err, user) {
if (!user) {
user = {
id: message.user,
};
}
user.name = name;
controller.storage.users.save(user, function(err, id) {
bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
});
});
});
controller.hears('^help', 'direct_message,direct_mention,mention', function(bot, message) {
var reply_with_attachments = {
"attachments": [
{
"pretext": "Here are all commands.",
"color": "#36a64f",
"fields": [
{
"title": "build deploy",
},
{
"title": "test [job name]",
},
{
"title": "my name is [your name]",
},
{
"title": "get [job name] result",
}
]
}
]
}
// console.log(JSON.stringify(reply_with_attachments))
bot.reply(message, reply_with_attachments)
});
controller.hears('^build deploy', 'direct_message,direct_mention,mention',function(bot,message) {
var job_name = 'Check_Build_Deploy';
console.log(job_name)
var job_url = 'http://192.168.5.1:8080/job/'+job_name+'/build?token=build_deploy';
request(job_url, function () {
var str = 'OK! Let me build the '+job_name
bot.reply(message, str)
})
})
controller.hears('^test (.*)', 'direct_message,direct_mention,mention',function(bot,message) {
var job_name = message.match[1].toLowerCase();
console.log(job_name)
var job_url = 'http://192.168.5.1:8080/job/'+job_name+'/build?token='+job_name;
request(job_url, function () {
if (job_name == 'autotest' || job_name =='monkeytest') {
var str = 'OK! Let me test the '+job_name
bot.reply(message, str)
}
else {
bot.reply(message, 'Sorry, I can\'t find the '+job_name+'. >_<!!')
}
})
})
controller.hears('^get (.*) result','direct_message,direct_mention,mention',function(bot, message) {
var job_name = message.match[1].toLowerCase();
var jenkins_url = 'http://192.168.5.1:8080/job/'+job_name+'/lastBuild/testReport/api/json';
request(jenkins_url, function(error, responce, body) {
if(!error && responce.statusCode === 200) {
var jenkinsBuildJson = JSON.parse(body);
var reply_with_attachments = {
"attachments": [
{
"fallback": "Required plain-text summary of the attachment.",
"color": "#55699e",
"title": "The last build robot result of " + job_name,
"title_link": 'http://192.168.5.1:8080/job/'+job_name+'/lastBuild/testReport',
"fields": [
{
"title": "Failed Cases",
"value": jenkinsBuildJson.failCount,
"short": true
},
{
"title": "Passed Cases",
"value": jenkinsBuildJson.childReports[0].result.passCount,
"short": true
},
]
}
]
}
bot.reply(message, reply_with_attachments)
}
})
})
controller.hears('','direct_message,direct_mention,mention',function(bot,message) {
bot.reply(message,'Fuck you!');
bot.reply(message,'You can use "help"');
})
/*
* Start RTM
*/
startRTM();
/*
* Autorestart RTM
*/
controller.on('rtm_close', startRTM);