-
-
Save nettofarah/c316cfe3a120ac220a4b6621472665e7 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
async function onTrack(event, settings) { | |
if (event.event !== settings.eventName) { | |
return; | |
} | |
const product = event.properties.products[0]; | |
const itemPurchased = `${product.brand} ${product.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 | |
); | |
} | |
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 | |
* | |
*/ | |
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), | |
}); | |
} | |
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