-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendNotificationWebHook.GS
105 lines (95 loc) · 2.67 KB
/
sendNotificationWebHook.GS
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
function sendNotification(){
let jogosDoDia = getGamesListToday();
let mapJogos = jogosDoDia.map((value)=>{
return {
casa:
{placar: value.home_score,
logo: value.home_flag,
time: value.home_team_en
},
fora:
{placar: value.away_score,
logo: value.away_flag,
time: value.away_team_en
},
horario : value.local_date
}
})
//#TODO ordenar pelo horario, do mais cedo para mais tarde
chatNotificationBot.postToRoom(mapJogos)
}
/*
* chatNotificationBot Class - Google Form Notifications
*/
class chatNotificationBot {
/*
* Constructor function
*/
constructor() {
const self = this;
}
static postToRoom(gameEvent) {
const notificationsBot = new chatNotificationBot(),
url = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL');
let response;
if (url) {
try {
let payload = notificationsBot.makeCard(gameEvent);
let options = {
'method' : 'post',
'contentType': 'application/json; charset=UTF-8',
'muteHttpExceptions' : true,
'payload' : JSON.stringify(payload)
};
response = UrlFetchApp.fetch(url, options);
console.log(response.getContentText())
} catch(err) {
console.log(err.message);
}
} else {
console.log('formsNotificationBot: No Webhook URL specified for Bot');
}
}
makeCard(gameEvent) {
let self = this,
sections = [],
cards = [],
card = {},
response = {};
card.header = self.getCardHeader_();
gameEvent.forEach(game => {
sections.push({"widgets" : [self.createGameWidget(game)]});
});
card.sections = sections;
cards.push({"card": card});
response.cardsV2 = cards;
return response;
}
getCardHeader_() {
let today = new Date()
const widget = {};
widget.title = "Jogos do Dia -- Copa 2022";
widget.subtitle = `${today.getDate()}/${today.getMonth()}/${today.getFullYear()}`;
return widget;
}
#TODO trocar a widget por uma grid para deixar centralizado os placares
//[{casa{time,placar,logo},fora{time,placar,logo}}]
createGameWidget(game){
let holderObj = {},
widget = {};
holderObj.startIcon = {
"altText" : game.casa.time,
"imageType" : "CIRCLE",
"iconUrl" : game.casa.logo
}
holderObj.text = `<b>${game.casa.placar} X ${game.fora.placar}</b>`;
holderObj.endIcon = {
"altText" : game.fora.time,
"imageType" : "CIRCLE",
"iconUrl" : game.fora.logo
}
widget.decoratedText = holderObj;
return widget;
}
}
#TODO fução de criação de triggers automático