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

@@ -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"