Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertainslie/7d8dff96912832c96de689f1dbfc2d6b to your computer and use it in GitHub Desktop.
Save robertainslie/7d8dff96912832c96de689f1dbfc2d6b to your computer and use it in GitHub Desktop.
const request = require('request');
exports.main = (event, callback) => {
let industry = event.session.properties.CONTACT.industry.value.toLowerCase();
let budget = formatBudget(event.session.properties.CONTACT.franchise_budget.value);
console.log(industry)
console.log(budget.minBudget,budget.maxBudget)
let query = {
"operationName": "franchises",
"query": "query franchises ($industry: HS_Enum, $minBudget:Number, $maxBudget:Number) { CRM {p_franchises_collection(filter: {industry__eq: $industry,initiation_fee__gte: $minBudget, initiation_fee__lte:$maxBudget }) { items {franchise_name,industry,initiation_fee,ongoing_dues}}}}",
"variables": {"industry": industry,"minBudget":budget.minBudget,"maxBudget":budget.maxBudget}
};
request({
url:`https://api.hubapi.com/collector/graphql?hapikey=${process.env.hapikey}`,
body:query,
method:"POST",
headers : {
"content-type": "application/json",
},
json: true
}, function (error, response, body) {
console.log(body.data)
let botMessage = createResponseMessage(body)
//console.log(franchises[0])
const responseJson = {
"botMessage": botMessage,
"responseExpected": false
}
callback(responseJson);
})
};
function formatBudget(budget){
if(budget == ">$100,000"){
console.log(budget)
return {
minBudget:0,
maxBudget:Number(budget.split('$')[1].replace(/\D/g, ''))
}
}
else {
console.log(budget)
return {
minBudget: Number(budget.split('-')[0].replace(/\D/g, '')),
maxBudget: Number(budget.split('-')[1].replace(/\D/g, ''))
}
}
}
function createResponseMessage(body){
if(body.data.CRM.p_franchises_collection.items.length >0){
let franchises = body.data.CRM.p_franchises_collection.items;
return `The best franchise opportunity for you is ${franchises[0]['franchise_name']} with a fee of $${franchises[0]['initiation_fee']}`
}
else {
return "We couldn't find a good recommendation but will follow up with you via email"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment