Skip to content

Instantly share code, notes, and snippets.

@taf2

taf2/action.js Secret

Last active April 15, 2022 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taf2/a021d4499204aeb32927293701d04497 to your computer and use it in GitHub Desktop.
Save taf2/a021d4499204aeb32927293701d04497 to your computer and use it in GitHub Desktop.
Super Page Transfer Call After Getting Information
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 &amp; 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