Using Custom IO
This page describes how to create a custom Integrated Onboarding trigger on any frontend framework
Requirements
Direct-Paid Accounts
Implementation
2
Open Onboarding Pop-Up
// `processParams` function retrieves the current URL search parameters and posts them to the parent window.
// If there is an `opener` window, the function posts the parameters to it and closes the current window.
function processParams() {
const params = window.location.search; // retrieve the current URL search parameters
// Check if there is an opener window
if (window.opener) {
window.opener.postMessage(params); // post the parameters to the opener window
window.close(); // close the current window
}
}
// `window.onload` event is used to trigger the execution of the `processParams` function
// when the page has finished loading.
window.onload = function() {
processParams();
};
// `open360DialogPopup` function opens a new window with the specified URL and options
// and adds a message event listener to the current window.
function open360DialogPopup(baseUrl) {
window.removeEventListener("message", receiveMessage); // remove any existing message event listeners
// Window options to be used in opening the new window
const windowFeatures = "toolbar=no, menubar=no, width=600, height=900, top=100, left=100";
const partnerId = "yourPartnerId";
const redirectUrl = "yourRedirectUrl"; // additional redirect if needed - if you don't want to use your
// previously set partner redirect
// Open the new window with the specified URL
open(
"https://hub.360dialog.com/dashboard/app/" + partnerId + "/permissions?redirect_url=" + redirectUrl,
"integratedOnboardingWindow",
windowFeatures
);
// Add a message event listener to the current window
window.addEventListener("message", (event) => receiveMessage(event, baseUrl), false);
}
// `receiveMessage` function is the callback function that is executed when the message event is triggered.
// It retrieves the data from the event, sets it as the search parameters of the current URL,
// and returns if the origin of the event is not the same as the `baseUrl` or the type of `event.data` is an object.
const receiveMessage = (event, baseUrl) => {
// Check if the event origin is not the same as `baseUrl` or `event.data` is an object.
if (event.origin != baseUrl || typeof event.data === "object") {
return;
}
const { data } = event; // retrieve the data from the event
const redirectUrl = `${data}`; // create a redirect URL from the data
window.location.search = redirectUrl; // set the redirect URL as the search parameters of the current URL
};3
Consume Redirect
Parameter in query
Description
window.addEventListener(
"message",
(event) => {
const { data } = event;
const queryString = `${data}`;
console.log(queryString);
// ?client=oaY9LLfUCL&channels=[y9MiLoCH]
}, false
);let params = new URLSearchParams(queryString);
let channels = params.get("channels");
console.log(channels)
// [y9MiLoCH]
console.log(client)
// oaY9LLfUCLfunction processParams() {
const params = window.location.search;
if (window.opener) {
window.opener.postMessage(params);
window.close();
}
}
window.onload = function() {
processParams();
}4
Handle Webhooks
// Example Express.js webhook handler
app.post('/webhooks/360dialog', express.json(), (req, res) => {
const event = req.body;
switch (event.type) {
case 'channel_created':
console.log(`New channel created: ${event.payload.channel}`);
break;
case 'channel_running':
console.log(`Channel ${event.payload.channel} is now active!`);
break;
case 'phone_number_quality_changed':
console.log(`Channel ${event.payload.channel} messaging limit: ${event.payload.value}`);
break;
}
// Always respond with 200 to acknowledge receipt
res.status(200).send();
});Last updated
Was this helpful?
