-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathGet NASA Astronomy Picture of the Day.js
168 lines (153 loc) · 5.63 KB
/
Get NASA Astronomy Picture of the Day.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
/*
activation_example:!apod, or !apod -r to get a random result
regex:!apod(\s-r)?
flags:gmi
*/
// Set a System Property named 'nasaapi.key' to leverage a free dedicated API key (get one here https://api.nasa.gov/)
// DEMO_KEY rate limit is (default) of 30/hr, with 50/day
// API Key rate limist is (default) 1000/hr
var apiKey = gs.getProperty('nasaapi.key', 'DEMO_KEY');
var isRandom = (current.text.match(/!apod\s-r/gmi) !== null);
var endpoint = isRandom ? 'https://api.nasa.gov/planetary/apod?count=1&thumbs=True&api_key=' + apiKey : 'https://api.nasa.gov/planetary/apod?thumbs=True&api_key=' + apiKey;
// Perform image call
var picReq = new sn_ws.RESTMessageV2();
picReq.setEndpoint(endpoint);
picReq.setHttpMethod('GET');
picReq.setRequestHeader('User-Agent', 'ServiceNow');
var picResp = picReq.execute();
// Process output
var output = handleAPI(picResp, isRandom);
var message = buildBlocks(output);
// Send to Slack
new x_snc_slackerbot.Slacker().send_chat(current, message, true);
// Function declarations
function handleAPI(response, random) {
var out = {};
out.payload = {};
// Check negative paths - rate limit or error
if (response.getStatusCode() == 429) {
out.success = false;
out.type = 'reached the API rate limit';
out.message = 'Your token has encountered the API limit, which is currently set to ' + picResp.getHeader('X-Ratelimit-Limit') + '. If you have not yet added a NASA API key, consider adding this to get a higher limit.';
} else if (response.getStatusCode() != 200) {
out.success = false;
out.type = 'encountered an unexpected response';
out.message = 'Please try again later. Response status code: ' + response.getStatusCode();
} else if (response.getErrorCode() != 0) {
out.success = false;
out.type = 'encountered unexpected behaviour';
out.message = 'Please try again later. Error message: ' + response.getErrorMessage();
} else {
// Positive path
var body = JSON.parse(response.getBody());
if (random) { // Returns array
body = body[0];
}
out.payload.date = body.date;
out.payload.title = body.title;
out.payload.copyright = (body.copyright !== undefined) ? body.copyright : null;
out.payload.type = body.media_type;
out.payload.thumb = (body.thumbnail_url !== undefined) ? body.thumbnail_url : null;
out.payload.content = body.url;
out.payload.image_fullres = body.hdurl;
out.success = true;
out.type = random ? 'random' : 'latest';
out.message = body.explanation;
}
return out;
}
function buildBlocks(details) {
var blockArr = [];
if (!details.success) {
blockArr.push({
'type': 'section',
'text': {
'type': 'mrkdwn',
'text': '!apod: Unfortunately, this parser ' + details.type + ' :service_no:. ' + details.message
}
});
} else {
blockArr.push({
'type': 'header',
'text': {
'type': 'plain_text',
'text': 'Astronomy Picture of the Day: ' + details.payload.title
}
});
blockArr.push({
'type': 'context',
'elements': [
{
'type': 'mrkdwn',
'text': '*Date:* ' + details.payload.date
}
]
});
if (details.payload.image_fullres !== undefined) {
blockArr.push({
'type': 'context',
'elements': [
{
'type': 'mrkdwn',
'text': '*HD Url:* <' + details.payload.image_fullres + '|Image>'
}
]
});
}
if (details.payload.copyright !== null) {
blockArr.push({
'type': 'context',
'elements': [
{
'type': 'mrkdwn',
'text': '*Copyright:* ' + details.payload.copyright
}
]
});
}
if (details.payload.type == 'image') {
blockArr.push({
'type': 'image',
'title': {
'type': 'plain_text',
'text': details.payload.title
},
'image_url': details.payload.content,
'alt_text': details.payload.title
});
} else {
blockArr.push({
'type': 'video',
'title': {
'type': 'plain_text',
'text': details.payload.title,
'emoji': true
},
'title_url': details.payload.content,
'description': {
'type': 'plain_text',
'text': 'NASA Astronomy Picture of the Day: Video',
'emoji': true
},
'video_url': details.payload.content,
'alt_text': details.payload.title,
'thumbnail_url': details.payload.thumb,
'provider_name': 'NASA',
'provider_icon_url': 'https://www.nasa.gov/wp-content/themes/nasa/assets/images/nasa-logo.svg'
});
}
blockArr.push({
'type': 'context',
'elements': [
{
'type': 'mrkdwn',
'text': '*Explanation:* ' + details.message
}
]
});
}
return {
'text': 'NASA Astronomy Picture of the Day',
'blocks': blockArr
};
}