Skip to content

Commit 8f5df6b

Browse files
committed
Improve custom subcommand in travis-bot
1 parent 7b98ab7 commit 8f5df6b

File tree

2 files changed

+76
-13
lines changed

2 files changed

+76
-13
lines changed

travis-bot/index.js

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var botkit = require('botkit');
22
var exec = require('child_process').exec;
33
var request = require('request');
4+
var Promise = require('bluebird')
5+
var sprintf = require('sprintf')
46

57
var SLACK_TOKEN = process.env.SLACK_TOKEN;
68
var TRAVIS_CI_TOKEN = process.env.TRAVIS_TOKEN;
@@ -39,19 +41,61 @@ var format = function(args) {
3941
};
4042
};
4143

42-
controller.hears(['^bot\\s+travis\\s+build'],
43-
['message_received', 'ambient'],
44-
function(bot, message) {
45-
bot.reply(message, format({
46-
text: 'Start to build...',
47-
color: '#00ff00',
48-
}));
49-
request(options, function (error, response, body) {
50-
if (error) {
51-
return bot.reply(message, format({
52-
text: 'Failed to build',
53-
color: '#ff0000',
54-
}));
44+
request._xxxAsync = function(method, param) {
45+
return function(param) {
46+
return new Promise(function(resolve, reject) {
47+
request[method](param, function(err, response, body) {
48+
if (err) {
49+
return reject(err);
5550
}
51+
resolve({
52+
response: response,
53+
body: body,
54+
});
5655
});
5756
});
57+
};
58+
};
59+
60+
request.getAsync = request._xxxAsync('get')
61+
request.postAsync = request._xxxAsync('post')
62+
63+
var commandProcMap = {
64+
'': function(bot, message) {
65+
return bot.reply(message, format({
66+
text: 'too few arguments',
67+
color: '#ff0000',
68+
}));
69+
},
70+
help: function(bot, message) {
71+
var attachments = require(__dirname + "/usage.json");
72+
return bot.reply(message, {
73+
attachments: attachments,
74+
icon_emoji: ':construction_worker:',
75+
username: 'travis bot',
76+
});
77+
},
78+
build: function(bot, message) {
79+
request.getAsync(options)
80+
.then(function() {
81+
return bot.reply(message, format({
82+
text: 'Called Travis API',
83+
color: '#00ff00',
84+
}));
85+
});
86+
}
87+
};
88+
89+
controller.hears([/^bot\s+travis(?:\s+(\w+)(?:\s+(\S+))?)?\s*$/],
90+
['message_received', 'ambient'],
91+
function(bot, message) {
92+
var command = message.match[1] || '';
93+
var commandProc = commandProcMap[command];
94+
if(! commandProc) {
95+
return bot.reply(message, format({
96+
text: sprintf('Unknown command [%s]', command),
97+
color: '#ff0000',
98+
}));
99+
}
100+
commandProc(bot, message);
101+
});

travis-bot/usage.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"fallback": "",
4+
"color": "",
5+
"author_name": ":construction_worker: travis-bot",
6+
"author_link": "https://github.com/zplug/bots/tree/master/travis-bot",
7+
"title": "travis bot",
8+
"title_link": "https://github.com/zplug/bots/tree/master/travis-bot",
9+
"text": "A bot to call Travis API. e.g. build",
10+
"fields": [
11+
{
12+
"title": "bot travis build",
13+
"value": "Build a new test",
14+
"short": false
15+
}
16+
],
17+
"footer": "zplug bot"
18+
}
19+
]

0 commit comments

Comments
 (0)