Skip to content

Instantly share code, notes, and snippets.

@torpio
Created March 25, 2014 16:03
Show Gist options
  • Save torpio/9765027 to your computer and use it in GitHub Desktop.
Save torpio/9765027 to your computer and use it in GitHub Desktop.
Takes a webhook from FLG 360 and creates a record in a DXI dialler.
/*
This script receives a webhook from FLG 360 and creates a record in any dialer based on DXI. These include:
- [Difference Dialer](http://www.differencecorp.com/products-and-services/dialler-technology/)
- [Easycallnow](http://www.easycallnow.net)
By default, the lead's name, address and phone number(s) are sent across to the dialer, but you
can map more fields using 'Extra Field Mappings'. For example, to map a dialer field ```mortgageamount```
to FLG 360's ```data5``` field, use:
{
"mortgageamount": "{{data5}}"
}
By specifying an 'Id Field', the lead will be updated with the record ID from the dialer. The
field can be any of those available in FLG 360's
[Lead Create & Update API](http://flg360.uservoice.com/knowledgebase/articles/332566-api-documentation).
*/
var settings = {};
// The dataset ID that the record should be created in.
settings.DATASET = '1'
// A simple JSON object of extra field mappings. e.g. { "field": "value" }. You can use curly bracket templating to insert any {{value}} from the FLG 360 webhook, for example {{data1}}.
settings.extra_field_mappings = ""
// Optionally, the FLG 360 field that you'd like the dialer's record ID
// to be saved to ('reference' is commonly used).
settings.id_field = ''
try {
JSON.parse(settings.extra_field_mappings || '{}');
var validExtraFieldMappings = true;
} catch (error) {
var validExtraFieldMappings = false;
}
if (!validExtraFieldMappings) {
log.error("The extra field mappings doesn't seem to be valid JSON. Have you enclosed all properties in quotes?");
} else if (!settings.DATASET.match(/^\d+$/)) {
log.error("The dataset ID should be numeric.");
} else if (!settings.id_field.match(/^[a-z0-9]*$/i)) {
log.error("The ID field should be the name of the field you want to save the dialer record ID to.");
} else if (!request.body) {
log.error("You should call this script from a FLG 360 webhook. We didn't receive any data.");
} else {
// Data fields for the dialer record
var data = {
dataset: settings.DATASET,
title: request.body.title,
firstname: request.body.firstname,
lastname: request.body.lastname,
address1: request.body.address,
address2: request.body.address2,
address3: request.body.address3,
address4: request.body.towncity,
postcode: request.body.postcode,
ddi_home: request.body.phone1,
ddi_mobile: request.body.phone2
};
// Add the extra field mappings if necessary
data = _.extend(data, JSON.parse(helper.template(settings.extra_field_mappings || '{}', request.body)));
// Now build the XML to send to the dialer
var xml = "<?xml version='1.0' encoding='utf-8'?>\n";
xml += "<easycall><ecnow_data>\n";
_.each(data, function (value, key) {
xml += "<" + key + ">" + XML.safe(value) + "</" + key + ">\n";
});
xml += "</ecnow_data></easycall>\n";
// Post the lead to the dialer
service.post('dxi', 'database.php?method=ecnow_data&action=create&raw=1&format=xml', xml, function (error, status, result) {
if (error) {
log.error("Couldn't create record in dialer.", error);
script.result({ status: 500 }); // The webhook should retry
} else if (result.indexOf('<success>1</success>') === -1 || result.indexOf('<bad>0</bad>') === -1) {
log.error("Couldn't create record in dialer.", result);
script.result({ status: 500 }); // The webhook should retry
} else {
XML.parse(result, function (error, output) {
if (error) {
log.error("Couldn't understand dialer's response.", error);
script.result({ status: 500 }); // The webhook should retry
} else {
log.info("Created record in dialer.", output);
if (settings.id_field) {
// ID of the record in the dialer
id = output.result.key;
// Build the lead update in FLG 360 (to add the dialer's ID as reference)
var input = "<data>\n \
<lead>\n \
<id>" + request.body.id + "</id>\n \
<" + settings.id_field + ">" + id + "</" + settings.id_field + ">\n \
</lead>\n \
</data>";
// Update the lead
service.post('flg360', 'APILeadCreateUpdate.php', input, function (error, status, result) {
if (error) {
log.error("Couldn't update the lead.", error);
} else if (status >= 500 || result.indexOf('<status>0</status>') === -1) {
log.error("Couldn't update the lead.", result);
} else {
log.info("Added dialer's record ID to the lead.", result);
}
});
}
}
});
}
});
}
// Install with #torpio at https://torpio.com/app/scripts/shared/view/33264
/*
This script receives a webhook from FLG 360 and creates a record in any dialer based on DXI. These include:
- [Difference Dialer](http://www.differencecorp.com/products-and-services/dialler-technology/)
- [Easycallnow](http://www.easycallnow.net)
By default, the lead's name, address and phone number(s) are sent across to the dialer, but you
can map more fields using 'Extra Field Mappings'. For example, to map a dialer field ```mortgageamount```
to FLG 360's ```data5``` field, use:
{
"mortgageamount": "{{data5}}"
}
By specifying an 'Id Field', the lead will be updated with the record ID from the dialer. The
field can be any of those available in FLG 360's
[Lead Create & Update API](http://flg360.uservoice.com/knowledgebase/articles/332566-api-documentation).
*/
var settings = {};
// The dataset ID that the record should be created in.
settings.DATASET = '1'
// A simple JSON object of extra field mappings. e.g. { "field": "value" }. You can use curly bracket templating to insert any {{value}} from the FLG 360 webhook, for example {{data1}}.
settings.extra_field_mappings = ""
// Optionally, the FLG 360 field that you'd like the dialer's record ID
// to be saved to ('reference' is commonly used).
settings.id_field = ''
try {
JSON.parse(settings.extra_field_mappings || '{}');
var validExtraFieldMappings = true;
} catch (error) {
var validExtraFieldMappings = false;
}
if (!validExtraFieldMappings) {
log.error("The extra field mappings doesn't seem to be valid JSON. Have you enclosed all properties in quotes?");
} else if (!settings.DATASET.match(/^\d+$/)) {
log.error("The dataset ID should be numeric.");
} else if (!settings.id_field.match(/^[a-z0-9]*$/i)) {
log.error("The ID field should be the name of the field you want to save the dialer record ID to.");
} else if (!request.body) {
log.error("You should call this script from a FLG 360 webhook. We didn't receive any data.");
} else {
// Data fields for the dialer record
var data = {
dataset: settings.DATASET,
title: request.body.title,
firstname: request.body.firstname,
lastname: request.body.lastname,
address1: request.body.address,
address2: request.body.address2,
address3: request.body.address3,
address4: request.body.towncity,
postcode: request.body.postcode,
ddi_home: request.body.phone1,
ddi_mobile: request.body.phone2
};
// Add the extra field mappings if necessary
data = _.extend(data, JSON.parse(helper.template(settings.extra_field_mappings || '{}', request.body)));
// Now build the XML to send to the dialer
var xml = "<?xml version='1.0' encoding='utf-8'?>\n";
xml += "<easycall><ecnow_data>\n";
_.each(data, function (value, key) {
xml += "<" + key + ">" + XML.safe(value) + "</" + key + ">\n";
});
xml += "</ecnow_data></easycall>\n";
// Post the lead to the dialer
service.post('dxi', 'database.php?method=ecnow_data&action=create&raw=1&format=xml', xml, function (error, status, result) {
if (error) {
log.error("Couldn't create record in dialer.", xml, error);
script.result({ status: 500 }); // The webhook should retry
} else if (result.indexOf('<success>1</success>') === -1 || result.indexOf('<bad>0</bad>') === -1) {
log.error("Couldn't create record in dialer.", xml, result);
script.result({ status: 500 }); // The webhook should retry
} else {
XML.parse(result, function (error, output) {
if (error) {
log.error("Couldn't understand dialer's response.", xml, error);
script.result({ status: 500 }); // The webhook should retry
} else {
log.info("Created record in dialer.", output);
if (settings.id_field) {
// ID of the record in the dialer
id = output.result.key;
// Build the lead update in FLG 360 (to add the dialer's ID as reference)
var input = "<data>\n \
<lead>\n \
<id>" + request.body.id + "</id>\n \
<" + settings.id_field + ">" + id + "</" + settings.id_field + ">\n \
</lead>\n \
</data>";
// Update the lead
service.post('flg360', 'APILeadCreateUpdate.php', input, function (error, status, result) {
if (error) {
log.error("Couldn't update the lead.", input, error);
} else if (status >= 500 || result.indexOf('<status>0</status>') === -1) {
log.error("Couldn't update the lead.", input, result);
} else {
log.info("Added dialer's record ID to the lead.", result);
}
});
}
}
});
}
});
}
// Install with #torpio at https://torpio.com/app/scripts/shared/view/33264
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment