Script to determinate Country from phone number

If Phone has the number of visitor, how can save in variable Country the country , based on phone number? I tried this code but it does not work // Obtener el número de teléfono de la variable Typebot let phone = "{{Phone}}"; // Función para determinar el país basado en los dos primeros dígitos del número de teléfono function determineCountry(phoneNumber) { let countryCode = phoneNumber.substring(0, 2); let country; switch (countryCode) { case "54": country = "Argentina"; break; case "59": country = "Uruguay"; break; case "52": country = "Mexico"; break; case "1": country = "Usa"; break; default: country = "UNKKNOWN"; break; } return country; } // Determinar el país y guardarlo en la variable countryfinal let countryfinal = determineCountry(phone); // Establecer la variable country en Typebot setVariable('country', countryfinal); Because The country is {{Country}} , always shows UNKNOWN
16 Replies
Hall
Hall4mo ago
Someone will reply to you shortly. In the meantime, this might help: -# This post was marked as solved by Baptiste. View answer.
altomarketing
altomarketingOP4mo ago
THE AI , give me this code, but still does not work
let phone = "{{Phone}}".replace(/\D/g, ''); let countryCode; if (phone.startsWith("+")) { phone = phone.substring(1); } function determineCountry(phoneNumber) { let country; if (phoneNumber.startsWith("54")) { country = "Argentina"; } else if (phoneNumber.startsWith("59")) { country = "Uruguay"; } else if (phoneNumber.startsWith("52")) { country = "Mexico"; } else if (phoneNumber.startsWith("1")) { country = "USA"; } else { country = "UNKNOWN"; } return country; } let countryfinal = determineCountry(phone); setVariable('country', countryfinal);
Mario Barretta
Mario Barretta4mo ago
Try with this (just the result with a deeper inspection by deepseek)
// Obtener el número de teléfono de la variable Typebot y evitar valores nulos let phone = "{{Phone}}" || ""; // Evita valores nulos phone = phone.replace(/\D/g, ''); // Elimina todos los caracteres no numéricos // Elimina prefijos como "+" o "00" if (phone.startsWith("00")) { phone = phone.substring(2); // Elimina "00" si está en formato internacional } else if (phone.startsWith("+")) { phone = phone.substring(1); // Elimina "+" } // Función para determinar el país basado en el prefijo function determineCountry(phoneNumber) { let country; // Comprueba los códigos internacionales if (phoneNumber.startsWith("54")) { country = "Argentina"; } else if (phoneNumber.startsWith("598")) { // Uruguay tiene un prefijo de 3 dígitos country = "Uruguay"; } else if (phoneNumber.startsWith("52")) { country = "Mexico"; } else if (phoneNumber.startsWith("1")) { // USA y Canadá country = "USA"; } else { country = "UNKNOWN"; // Por defecto } return country; } // Determina el país y guarda el resultado let countryfinal = determineCountry(phone); // Establece la variable 'country' en Typebot setVariable('country', countryfinal); // Depuración para verificar los datos console.log("Phone:", phone); // Muestra el número limpio console.log("Country:", countryfinal); // Muestra el país detectado
Baptiste
Baptiste4mo ago
If you are setting one variable only, use a Set variable block with a return value Here is a valid code:
let phone = {{Phone}}.replace(/\D/g, "");
let countryCode;

if (phone.startsWith("+")) {
phone = phone.substring(1);
}

function determineCountry(phoneNumber) {
let country;
if (phoneNumber.startsWith("54")) {
country = "Argentina";
} else if (phoneNumber.startsWith("59")) {
country = "Uruguay";
} else if (phoneNumber.startsWith("52")) {
country = "Mexico";
} else if (phoneNumber.startsWith("1")) {
country = "USA";
} else {
country = "UNKNOWN";
}
return country;
}

return determineCountry(phone);
let phone = {{Phone}}.replace(/\D/g, "");
let countryCode;

if (phone.startsWith("+")) {
phone = phone.substring(1);
}

function determineCountry(phoneNumber) {
let country;
if (phoneNumber.startsWith("54")) {
country = "Argentina";
} else if (phoneNumber.startsWith("59")) {
country = "Uruguay";
} else if (phoneNumber.startsWith("52")) {
country = "Mexico";
} else if (phoneNumber.startsWith("1")) {
country = "USA";
} else {
country = "UNKNOWN";
}
return country;
}

return determineCountry(phone);
altomarketing
altomarketingOP4mo ago
I dont know what im doing wrong.. but it prints all code instead of executing it
No description
Baptiste
Baptiste4mo ago
Copy paste your code here there is most likely a syntax issue with it
altomarketing
altomarketingOP4mo ago
attaching now the bot.. error in test is TypeError: bernhrdrreyefbsnxbw6l1l6.replace is not a function
michaelvips
michaelvips4mo ago
much more easy use a api, send the phone number to a phone number api checker like abstractapi, then get only what you need.
altomarketing
altomarketingOP4mo ago
why should i pay and relay on an external api if i only want to define a variable checking only if it starts in first 2 numbers ? any advice ?
Baptiste
Baptiste4mo ago
I was talking about the script code content But with the error message I can already guess what's wrong You most likely do: "{{Phone}}".replace(/\D/g, "") instead of {{Phone}}.replace(/\D/g, "") See the 2nd Info box at the top of this page: https://docs.typebot.io/editor/blocks/logic/script#script-block
altomarketing
altomarketingOP4mo ago
See the image, i did {{Phone}}.replace(/\D/g, "") i did copy and paste your code..
Baptiste
Baptiste4mo ago
Ok that's because you are passing a number: 1234556 you can't do 1234556.replace(..) you can convert it to string first: 234556.toString().replace(..)
altomarketing
altomarketingOP4mo ago
lets forget the converting.. it still printing all code instead of executing it..
No description
altomarketing
altomarketingOP4mo ago
Im using version 3.2.0
Baptiste
Baptiste4mo ago
Now the error is TypeError: phone.startsWith is not a function because phone is a number as long as the code is failing, the whole content is returned to the variable This behavior will change soon
altomarketing
altomarketingOP4mo ago
thanks for your support.. i will wait

Did you find this page helpful?