-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (93 loc) · 3.02 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var express = require('express');
bodyParser = require('body-parser');
Realm = require('realm');
var TelstraMessaging = require('Telstra_Messaging');
var sentiment = require('node-sentiment');
var api = new TelstraMessaging.AuthenticationApi()
var clientId = ""; // {String}
var clientSecret = ""; // {String}
var grantType = "client_credentials"; // {String}
var app = express();
var server = app.listen(8080);
var io = require('socket.io')(server);
app.io = io;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
let DataSchema = {
name: 'sms',
properties: {
timestamp: 'date',
msgid: 'string',
from: 'string',
msg: 'string',
sentiment: 'float'
}
};
var smsData = new Realm({
path: 'data.sms',
schema: [DataSchema],
schemaVersion: 3,
});
function sentemoji(data){
var ranges = [
'\ud83c[\udf00-\udfff]',
'\ud83d[\udc00-\ude4f]',
'\ud83d[\ude80-\udeff]'
];
data = data.match(new RegExp(ranges.join('|'), 'g'), '');
if(data){
var ts = 0;
for (var i in data){
ts += sentiment(data[i]).score;
}
data = data.join('');
}
else{
data = "";
ts = 0;
}
return {ts: ts, emoji: data};
}
app.post('/', function (req, res) {
console.log(req.body);
var emoji = sentemoji(req.body['body']);
let msgid = req.body['messageId'],
msg = emoji.emoji,
from = req.body['from'],
score = emoji.ts,
timestamp = req.body['sentTimestamp'];
smsData.write(() => {
smsData.create('sms', {msgid: msgid, msg: msg, timestamp: req.body['sentTimestamp'], sentiment: score, from: from});
});
req.app.io.emit('score', smsData.objects('sms').sum('sentiment'));
req.app.io.emit('smsdata', {msgid: msgid, from: "04XXXXX"+from.slice(-3), msg: msg, sentiment: score});
});
app.get('/provision', function(req, res) {
var callback = function(error, data, response) {
var defaultClient = TelstraMessaging.ApiClient.instance;
var auth = defaultClient.authentications['auth'];
auth.accessToken = data.access_token;
var apiInstance = new TelstraMessaging.ProvisioningApi();
var provisionNumberRequest = new TelstraMessaging.ProvisionNumberRequest(); // ProvisionNumberRequest | A JSON payload containing the required attributes
provisionNumberRequest.notifyURL = "";
var callback = function(error, data, response) {
if (error) {
res.send(error);
} else {
console.log(data);
}
};
apiInstance.createSubscription(provisionNumberRequest, callback);
}
api.authToken(clientId, clientSecret, grantType, callback);
})
app.get('/', function(req, res) {
res.render('index.ejs');
});
io.on('connection', function (socket) {
socket.emit('score', smsData.objects('sms').sum('sentiment'));
let smss = smsData.objects('sms').sorted('timestamp', true).filtered('msg.@size > 0 and from.@size > 0');
for (var i in smss) {
socket.emit('smsdata', {msgid: smss[i].msgid, from: "04XXXXX"+smss[i].from.slice(-3), msg: smss[i].msg, sentiment: smss[i].sentiment});
}
});