-
-
Save taf2/a021d4499204aeb32927293701d04497 to your computer and use it in GitHub Desktop.
Super Page Transfer Call After Getting Information
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
const Mustache = require('mustache'); | |
exports.handler = function(event, context, callback) { | |
let activity = event.activity; | |
// define our client side code here for easier debugging | |
let script = function() { | |
let form = document.getElementById('form'); | |
console.log("execute the user script", form); | |
form.addEventListener('submit', function(e) { e.preventDefault(); | |
// save the field data | |
console.log("save the form via CTM API"); | |
let zip = document.getElementById('zip').value; | |
CTM.invoke('LDA8CA4483EED6CEF9E733131272ED71AB06B66737BACF304D7', {input: zip}).then(function(res) { | |
console.log("saved successfully", res); | |
let output = res.data[1].data; | |
document.getElementById('output').innerHTML = '<p>' + output.replace(/say:Joanna:en-US:/,'') + '</p>'; | |
}).catch(function(err) { | |
console.error("Something went wrong"); | |
}); | |
}, false); | |
document.getElementById("transfer-call").addEventListener( "click", function(e) { e.preventDefault(); | |
let data = { | |
transfer_type: 'transfer', route_to: 'menu', to_menu: 'VOM93F974B7707B2467A8BEC831DBBCCECA4F29FF0C7C677081' | |
}; | |
console.log("transfer data:", data); | |
let url = `https://app.calltrackingmetrics.com/api/v1/accounts/${CTX.Call.account_id}/calls/${CTX.Call.id}/transfer.json`; | |
console.log("transfer url: ", url, CTX); | |
fetch(url, { | |
method: 'POST', mode: 'cors', cache: 'no-cache', credentials: 'include', | |
headers: { 'Content-Type': 'application/json' }, | |
redirect: 'follow', referrerPolicy: 'no-referrer', body: JSON.stringify(data) | |
}).then(function(response) { | |
response.json().then(function(res) { | |
console.log("response data:", res) | |
}); | |
}); | |
}, false); | |
}.toString(); | |
activity.isCallLive = activity.status == 'in-progress'; | |
const response = { | |
statusCode: 200, | |
contentType: 'text/html', | |
script: `const CTX={Call:${JSON.stringify(activity)}}; (${script})()`, | |
body: Mustache.to_html(` | |
<h3>Contact: ${activity.name} </h3> | |
<form id='form'> | |
<div class="field"> | |
<label>Zip Code</label> | |
{{#activity.postal_code}} | |
<input class='text' type='text' id='zip' name='zip' value='{{activity.postal_code}}'/> | |
{{/activity.postal_code}} | |
{{#isCallLive}}<a class="button" id="transfer-call"> Transfer Call </a>{{/isCallLive}} | |
<a target="_blank" href="https://www.google.com/">search for it</a> | |
</div> | |
<div id="output" class='card'></div> | |
<footer> | |
<input type='submit' class='button' value='Lookup Weather & Transfer'/> | |
</footer> | |
</form> | |
`, {activity: activity}), | |
}; | |
context.done(null, response); | |
console.log("the code ran"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment