Skip to content

Commit dd7effe

Browse files
Persijn Kwekkeboompoi33
authored andcommitted
New slack bot integration #691
New message format for slack bot integration Changed the data displayed in message
1 parent fff669b commit dd7effe

File tree

3 files changed

+193
-29
lines changed

3 files changed

+193
-29
lines changed

src/main/resources/lib/office-league-report.js

Lines changed: 166 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ eventLib.listener({
1515
storeLib.refresh();
1616
var game = storeLib.getGameById(event.data.gameId);
1717
if (game) {
18-
var baseUrl = app.config['officeleague.baseUrl'] || 'http://localhost:8080/portal/draft/office-league/app';
19-
var gameJson = createGameJson(game, baseUrl);
18+
var baseUrl = app.config['officeleague.baseUrl'] || 'localhost:8080/webapp/com.enonic.app.officeleague';
19+
var gameData = createGameData(game, baseUrl);
2020
eventLib.send({
2121
type: OFFICE_LEAGUE_GAME_REPORT_EVENT_ID,
2222
distributed: false,
2323
data: {
24-
game: JSON.stringify(gameJson) //TODO Bug in eventLib
24+
game: JSON.stringify(gameData) //TODO Bug in eventLib
2525
}
2626
});
2727
}
2828
}
2929
});
3030

