-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
84 lines (59 loc) · 2.35 KB
/
server.js
File metadata and controls
84 lines (59 loc) · 2.35 KB
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
const http = require('http');
const express = require('express');
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const app = express();
var bodyParser = require('body-parser');
const twilio = require('twilio');
app.use(bodyParser.urlencoded({ extended: false }));
//async
app.post('/whatsapp', async (req, res) => {
const twiml = new MessagingResponse();//bodyparser
const GOOGLE_APPLICATION_CREDENTIALS="/Users/longhchung/Desktop/jsexercisedemo/durhack/keys.json"
async function quickstart(GOOGLE_APPLICATION_CREDENTIALS, req){
// Imports the Google Cloud client library
const language = require('@google-cloud/language');
// Instantiates a client
const client = new language.LanguageServiceClient();
// The text to analyze
const text = req.body.Body;
const document = {
content: text,
type: 'PLAIN_TEXT',
};
// Detects the sentiment of the text
const [result] = await client.analyzeSentiment({document: document});
const sentiment = result.documentSentiment;
console.log(sentiment.score)
twiml.message('Text: '+text);
twiml.message('Sentiment score:'+sentiment.score.toFixed(2));
if (sentiment.score<-0.5) {
twiml.message('Probably True') ;
} else if (sentiment.score < 0.0) {
twiml.message('Maybe True');
} else if (sentiment.score < 0.5) {
twiml.message( 'Maybe False');
} else {
twiml.message('Probably False');
}
//twiml.message('Sentiment magnitude: '+sentiment.magnitude);
return sentiment
}
if (req.body.Body == 'BBC') {
twiml.message('Hi!');
} else {
//twiml.message(sentiment.score.toString());
let sentiment = await quickstart(GOOGLE_APPLICATION_CREDENTIALS, req)
console.log(`Sentiment score: ${sentiment.score}`);
// console.log(`Text: ${text}`);
// console.log(`Sentiment score: ${sentiment.score}`);
// console.log(`Sentiment magnitude: ${sentiment.magnitude}`);
}
res.writeHead(200, {'Content-Type': 'text/xml'});
//response.redirect('https://demo.twilio.com/welcome/sms/');
//https://timberwolf-mastiff-9776.twil.io/demo-reply
res.end(twiml.toString());
});
http.createServer(app).listen(1337, () => {
console.log('Express server listening on port 1337');
});
//twilio phone-numbers:update "+447360545958" --sms-url="http://localhost:1337/whatsapp"