add sms messaging support

This commit is contained in:
Dave Horton
2020-10-09 08:00:17 -04:00
parent 950f1c83b7
commit c02aa94500
12 changed files with 689 additions and 590 deletions

View File

@@ -0,0 +1,35 @@
const router = require('express').Router();
const CallInfo = require('../../session/call-info');
const {CallDirection} = require('../../utils/constants');
const SmsSession = require('../../session/sms-call-session');
const normalizeJambones = require('../../utils/normalize-jambones');
const makeTask = require('../../tasks/make_task');
router.post('/:sid', async(req, res) => {
const {logger} = req.app.locals;
const {srf} = req.app.locals;
const {messageSid} = req.body;
logger.debug({body: req.body}, 'got createMessage request');
const data = [Object.assign({verb: 'message'}, req.body)];
delete data[0].messageSid;
try {
const tasks = normalizeJambones(logger, data)
.map((tdata) => makeTask(logger, tdata));
const callInfo = new CallInfo({
direction: CallDirection.None,
messageSid,
accountSid: req.params.sid,
res
});
const cs = new SmsSession({logger, srf, tasks, callInfo});
cs.exec();
} catch (err) {
logger.error({err, body: req.body}, 'OutboundSMS: error launching SmsCallSession');
}
});
module.exports = router;

View File

@@ -6,6 +6,9 @@ api.use('/conference', require('./conference'));
api.use('/dequeue', require('./dequeue'));
api.use('/enqueue', require('./enqueue'));
api.use('/messaging', require('./messaging')); // inbound SMS
api.use('/createMessage', require('./create-message')); // outbound SMS (REST)
// health checks
api.get('/', (req, res) => res.sendStatus(200));
api.get('/health', (req, res) => res.sendStatus(200));

View File

@@ -0,0 +1,71 @@
const router = require('express').Router();
const Requestor = require('../../utils/requestor');
const CallInfo = require('../../session/call-info');
const {CallDirection} = require('../../utils/constants');
const SmsSession = require('../../session/sms-call-session');
const normalizeJambones = require('../../utils/normalize-jambones');
const {TaskPreconditions} = require('../../utils/constants');
const makeTask = require('../../tasks/make_task');
router.post('/:partner', async(req, res) => {
const {logger} = req.app.locals;
logger.debug({body: req.body}, `got incomingSms request from partner ${req.params.partner}`);
let tasks;
const app = req.body.app;
const hook = app.messaging_hook;
const requestor = new Requestor(logger, hook);
const payload = {
provider: req.params.partner,
messageSid: app.messageSid,
accountSid: app.accountSid,
applicationSid: app.applicationSid,
from: req.body.from,
to: req.body.to,
cc: req.body.cc,
text: req.body.text,
media: req.body.media
};
res.status(200).json({sid: req.body.messageSid});
try {
tasks = await requestor.request(hook, payload);
logger.info({tasks}, 'response from incoming SMS webhook');
} catch (err) {
logger.error({err, hook}, 'Error sending incoming SMS message');
return;
}
// process any versb in response
if (Array.isArray(tasks) && tasks.length) {
const {srf} = req.app.locals;
app.requestor = requestor;
app.notifier = {request: () => {}};
try {
tasks = normalizeJambones(logger, tasks)
.map((tdata) => makeTask(logger, tdata))
.filter((t) => t.preconditions === TaskPreconditions.None);
if (0 === tasks.length) {
logger.info('inboundSMS: after removing invalid verbs there are no tasks left to execute');
return;
}
const callInfo = new CallInfo({
direction: CallDirection.None,
messageSid: app.messageSid,
accountSid: app.accountSid,
applicationSid: app.applicationSid
});
const cs = new SmsSession({logger, srf, application: app, tasks, callInfo});
cs.exec();
} catch (err) {
logger.error({err, tasks}, 'InboundSMS: error launching SmsCallSession');
}
}
});
module.exports = router;

View File

