Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (61 loc) · 1.99 KB
/
Copy pathindex.js
File metadata and controls
78 lines (61 loc) · 1.99 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
'use strict';
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const language = require('@google-cloud/language');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const server = app.listen(process.env.PORT || 3000, () => {
console.log(`Express server listening on port ${server.address().port} in ${app.settings.env} mode`);
});
// this is used with the heroku one-click install.
// if you are running locally, use GOOGLE_APPLICATION_CREDENTIALS to point to the file location
let config = null;
if (process.env.GOOGLE_APPLICATION_CREDENTIALS === undefined) {
config = {
projectId: 'nexmo-extend',
credentials: {
client_email: process.env.GOOGLE_CLIENT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n')
}
}
}
// Reading the inbound SMS messages
const handleRoute = (req, res) => {
let params = req.body;
if (req.method === "GET") {
params = req.query
}
if (!params.to || !params.msisdn) {
res.status(400).send({'error': 'This is not a valid inbound SMS message!'});
} else {
analyzeTone(params);
res.status(200).end();
}
};
// Using route here to allow for GET or POST from https://dashboard.nexmo.com/settings
app.route('/message')
.get(handleRoute)
.post(handleRoute)
.all((req, res) => res.status(405).send());
// Google Cloud Sentiment Analysis
const client = new language.LanguageServiceClient();
function analyzeTone(params) {
let document = {
content: params.text,
type: 'PLAIN_TEXT',
};
client.analyzeSentiment({document: document})
.then(results => {
// console.log(results[0]);
console.log(params.text);
console.dir(results, {depth: null})
})
.catch(err => {
console.log('error', err);
});
}
analyzeTone({text:"I am happy!", id:"1"});
analyzeTone({text:"I am sad.", id:"2"});
analyzeTone({text:"So happy there was a pop quiz today.", id:"3"});