implement actionHook for message verb

This commit is contained in:
Dave Horton
2021-09-22 13:28:56 -04:00
parent ac263de729
commit f0bd681ccc

View File

@@ -10,7 +10,7 @@ class TaskMessage extends Task {
this.payload = { this.payload = {
message_sid: this.data.message_sid || uuidv4(), message_sid: this.data.message_sid || uuidv4(),
provider: this.data.provider, carrier: this.data.carrier,
to: this.data.to, to: this.data.to,
from: this.data.from, from: this.data.from,
text: this.data.text text: this.data.text
@@ -27,16 +27,18 @@ class TaskMessage extends Task {
const {srf, accountSid} = cs; const {srf, accountSid} = cs;
const {res} = cs.callInfo; const {res} = cs.callInfo;
let payload = this.payload; let payload = this.payload;
const actionParams = {message_sid: this.payload.message_sid};
await super.exec(cs); await super.exec(cs);
try { try {
const {getSmpp, dbHelpers} = srf.locals; const {getSmpp, dbHelpers} = srf.locals;
const {lookupSmppGateways} = dbHelpers; const {lookupSmppGateways} = dbHelpers;
this.logger.info(`looking up gateways for account_sid: ${accountSid}`); this.logger.debug(`looking up gateways for account_sid: ${accountSid}`);
const r = await lookupSmppGateways(accountSid); const r = await lookupSmppGateways(accountSid);
let gw, url, relativeUrl; let gw, url, relativeUrl;
if (r.length > 0) { if (r.length > 0) {
gw = r.find((o) => 1 === o.sg.outbound && (!this.payload.provider || o.vc.name === this.payload.provider)); gw = r.find((o) => 1 === o.sg.outbound && (!this.payload.carrier || o.vc.name === this.payload.carrier));
} }
if (gw) { if (gw) {
this.logger.info({gw, accountSid}, 'Message:exec - using smpp to send message'); this.logger.info({gw, accountSid}, 'Message:exec - using smpp to send message');
@@ -51,21 +53,34 @@ class TaskMessage extends Task {
else { else {
//TMP: smpp only at the moment, need to add http back in //TMP: smpp only at the moment, need to add http back in
/* /*
this.logger.info({gw, accountSid, provider: this.payload.provider}, this.logger.info({gw, accountSid, carrier: this.payload.carrier},
'Message:exec - no smpp gateways found to send message'); 'Message:exec - no smpp gateways found to send message');
relativeUrl = 'v1/outboundSMS'; relativeUrl = 'v1/outboundSMS';
const sbcAddress = getSBC(); const sbcAddress = getSBC();
if (sbcAddress) url = `http://${sbcAddress}:3000/`; if (sbcAddress) url = `http://${sbcAddress}:3000/`;
*/ */
return res.sendStatus(404); this.performAction({
...actionParams,
message_status: 'no carriers'
}).catch((err) => {});
if (res) res.sendStatus(404);
return;
} }
if (url) { if (url) {
const post = bent(url, 'POST', 'json', 201, 480); const post = bent(url, 'POST', 'json', 201, 480);
this.logger.info({payload, url}, 'Message:exec sending outbound SMS'); this.logger.info({payload, url}, 'Message:exec sending outbound SMS');
const response = await post(relativeUrl, payload); const response = await post(relativeUrl, payload);
if (response.smpp_err_code) { const {smpp_err_code, carrier, message_id, message} = response;
this.logger.info({response}, 'Error sending SMS'); if (smpp_err_code) {
if (cs.callInfo.res) { this.logger.info({response}, 'SMPP error sending SMS');
this.performAction({
...actionParams,
carrier,
carrier_message_id: message_id,
message_status: 'failure',
message_failure_reason: message
}).catch((err) => {});
if (res) {
res.status(480).json({ res.status(480).json({
...response, ...response,
sid: cs.callInfo.messageSid sid: cs.callInfo.messageSid
@@ -73,25 +88,38 @@ class TaskMessage extends Task {
} }
} }
else { else {
const {message_id, carrier} = response;
this.logger.info({response}, 'Successfully sent SMS'); this.logger.info({response}, 'Successfully sent SMS');
if (cs.callInfo.res) { this.performAction({
this.logger.info('Message:exec sending 200 OK response to HTTP POST from api server'); ...actionParams,
carrier,
carrier_message_id: message_id,
message_status: 'success',
}).catch((err) => {});
if (res) {
res.status(200).json({ res.status(200).json({
sid: cs.callInfo.messageSid, sid: cs.callInfo.messageSid,
providerResponse: response carrierResponse: response
}); });
} }
} }
// TODO: action Hook
} }
else { else {
this.logger.info('Message:exec - unable to send SMS as there are no available SMS gateways'); this.logger.info('Message:exec - unable to send SMS as SMPP is not configured on the system');
res.status(422).json({message: 'no configured SMS gateways'}); this.performAction({
...actionParams,
message_status: 'smpp configuration error'
}).catch((err) => {});
if (res) res.status(404).json({message: 'no configured SMS gateways'});
} }
} catch (err) { } catch (err) {
this.logger.error(err, 'TaskMessage:exec - Error sending SMS'); this.logger.error(err, 'TaskMessage:exec - unexpected error sending SMS');
res.status(422).json({message: 'no configured SMS gateways'}); this.performAction({
...actionParams,
message_status: 'system error',
message_failure_reason: err.message
});
if (res) res.status(422).json({message: 'no configured SMS gateways'});
} }
} }
} }