-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
93 lines (83 loc) · 2.4 KB
/
server.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
const dotenv = require('dotenv');
const express = require('express');
const bodyParser = require('body-parser');
const Nexmo = require('nexmo');
const Twitter = require('twitter');
const path = require('path');
dotenv.config();
const app = express();
app.use(bodyParser.json());
// heroku handling
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
})
}
// const port = 5000;
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`server started on port ${port}`));
// Twitter credentials
const client = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN_KEY,
access_token_secret: process.env.ACCESS_TOKEN_SECRET
});
// Nexmo credentials
const nexmo = new Nexmo({
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
applicationId: process.env.APPLICATION_ID,
privateKey: './private.key'
}, {debug: true});
// Receive input and call Twitter API
app.post('/userName', function(req, res) {
userName = req.body.userName;
app.get('/twitter', (req, res) => {
// read most recent tweet
var username = {screen_name: userName };
client.get('statuses/user_timeline', username, function(error, tweets, response) {
if (!error) {
console.log(`most recent tweet: `, tweets[0].text);
res.json(tweets[0].text)
}
});
});
});
// send SMS via Nexmo
app.post('/sendSMS', (req, res) => {
res.send(req.body);
let score = req.body.score;
let toNumber = req.body.number;
let tweet = req.body.tweetContent;
let userName = req.body.userName;
let scoreSign = '';
// analyze the sentiment and assign emoji
if (score > '.5') {
scoreSign = '✅'
} else if (score == '.5') {
scoreSign = '😐'
} else {
scoreSign = '👿'
}
// Nexmo Messages API
const nexmoNumber = process.env.NEXMO_NUMBER
nexmo.channel.send(
{ type: 'sms', number: toNumber }, // To
{ type: 'sms', number: nexmoNumber }, // From
{
content: {
type: 'text',
text: `${userName}'s most recent tweet was: \"\ ${tweet}\"\ and the sentiment score is: ${scoreSign}`,
}
},
(err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
}
);
});