-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
67 lines (51 loc) · 1.6 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
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Simple Emotion Analysis Demo:
* Read incoming SMS messages and have Watson to analyze the messages
*/
'use strict';
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const ToneAnalyzerV3 = require('ibm-watson/tone-analyzer/v3');
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`);
});
// 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());
// IBM Watson Tone Analysis
var toneAnalyzer = new ToneAnalyzerV3({
iam_apikey: process.env.TONE_ANALYZER_IAM_APIKEY,
url: process.env.TONE_ANALYZER_URL,
version: '2017-09-21'
});
function analyzeTone(params) {
let toneParams = {
tone_input: { 'text': params.text},
content_type: 'application/json',
};
toneAnalyzer.tone(toneParams)
.then(toneAnalyzer => {
console.log(JSON.stringify(toneAnalyzer, null, 2));
})
.catch(err => {
console.log('error', err);
});
}