@@ -44,6 +44,14 @@ class CallInfo {
this.callStatus = CallStatus.Trying,
this.sipStatus = 100;
}
else if (this.direction === CallDirection.None) {
// outbound SMS
const {messageSid, accountSid, applicationSid, res} = opts;
this.messageSid = messageSid;
this.accountSid = accountSid;
this.applicationSid = applicationSid;
this.res = res;
}
else {
// outbound call triggered by REST
const {req, accountSid, applicationSid, to, tag} = opts;

View File

@@ -34,18 +34,20 @@ class CallSession extends Emitter {
this.srf = srf;
this.callInfo = callInfo;
this.tasks = tasks;
this.updateCallStatus = srf.locals.dbHelpers.updateCallStatus;
this.serviceUrl = srf.locals.serviceUrl;
this.taskIdx = 0;
this.stackIdx = 0;
this.callGone = false;
this.tmpFiles = new Set();
// if this is a ConfirmSession
if (!this.isConfirmCallSession) sessionTracker.add(this.callSid, this);
if (!this.isSmsCallSession) {
this.updateCallStatus = srf.locals.dbHelpers.updateCallStatus;
this.serviceUrl = srf.locals.serviceUrl;
}
if (!this.isConfirmCallSession && !this.isSmsCallSession) {
sessionTracker.add(this.callSid, this);
}
}
/**
@@ -66,7 +68,7 @@ class CallSession extends Emitter {
* SIP call-id for the call
*/
get callId() {
return this.callInfo.direction;
return this.callInfo.callId;
}
/**
@@ -166,6 +168,13 @@ class CallSession extends Emitter {
return this.constructor.name === 'ConfirmCallSession';
}
/**
* returns true if this session is a SmsCallSession
*/
get isSmsCallSession() {
return this.constructor.name === 'SmsCallSession';
}
/**
* execute the tasks in the CallSession. The tasks are executed in sequence until
* they complete, or the caller hangs up.
@@ -201,7 +210,7 @@ class CallSession extends Emitter {
this._onTasksDone();
this._clearResources();
if (!this.isConfirmCallSession) sessionTracker.remove(this.callSid);
if (!this.isConfirmCallSession && !this.isSmsCallSession) sessionTracker.remove(this.callSid);
}
trackTmpFile(path) {

View File

@@ -0,0 +1,22 @@
const CallSession = require('./call-session');
/**
* @classdesc Subclass of CallSession. Represents a CallSession
* that is established for the purpose of sending an outbound SMS
* @extends CallSession
*/
class SmsCallSession extends CallSession {
constructor({logger, application, srf, tasks, callInfo}) {
super({
logger,
application,
srf,
tasks,
callInfo
});
}
}
module.exports = SmsCallSession;

View File

@@ -39,6 +39,9 @@ function makeTask(logger, obj, parent) {
case TaskName.Leave:
const TaskLeave = require('./leave');
return new TaskLeave(logger, data, parent);
case TaskName.Message:
const TaskMessage = require('./message');
return new TaskMessage(logger, data, parent);
case TaskName.Say:
const TaskSay = require('./say');
return new TaskSay(logger, data, parent);

57
lib/tasks/message.js Normal file
View File

@@ -0,0 +1,57 @@
const Task = require('./task');
const {TaskName, TaskPreconditions} = require('../utils/constants');
const bent = require('bent');
class TaskMessage extends Task {
constructor(logger, opts) {
super(logger, opts);
this.preconditions = TaskPreconditions.None;
this.payload = {
provider: this.data.provider,
to: this.data.to,
from: this.data.from,
cc: this.data.cc,
text: this.data.text,
media: this.data.media
};
}
get name() { return TaskName.Message; }
/**
* Send outbound SMS
*/
async exec(cs, dlg) {
const {srf} = cs;
await super.exec(cs);
try {
const {getSBC} = srf.locals;
const sbcAddress = getSBC();
if (sbcAddress) {
const url = `http://${sbcAddress}:3000/`;
const post = bent(url, 'POST', 'json', 200);
this.logger.info({payload: this.payload, sbcAddress}, 'Message:exec sending outbound SMS');
const response = await post('v1/outboundSMS', this.payload);
this.logger.info({response}, 'Successfully sent SMS');
if (cs.callInfo.res) {
this.logger.info('Message:exec sending 200 OK response to HTTP POST from api server');
cs.callInfo.res.status(200).json({
sid: cs.callInfo.messageSid,
providerResponse: response
});
}
// TODO: action Hook
}
else {
this.logger.info('Message:exec - unable to send SMS as there are no available SBCs');
}
} catch (err) {
this.logger.error(err, 'TaskMessage:exec - Error sending SMS');
}
}
}
module.exports = TaskMessage;

View File

@@ -164,6 +164,20 @@
"url"
]
},
"message": {
"properties": {
"provider": "string",
"to": "string",
"from": "string",
"text": "string",
"media": "string|array",
"actionHook": "object|string"
},
"required": [
"to",
"from"
]
},
"pause": {
"properties": {
"length": "number"

View File

@@ -9,6 +9,7 @@
"Hangup": "hangup",
"Leave": "leave",
"Listen": "listen",
"Message": "message",
"Pause": "pause",
"Play": "play",
"Redirect": "redirect",
@@ -34,7 +35,8 @@
},
"CallDirection": {
"Inbound": "inbound",
"Outbound": "outbound"
"Outbound": "outbound",
"None": "none"
},
"ListenStatus": {
"Pause": "pause",

1030
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -26,8 +26,8 @@
"jslint": "eslint app.js lib"
},
"dependencies": {
"@jambonz/db-helpers": "^0.4.2",
"@jambonz/realtimedb-helpers": "^0.2.16",
"@jambonz/db-helpers": "^0.5.1",
"@jambonz/realtimedb-helpers": "^0.2.17",
"@jambonz/stats-collector": "^0.0.4",
"bent": "^7.3.9",
"cidr-matcher": "^2.1.1",
@@ -47,9 +47,10 @@
"devDependencies": {
"blue-tape": "^1.0.0",
"clear-module": "^4.1.1",
"eslint": "^7.7.0",
"eslint": "^7.10.0",
"eslint-plugin-promise": "^4.2.1",
"nyc": "^15.1.0",
"tap-dot": "^2.0.0",
"tap-spec": "^5.0.0"
}
}