Last active
February 8, 2021 20:42
-
-
Save sudheendrach/07bcccae944ee11df119f321dc505cc6 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
/** | |
* @param {SpecTrack} event The track event | |
* @param {Object.<string, any>} settings Custom settings | |
* @return void | |
*/ | |
async function onTrack(event, settings) { | |
if (event['event'] === settings.eventName) { | |
let itemPurchased = | |
event['properties']['products'][0]['brand'] + | |
' ' + | |
event['properties']['products'][0]['name']; | |
const Body = | |
'Thank you for purchasing ' + | |
itemPurchased + | |
' from our site. We will follow-up with the tracking details shortly.'; | |
const To = settings.twilioDestinationNumber; | |
if (settings.twilioFrom) { | |
await sendText( | |
{ | |
From: settings.twilioFrom, | |
To, | |
Body | |
}, | |
settings | |
).catch(err => console.log(err)); | |
/* async function onIdentify(event, settings) { | |
console.log(event); | |
const Body = 'Someone new is frustrated.'; | |
const To = settings.twilioDestinationNumber; | |
if (settings.twilioFrom) { | |
await sendText( | |
{ | |
From: settings.twilioFrom, | |
To, | |
Body | |
}, | |
settings | |
); | |
} | |
} | |
/* await phoneCall( | |
{ | |
From: settings.twilioFrom, | |
To, | |
// Learn more at: https://www.twilio.com/docs/voice/twiml | |
Twiml: ` | |
<Response> | |
<Break strength="x-weak" time="1000ms"/> | |
<Say>${Body}</Say> | |
</Response> | |
` | |
}, | |
settings | |
);*/ | |
} | |
if (settings.twilioWhatsAppFrom) { | |
// Learn more at: https://www.twilio.com/docs/whatsapp | |
await sendText( | |
{ | |
To: 'whatsapp:' + To, | |
From: settings.twilioWhatsAppFrom, | |
Body | |
}, | |
settings | |
); | |
} | |
} | |
} | |
/** | |
* Sends SMS or WhatsApp message with Twilio | |
* | |
* https://www.twilio.com/docs/sms | |
* https://www.twilio.com/docs/whatsapp | |
* | |
*/ | |
async function sendText(params, settings) { | |
const endpoint = `https://api.twilio.com/2010-04-01/Accounts/${settings.twilioAccountId}/Messages.json`; | |
await fetch(endpoint, { | |
method: 'POST', | |
headers: { | |
Authorization: `Basic ${btoa( | |
settings.twilioAccountId + ':' + settings.twilioToken | |
)}`, | |
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' | |
}, | |
body: toFormParams(params) | |
}); | |
} | |
/** | |
* Places a Twilio phone call. | |
* | |
* https://www.twilio.com/docs/voice | |
* | |
*/ | |
/*async function phoneCall(params, settings) { | |
const endpoint = `https://api.twilio.com/2010-04-01/Accounts/${settings.twilioAccountId}/Calls.json`; | |
await fetch(endpoint, { | |
method: 'POST', | |
headers: { | |
Authorization: `Basic ${btoa( | |
settings.twilioAccountId + ':' + settings.twilioToken | |
)}`, | |
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' | |
}, | |
body: toFormParams(params) | |
}); | |
}*/ | |
function toFormParams(params) { | |
return Object.entries(params) | |
.map(([key, value]) => { | |
const paramName = encodeURIComponent(key); | |
const param = encodeURIComponent(value); | |
return `${paramName}=${param}`; | |
}) | |
.join('&'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment