@apidaze/node-red-contrib-apidaze
Version:
Node-RED module for Apidaze
62 lines (49 loc) • 1.7 kB
JavaScript
module.exports = function (RED) {
'use strict';
const operations = {
contains: (value1, value2) => value1.toString().includes(value2.toString()),
notContains: (value1, value2) =>
!value1.toString().includes(value2.toString()),
equal: (value1, value2) => value1 === value2,
notEqual: (value1, value2) => value1 !== value2,
regex: (value1, value2) => {
const regexMatch = value2
.toString()
.match(new RegExp('^/(.*?)/([gimy]*)$'));
let regex;
if (!regexMatch) {
regex = new RegExp(value2.toString());
} else if (regexMatch.length === 1) {
regex = new RegExp(regexMatch[1]);
} else {
regex = new RegExp(regexMatch[1], regexMatch[2]);
}
return !!value1.toString().match(regex);
},
};
function DTMFHandler(config) {
RED.nodes.createNode(this, config);
this.operation = config.operation;
this.dialedDigits = config.dialedDigits;
this.on('input', async (msg, send, done) => {
const { payload } = msg;
if (!payload.call || !payload.call.call_uuid) {
return send(null);
}
const { dialed_digits: dialedDigits } = payload.call;
const noDTMF = typeof dialedDigits === 'undefined';
const emptyDTMF = typeof dialedDigits === 'string' && dialedDigits.length === '';
if (noDTMF || emptyDTMF) {
return send([null, null, msg]);
}
const result = operations[this.operation](dialedDigits, this.dialedDigits);
if (result) {
send([msg, null, null])
} else {
send([null, msg, null])
}
done();
});
}
RED.nodes.registerType('dtmf-handler', DTMFHandler);
};