Share feedback, ideas and get community help

Updated 10 months ago

Need help with typebot scripts

At a glance

The community member is trying to run a script on Typebot that fetches data from an API and sets variables based on the response. The script uses setVariable to log information to the console and set the agent_email variable.

In the comments, other community members point out that the way setVariable is used in the script is incorrect, and that fetch does not work the same way on the server-side as it does on the client-side. They provide links to the Typebot documentation for more information.

After investigating, the community member found that using setVariable with client-side execution is better, but they now have a CORS issue. Another community member notes that the community member does not have access to setVariable on the client-side.

There is no explicitly marked answer in the comments.

Useful resources
setVariable({{console_log}}, 'Console Log')

const accessTokens = {
'hussan.hussain@maqsad.io': 'token'
};

const apiUrl = 'https://chatwoot.com/api/v1/accounts/2/conversations/meta?labels="mdcat-landing-page-get-guidance-now"&status=open';

let foundEmail = null;

for (const email in accessTokens) {
if (accessTokens.hasOwnProperty(email)) {
const token = accessTokens[email];
try {
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'api_access_token': ${token}
}
});

if (!response.ok) {
setVariable({{console_log}}, HTTP error! Status: ${response.status})
throw new Error(HTTP error! Status: ${response.status});
}

const data = await JSON.parse();

if (data.meta && data.meta.mine_count < 200) {
foundEmail = email;
setVariable({{console_log}}, Found email: ${email} with mine_count: ${data.meta.mine_count})
break; // Exit the loop once the condition is met
}
} catch (error) {
setVariable({{console_log}}, Error fetching data for email ${email}: ${error}})
throw new Error(Error fetching data for email ${email}:, error);
}
} else {
setVariable({{console_log}}, Own property not found)
}
}

if (foundEmail) {
setVariable({{agent_email}}, foundEmail)
setVariable({{console_log}}, Email with mine_count < 200: ${foundEmail})
} else {
setVariable({{console_log}}, No email found with mine_count < 200)
}

i am trying to run this script on typebot and would appreciate if someone can tell me what i am doing wrong?
B
A
3 comments
Ok so after investigating i found out that using setVariable block with client side execution is better but now i have run into CORS issue...
You don't have access to setVariable on the client side
Add a reply
Sign up and join the conversation on Discord