-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (45 loc) · 1.43 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
const express = require('express');
const https = require('https');
const config = require('./config');
const client = require('twilio')(config.accountSid, config.authToken);
const app = express();
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
// request to statsapi for the Minnesota Wild
const req = https.request('https://statsapi.web.nhl.com/api/v1/teams/30?expand=team.schedule.previous', (res) => {
res.on('data', (d) => {
const data = JSON.parse(d);
const teamData = data.teams[0].previousGameSchedule.dates[0].games[0].teams;
let homeTeam = teamData.home.team.id === 30 ? teamData.home : teamData.away;
let competitor = teamData.home.team.id === 30 ? teamData.away : teamData.home;
let verb;
let msgBody;
if( homeTeam.score > competitor.score ) {
// win
verb = 'yes';
} else if ( homeTeam.score === competitor.score ) {
// tie game
verb = 'tie';
} else {
// lost
verb = 'no';
}
msgBody = verb.toUpperCase() + ' ' + homeTeam.score + '-' + competitor.score + ' against the ' + competitor.team.name
sendData(msgBody);
});
});
req.on('error', (e)=> {
console.error(e.message);
});
req.end();
function sendData(data) {
client.messages
.create({
body: 'Did the Minnesota Wild win?\n' + data,
from: config.from,
to: config.to
})
.then(message=> console.log(message.sid))
.done();
}