Last active
February 20, 2016 00:02
-
-
Save johnbuck/c31d1a6b72be33eb1ce1 to your computer and use it in GitHub Desktop.
This file contains 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
// Groove Access Token | |
var access_token = '[ACCESS TOKEN]]'; | |
// Get current opened and unread tickets | |
fetch( ['https://api.groovehq.com/v1/tickets?state=opened,unread&per_page=50&access_token=', access_token].join('') ) | |
.then(function(res) { | |
return res.json(); | |
}) | |
.then(function(json) { | |
var i, foundTicket = false; | |
// Try to find ticket with similar subject | |
if ( json && json.tickets ) { | |
for ( i in json.tickets ) { | |
var | |
ticket = json.tickets[i], | |
expr = new RegExp( preg_quote(ticket.title), 'i' ); | |
// Test if existing ticket has same title | |
if ( expr.test( input.subject ) ) { | |
foundTicket = ticket; | |
break; | |
} | |
} | |
} | |
var postData = {}, postString = ''; | |
// If ticket not found create new ticket | |
// else create new message for found ticket | |
if ( !foundTicket ) { | |
// Prepare data for Create New Ticket | |
postData = { | |
from: input.from, | |
to: input.to, | |
subject: input.subject, | |
body: input.body, | |
email: input.from, | |
name: input.from_name, | |
}; | |
// encoding post data | |
postString = http_build_query( postData ); | |
// New Ticket Request | |
return fetch( [ | |
'https://api.groovehq.com/v1/tickets?access_token=', access_token | |
].join(''), | |
{ | |
method: 'POST', | |
body: postString, | |
headers: { | |
'Content-Type' : 'application/x-www-form-urlencoded' | |
} | |
} ) | |
.then(function(res) { | |
return res.json(); | |
}) | |
.then(function(json) { | |
callback(null, json); | |
}) | |
.catch(callback); | |
} else { | |
// Prepare data for Create new message and assign it to current ticket | |
postData = { | |
'author' : input.from, | |
'body' : input.body | |
}; | |
// encoding post data | |
postString = http_build_query( postData ); | |
// New Message Request | |
return fetch( [ | |
'https://api.groovehq.com/v1/tickets/', foundTicket.number , | |
'/messages?access_token=', access_token | |
].join(''), | |
{ | |
method: 'POST', | |
body: postString, | |
headers: { | |
'Content-Type' : 'application/x-www-form-urlencoded' | |
} | |
} ) | |
.then(function(res) { | |
return res.json(); | |
}) | |
.then(function(json) { | |
callback(null, json); | |
}) | |
.catch(callback); | |
} | |
return []; | |
}) | |
.then(function(json) { | |
var output = json; | |
callback(null, output); | |
}) | |
.catch(callback); | |
function http_build_query(formdata, numeric_prefix, arg_separator) { | |
// discuss at: http://phpjs.org/functions/http_build_query/ | |
// original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) | |
// improved by: Legaev Andrey | |
// improved by: Michael White (http://getsprink.com) | |
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) | |
// improved by: Brett Zamir (http://brett-zamir.me) | |
// revised by: stag019 | |
// input by: Dreamer | |
// bugfixed by: Brett Zamir (http://brett-zamir.me) | |
// bugfixed by: MIO_KODUKI (http://mio-koduki.blogspot.com/) | |
// note: If the value is null, key and value are skipped in the http_build_query of PHP while in phpjs they are not. | |
// depends on: urlencode | |
// example 1: http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&'); | |
// returns 1: 'foo=bar&php=hypertext+processor&baz=boom&cow=milk' | |
// example 2: http_build_query({'php': 'hypertext processor', 0: 'foo', 1: 'bar', 2: 'baz', 3: 'boom', 'cow': 'milk'}, 'myvar_'); | |
// returns 2: 'myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&php=hypertext+processor&cow=milk' | |
var value, key, tmp = [], | |
that = this; | |
var _http_build_query_helper = function(key, val, arg_separator) { | |
var k, tmp = []; | |
if (val === true) { | |
val = '1'; | |
} else if (val === false) { | |
val = '0'; | |
} | |
if (val != null) { | |
if (typeof val === 'object') { | |
for (k in val) { | |
if (val[k] != null) { | |
tmp.push(_http_build_query_helper(key + '[' + k + ']', val[k], arg_separator)); | |
} | |
} | |
return tmp.join(arg_separator); | |
} else if (typeof val !== 'function') { | |
return encodeURIComponent(key) + '=' + encodeURIComponent(val); | |
} else { | |
throw new Error('There was an error processing for http_build_query().'); | |
} | |
} else { | |
return ''; | |
} | |
}; | |
if (!arg_separator) { | |
arg_separator = '&'; | |
} | |
for (key in formdata) { | |
value = formdata[key]; | |
if (numeric_prefix && !isNaN(key)) { | |
key = String(numeric_prefix) + key; | |
} | |
var query = _http_build_query_helper(key, value, arg_separator); | |
if (query !== '') { | |
tmp.push(query); | |
} | |
} | |
return tmp.join(arg_separator); | |
} | |
function preg_quote(str, delimiter) { | |
// discuss at: http://phpjs.org/functions/preg_quote/ | |
// original by: booeyOH | |
// improved by: Ates Goral (http://magnetiq.com) | |
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) | |
// improved by: Brett Zamir (http://brett-zamir.me) | |
// bugfixed by: Onno Marsman | |
// example 1: preg_quote("$40"); | |
// returns 1: '\\$40' | |
// example 2: preg_quote("*RRRING* Hello?"); | |
// returns 2: '\\*RRRING\\* Hello\\?' | |
// example 3: preg_quote("\\.+*?[^]$(){}=!<>|:"); | |
// returns 3: '\\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:' | |
return String(str) | |
.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment