-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
182 lines (156 loc) · 7.05 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* A Bot for Slack!
*/
/**
* Define a function for initiating a conversation on installation
* With custom integrations, we don't have a way to find out who installed us, so we can't message them :(
*/
require('dotenv').config();
let JIRA_URL = process.env.JIRA_HOST_PROTOCOL + '://' + process.env.JIRA_HOST + ':' + process.env.JIRA_PORT + '/browse/';
let JiraClient = require('jira-connector');
let jira = new JiraClient({
host: process.env.JIRA_HOST,
basic_auth: {
username: process.env.JIRA_USERNAME,
password: process.env.JIRA_PASSWORD
},
protocol: process.env.JIRA_HOST_PROTOCOL,
port: process.env.JIRA_PORT
});
// jira.issue.getIssue({
// issueKey: 'DEVOPS-600'
// }, function(error, issue) {
// console.log(issue.fields.status());
// });
function onInstallation(bot, installer) {
if (installer) {
bot.startPrivateConversation({user: installer}, function (err, convo) {
if (err) {
console.log(err);
} else {
convo.say('I am a bot that has just joined your team');
convo.say('You must now /invite me to a channel so that I can be of use!');
}
});
}
}
/**
* Configure the persistence options
*/
let config = {};
if (process.env.MONGOLAB_URI) {
let BotkitStorage = require('botkit-storage-mongo');
config = {
storage: BotkitStorage({mongoUri: process.env.MONGOLAB_URI}),
};
} else {
config = {
json_file_store: ((process.env.TOKEN) ? './db_slack_bot_ci/' : './db_slack_bot_a/'), //use a different name if an app or CI
};
}
let controller;
/**
* Are being run as an app or a custom integration? The initialization will differ, depending
*/
if (process.env.TOKEN || process.env.SLACK_TOKEN) {
//Treat this as a custom integration
let customIntegration = require('./lib/custom_integrations');
let token = (process.env.TOKEN) ? process.env.TOKEN : process.env.SLACK_TOKEN;
controller = customIntegration.configure(token, config, onInstallation);
} else if (process.env.CLIENT_ID && process.env.CLIENT_SECRET && process.env.PORT) {
//Treat this as an app
let app = require('./lib/apps');
controller = app.configure(process.env.PORT, process.env.CLIENT_ID, process.env.CLIENT_SECRET, config, onInstallation);
} else {
console.log('Error: If this is a custom integration, please specify TOKEN in the environment. If this is an app, please specify CLIENTID, CLIENTSECRET, and PORT in the environment');
process.exit(1);
}
/**
* A demonstration for how to handle websocket events. In this case, just log when we have and have not
* been disconnected from the websocket. In the future, it would be super awesome to be able to specify
* a reconnect policy, and do reconnections automatically. In the meantime, we aren't going to attempt reconnects,
* WHICH IS A B0RKED WAY TO HANDLE BEING DISCONNECTED. So we need to fix this.
*
* TODO: fixed b0rked reconnect behavior
*/
// Handle events related to the websocket connection to Slack
controller.on('rtm_open', function (bot) {
console.log('** The RTM api just connected!');
});
controller.on('rtm_close', function (bot) {
console.log('** The RTM api just closed');
// you may want to attempt to re-open
});
/**
* Core bot logic goes here!
*/
// BEGIN EDITING HERE!
controller.on('bot_channel_join', function (bot, message) {
bot.reply(message, "I'm here!")
});
controller.hears(['hello', 'hi', 'greetings'], ['direct_mention', 'mention', 'direct_message'], function (bot, message) {
bot.reply(message, 'Hello!');
});
/**
* AN example of what could be:
* Any un-handled direct mention gets a reaction and a pat response!
*/
controller.on('direct_message,mention,direct_mention', function (bot, message) {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'heart',
}, function (err) {
if (err) {
console.log(err)
}
// bot.reply(message, 'I heard you loud and clear boss.');
});
});
controller.hears(['use (.*) ticket (.*)', 'color (.*) ticket (.*)', 'use (.*) jira (.*)', 'color (.*) jira (.*)'], 'direct_message,direct_mention,mention', function (bot, message) {
let color = message.match[1].toLowerCase();
let ticket = message.match[2];
let userData = {isFree: false, ticket_num: ticket, slack_user_id: message.user};
if (color === 'red' || color === 'blue' || color === 'stress' || color === 'orange' || color === 'green') {
controller.storage.users.get(color, function (err, _color) {
let colorData;
if (!_color || _color.name.isFree) {
colorData = {
id: color,
name: userData
};
controller.storage.users.save(colorData);
bot.reply(message, 'Thank you <@' + message.user + '>, ' + 'You have been assigned for `' + color.toUpperCase() + '` with the ticket <' + JIRA_URL + ticket + '|' + ticket + '> ');
} else {
bot.reply(message, 'Sorry <@' + message.user + '>, but `' + color.toUpperCase() + '` is currently being used by <@' + _color.name.slack_user_id + '> Ticket:' +
'<' + JIRA_URL + _color.name.ticket_num + '|' + _color.name.ticket_num + '>')
}
});
} else {
bot.reply(message, 'Sorry <@' + message.user + '>, but the color you\'ve selected (`' + color.toUpperCase() + '`) is invalid. \n You may choose between ' +
'Red/Blue/Green/Orange/Stress');
}
});
controller.hears(['release (.*)', 'free (.*)', 'done (.*)'], 'direct_message,direct_mention,mention', function (bot, message) {
let color = message.match[1].toLowerCase();
if (color === 'red' || color === 'blue' || color === 'stress' || color === 'orange' || color === 'green') {
controller.storage.users.get(color, function (err, _color) {
if (!_color || _color.name.isFree === true) {
bot.reply(message, 'Psss....<@' + message.user + '>, the color `' + color.toUpperCase() + '` is already freed.');
} else {
if (message.user === _color.name.slack_user_id) {
_color.name.isFree = true;
controller.storage.users.save(_color);
bot.reply(message, 'Thank you <@' + message.user + '>, `' + color.toUpperCase() + '` has been freed and it\'s now available to anyone');
} else {
bot.reply(message, 'Sorry <@' + message.user + '>, but `' + color.toUpperCase() + '` is currently being used by <@' + _color.name.slack_user_id + '> Ticket:' +
'<' + JIRA_URL + _color.name.ticket_num + '|' + _color.name.ticket_num + '> and only <@' + _color.name.slack_user_id + '>' +
'can release/free it');
}
}
});
} else {
bot.reply(message, 'Sorry <@' + message.user + '>, but the color you\'ve selected (`' + color.toUpperCase() + '`) is invalid. \n You may choose between ' +
'Red/Blue/Green/Orange/Stress');
}
});