-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextbot.js
More file actions
76 lines (58 loc) · 2.04 KB
/
Copy pathtextbot.js
File metadata and controls
76 lines (58 loc) · 2.04 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
/*
This Avaya CPaaS application accepts incoming SMS text messages and sends the received message back to the sender.
It can be easly enhanced to provide some form of bot processing.
*/
const express = require('express');
const bodyParser = require('body-parser');
const cpaas = require('@avaya/cpaas'); //Avaya cloud
var enums = cpaas.enums;
var ix = cpaas.inboundXml;
const URL_PORT = 5055;
var app = express();
// Middleware to parse JSON
app.use(bodyParser.urlencoded({
extended : true
}));
app.use(bodyParser.json());
// Tell server to listen on port
var server = app.listen(URL_PORT, function () {
var host = server.address().address;
var port = server.address().port;
console.log("SMS Virtual Agent is listening on port %s", port)
});
// Entry point for sms text
app.post('/cpaas-sms/', function (req, res) {
processText(req.body.From, req.body.To, req.body.Body, res);
});
async function processText(from, to, body, res) {
var prompt = body;
// Do something with the incoming message body and set prompt to the response text message body
// For example, call Google Dialogflow to process messsage and set prompt to Intent Response
console.log(body);
returnTextResponse(prompt, from, to, res);
}
async function returnTextResponse(prompt, from, to, res) {
var xmlDefinition = generateXMLText(from, to, prompt)
var serverResponse = await buildCPaaSResponse(xmlDefinition);
res.type('application/xml');
res.send(serverResponse);
}
function generateXMLText(customer , cpaas , body) {
var sms = ix.sms({
text : body ,
to : customer ,
from : cpaas
});
var xml_content = [];
xml_content.push(sms);
var xmlDefinition = ix.response({content: xml_content});
return xmlDefinition;
}
async function buildCPaaSResponse(xmlDefinition) {
var result = await ix.build(xmlDefinition).then(function(xml){
return xml;
}).catch(function(err){
console.log('The generated XML is not valid!', err);
});
return result;
}