31-
var createGameJson = function (game, baseUrl) {
32-
var gameJson = {
31+
const createGameData = function (game, baseUrl) {
32+
const gameData = {
3333
gameId: game._id,
34-
gameUrl: url(baseUrl, '/games/' + game._id),
34+
url: url(baseUrl, '/games/' + game._id),
3535
finished: game.finished,
3636
startTime: game.time,
3737
modifiedTime: game._timestamp,
@@ -54,7 +54,7 @@ var createGameJson = function (game, baseUrl) {
5454
};
5555

5656
var league = storeLib.getLeagueById(game.leagueId);
57-
gameJson.league = createLeagueJson(league, baseUrl);
57+
gameData.league = createLeagueJson(league, baseUrl);
5858

5959
var p, gp, playerJson, player, leaguePlayer;
6060
for (p = 0; p < game.gamePlayers.length; p++) {
@@ -64,14 +64,14 @@ var createGameJson = function (game, baseUrl) {
6464

6565
playerJson = createPlayerJson(player, gp, leaguePlayer, baseUrl);
6666
playerJson.ranking = storeLib.getRankingForPlayerLeague(gp.playerId, game.leagueId);
67-
gameJson.players[playerJson.playerId] = playerJson;
67+
gameData.players[p] = playerJson;
6868

6969
if (gp.side === 'red') {
70-
gameJson.sides.red.totalScore += gp.score;
71-
gameJson.sides.blue.totalScore += gp.scoreAgainst;
70+
gameData.sides.red.totalScore += gp.score;
71+
gameData.sides.blue.totalScore += gp.scoreAgainst;
7272
} else if (gp.side === 'blue') {
73-
gameJson.sides.blue.totalScore += gp.score;
74-
gameJson.sides.red.totalScore += gp.scoreAgainst;
73+
gameData.sides.blue.totalScore += gp.score;
74+
gameData.sides.red.totalScore += gp.scoreAgainst;
7575
}
7676
}
7777

@@ -83,12 +83,12 @@ var createGameJson = function (game, baseUrl) {
8383

8484
teamJson = createTeamJson(team, gt, leagueTeam, baseUrl);
8585
teamJson.ranking = storeLib.getRankingForTeamLeague(gt.teamId, game.leagueId);
86-
gameJson.teams[teamJson.teamId] = teamJson;
86+
gameData.teams[t] = teamJson;
8787
}
8888
var winPoints = (league.rules || {}).pointsToWin || 10;
89-
setExpectedScore(gameJson, winPoints);
89+
setExpectedScore(gameData, winPoints);
9090

91-
return gameJson;
91+
return createNewMessage(gameData);
9292
};
9393

9494
var createTeamJson = function (team, gameTeam, leagueTeam, baseUrl) {
@@ -159,4 +159,154 @@ var avg = function (numberArray) {
159159

160160
var url = function (baseUrl, relUrl) {
161161
return baseUrl + relUrl;
162-
};
162+
};
163+
164+
function createNewMessage(data) {
165+
if (data.finished) {
166+
return createFinishedGameMessage(data);
167+
} else {
168+
return createNewGameMessage(data);
169+
}
170+
}
171+
172+
/**
173+
* Formats the finished game to a slack message format
174+
*/
175+
function createFinishedGameMessage(data) {
176+
const message = {
177+
"blocks": []
178+
};
179+
180+
message.blocks.push(
181+
{
182+
"type": "header",
183+
"text": {
184+
"type": "plain_text",
185+
"text": `Game finished in ${data.league.name}`
186+
}
187+
}
188+
);
189+
190+
return message;
191+
}
192+
193+
/**
194+
* Formats the new game to a slack message format
195+
* @returns Object
196+
*/
197+
function createNewGameMessage(data) {
198+
const message = {
199+
"blocks": []
200+
};
201+
202+
message.blocks.push(
203+
{
204+
"type": "header",
205+
"text": {
206+
"type": "plain_text",
207+
"text": `New game in ${data.league.name}`
208+
}
209+
}
210+
);
211+
if (Array.isArray(data.teams) && data.teams.length > 0) {
212+
message.blocks.push(
213+
createTeamSection(data.teams[1], [data.players[0], data.players[2]]),
214+
createVsTeamSection(data.players),
215+
createTeamSection(data.teams[0], [data.players[1], data.players[3]])
216+
);
217+
} else {
218+
message.blocks.push(
219+
createPlayerSection(data.players[0]),
220+
createVsPlayerSection(data.players),
221+
createPlayerSection(data.players[1])
222+
);
223+
}
224+
225+
return message;
226+
}
227+
228+
function createVsTeamSection(data) {
229+
return {
230+
"type": "context",
231+
"elements": [
232+
{
233+
"type": "image",
234+
"image_url": `${players[0].imageUrl}`,
235+
"alt_text": `Profile image of ${players[0].name}`
236+
},
237+
{
238+
"type": "image",
239+
"image_url": `${players[2].imageUrl}`,
240+
"alt_text": `Profile image of ${players[2].name}`
241+
},
242+
{
243+
"type": "mrkdwn",
244+
"text": "*VS*"
245+
},
246+
{
247+
"type": "image",
248+
"image_url": `${players[1].imageUrl}`,
249+
"alt_text": `Profile image of ${players[1].name}`
250+
},
251+
{
252+
"type": "image",
253+
"image_url": `${players[3].imageUrl}`,
254+
"alt_text": `Profile image of ${players[3].name}`
255+
}
256+
]
257+
};
258+
}
259+
260+
function createVsPlayerSection(players) {
261+
return {
262+
"type": "context",
263+
"elements": [
264+
{
265+
"type": "mrkdwn",
266+
"text": "*VS*"
267+
}
268+
]
269+
};
270+
}
271+
272+
function createPlayerSection(player) {
273+
return {
274+
"type": "section",
275+
"fields": [
276+
{
277+
"type": "mrkdwn",
278+
"text": `*${player.name}* ${player.rating}`
279+
},
280+
],
281+
"accessory": {
282+
"type": "image",
283+
"image_url": `${player.imageUrl}`,
284+
"alt_text": `Profile image of ${player.name}`
285+
}
286+
}
287+
}
288+
289+
function createTeamSection(teamData, players) {
290+
return {
291+
"type": "section",
292+
"text": {
293+
"type": "mrkdwn",
294+
"text": `*${teamData.name}*`
295+
},
296+
"fields": [
297+
{
298+
"type": "mrkdwn",
299+
"text": `*${players[0].name}* ${players[0].rating}`
300+
},
301+
{
302+
"type": "mrkdwn",
303+
"text": `*${players[1].name}* ${players[1].rating}`
304+
}
305+
],
306+
"accessory": {
307+
"type": "image",
308+
"image_url": `${teamData.imageUrl}`,
309+
"alt_text": `Profile image for ${teamData.name}`
310+
}
311+
};
312+
}

src/main/resources/lib/webhooks.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ var httpClient = require('/lib/http-client');
66
var OFFICE_LEAGUE_GAME_REPORT_EVENT_ID = reportLib.OFFICE_LEAGUE_GAME_REPORT_EVENT_ID;
77
var UUID = Java.type('java.util.UUID');
88

9+
/**
10+
* Setup an event listner so the webhook can handle the data
11+
*/
912
exports.setupWebHooks = function () {
1013
eventLib.listener({
1114
type: 'custom.' + OFFICE_LEAGUE_GAME_REPORT_EVENT_ID,
@@ -22,7 +25,11 @@ exports.setupWebHooks = function () {
2225
});
2326
};
2427

25-
var processGameWebhooks = function (game) {
28+
/***
29+
* Goes over all webhooks and tried processes the games
30+
* @param game -- Json data of the current game event
31+
*/
32+
const processGameWebhooks = function (game) {
2633
var urls = [];
2734
for (var key in app.config) {
2835
if (key.indexOf('webhooks.server.') === 0 && key.endsWith('.url')) {
@@ -31,19 +38,24 @@ var processGameWebhooks = function (game) {
3138
}
3239

3340
for (var u = 0; u < urls.length; u++) {
34-
try {
41+
/* try { */
3542
processGameWebhook(game, urls[u]);
36-
} catch (e) {
43+
/* } catch (e) {
3744
log.warning('Error posting to webhook: ' + urls[u]);
3845
if (e.printStackTrace) {
3946
e.printStackTrace();
4047
}
41-
}
48+
} */
4249
}
4350
};
4451

45-
var processGameWebhook = function (gameJson, url) {
46-
var event = {
52+
/***
53+
* Creates the message data and send it to the the url
54+
* @param gameJson json data of the game
55+
* @param url url to the webhook where the game data will be sent
56+
*/
57+
const processGameWebhook = function (gameJson, url) {
58+
const event = {
4759
id: "evt_" + randomId(),
4860
event: "game.updated",
4961
created: Math.floor(new Date().getTime() / 1000),
@@ -52,17 +64,18 @@ var processGameWebhook = function (gameJson, url) {
5264
}
5365
};
5466

55-
var response = httpClient.request({
56-
url: url,
67+
// log.info(JSON.stringify(gameJson, null, 4));
68+
69+
const response = httpClient.request({
70+
url,
5771
method: 'POST',
5872
contentType: 'application/json',
59-
body: JSON.stringify(event),
60-
headers: {
61-
'Content-Type': 'application/json'
62-
}
73+
body: JSON.stringify(gameJson),
6374
});
6475

6576
if (response.status >= 300 || response.status < 200) {
77+
// log.info(JSON.stringify(gameJson, null, 4));
78+
// log.info(JSON.stringify(response, null, 4));
6679
log.info('Error posting webhook to [' + url + ']: ' + response.status + ' ' + response.message);
6780
}
6881
};

src/main/resources/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ if (!app.config['skip-test-data']) {
2020
maintenanceTaskLib.launchGameGCTask();
2121

2222
webhooks.setupWebHooks();
23-
gameNotifications.setupPushNotifications();
23+
// Currently not working
24+
// gameNotifications.setupPushNotifications();

0 commit comments

Comments
 (0)