Last active
August 29, 2015 14:07
-
-
Save torpio/ca4ddf3a5aca0eff35fa to your computer and use it in GitHub Desktop.
Takes a webhook from FLG 360 and adds an event to the lead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This script receives a webhook from FLG 360 and creates an event. You can choose the description of the event, it's date/time & duration and the user it should be assigned to. | |
You can use curly bracket templating to insert any {{value}} from the FLG 360 webhook, for example {{data1}}. | |
For the event duration setting, use this notation: | |
* ```15m``` (15 minutes) | |
* ```2h``` (2 hours) | |
* ```1d``` (1 day) | |
*/ | |
var settings = {}; | |
// The description of the event. | |
settings.DESCRIPTION = '' | |
// The start date/time. | |
settings.START_DATE_AND_TIME = '{{received}}' | |
// The duration of the event. For example, 1h (1 hour), 2d (2 days) or 3w (3 weeks). See below for more examples. | |
settings.EVENT_DURATION = '1h' | |
// The full name of the user that the event should be assigned to (case sensitive, exactly as shown in FLG 360). | |
// Leave BLANK to assign the event to the lead's assigned user. | |
settings.user = '' | |
if (!settings.DESCRIPTION) { | |
log.error("Please provide the description for the event in settings."); | |
} else if (!settings.START_DATE_AND_TIME) { | |
log.error("Please specify a start date/time for the event in settings."); | |
} else if (!settings.EVENT_DURATION) { | |
log.error("Please set the event's duration in settings."); | |
} else { | |
// Substitute any placeholders | |
var startDateTime = helper.template(settings.START_DATE_AND_TIME || "", request.body); | |
var description = helper.template(settings.DESCRIPTION || "", request.body); | |
var duration = helper.template(settings.EVENT_DURATION || "", request.body); | |
var user = helper.template(settings.user || "", request.body); | |
// Create the end date/time string | |
if ((duration.match(/[mhd]/))[0] === 'm') { | |
var multiplier = 1; | |
} else if ((duration.match(/[mhd]/))[0] === 'h') { | |
var multiplier = 60; | |
} else if ((duration.match(/[mhd]/))[0] === 'd') { | |
var multiplier = 1440; | |
} | |
// Deal with start date/times in the format dd/mm/yyyy | |
if (startDateTime.indexOf('/') !== -1) { | |
var startDateTimeArray = startDateTime.split(' '); | |
var startDateArray = startDateTimeArray[0].split('/'); | |
startDateTime = startDateArray[2] + '-' + startDateArray[1] + '-' + startDateArray[0] + ' ' + startDateTimeArray[1]; | |
} | |
// Deal with start date/time not having seconds | |
if ((startDateTime.match(/:/g) || []).length < 2) { | |
startDateTime = startDateTime + ':00'; | |
} | |
var minutes = (duration.match(/\d+/))[0] * multiplier * 60000; | |
var startDateSeconds = new Date(startDateTime); | |
var endDateSeconds = startDateSeconds.getTime() + minutes; | |
var endDateTime = (new Date(endDateSeconds)).toISOString(); | |
endDateTime = (endDateTime.replace('T', ' ')).split('.')[0]; | |
// Build the event create in FLG 360 | |
var input = "<data> \ | |
<request>create</request> \ | |
<type>Event</type> \ | |
<leadid>" + request.body.id + "</leadid> \ | |
<duedatetime>" + startDateTime + "</duedatetime> \ | |
<enddatetime>" + endDateTime + "</enddatetime> \ | |
<field1>" + XML.safe(description) + "</field1> \ | |
<user>" + XML.safe(user ? user : request.body.username) + "</user> \ | |
</data>"; | |
// Create the event | |
service.post('flg360', 'APIActivity.php', input, function (error, status, result) { | |
if (error) { | |
log.error("Couldn't create the event.", input, error); | |
} else if (status >= 500 || result.indexOf('<code>0</code>') === -1) { | |
log.error("Couldn't create the event.", input, result); | |
} else { | |
log.info("Successfully created the event.", result); | |
} | |
}); | |
} | |
// Install with #torpio at https://torpio.com/app/scripts/shared/view/34419 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment