// All Actions should start with a function called output()
function output(userData, {setUserData}) {
// If the user data has already been sent, and a portal URL has been received, don't resend it
if (userData.telegra_md_portal_url) return
// @TODO: Replace with your actual company name,
// and provide the API key to Embeddables to store securely on the backend
const API_KEY_IDENTIFIER = "{{YOUR_COMPANY_NAME---telegra---api_key}}";
const INTEGRATION_CONFIGURATION_ID = "<INTEGRATION_CONFIGURATION_ID>";
const TELEGRA_MD_HOST = "https://telegramd-rest.telegramd.com";
// @TODO: Update to match your User Data keys, and add any additional data you want to send
const requestBody = {
email: userData.email,
firstName: userData.first_name,
lastName: userData.last_name,
name: userData.first_name + ' ' + userData.last_name,
gender: userData.gender,
phone: userData.phone,
requested_medications: [
{
productVariation: userData.selected_product_data.product_variation,
quantity: 1
}
],
payment_configuration: {
system: "stripe",
system_customer_identifier: userData.customer_id,
system_payment_intent_identifier: userData.payment_intent_id
},
externalIdentifier: userData.payment_intent_id,
};
console.log('[Submit to Telegra] - requestBody:', requestBody)
const url = `${TELEGRA_MD_HOST}/integrations/embeddables/${INTEGRATION_CONFIGURATION_ID}/visit/initiate?access_token=${API_KEY_IDENTIFIER}`;
// Sends POST request, using the Embeddables backend proxy to keep the access token secure
fetch(`https://proxy-secure.trysavvy.com/?url=${encodeURIComponent(url)}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(requestBody)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log("[Submit to Telegra] - Response:", data);
// Adds the portal URL to the user data, so it can be used in future Actions
setUserData({ telegra_md_portal_url: data.visitUrl })
})
.catch(error => {
console.error("[Submit to Telegra] - Error:", error);
});
}