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 UNKNOWN16 Replies
Someone will reply to you shortly. In the meantime, this might help:
-# This post was marked as solved by Baptiste. View answer.
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);
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
If you are setting one variable only, use a Set variable block with a return value
Here is a valid code:
I dont know what im doing wrong.. but it prints all code instead of executing it

Copy paste your code here there is most likely a syntax issue with it
attaching now the bot.. error in test is TypeError: bernhrdrreyefbsnxbw6l1l6.replace is not a function
much more easy use a api, send the phone number to a phone number api checker like abstractapi, then get only what you need.
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 ?
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-blockSee the image, i did {{Phone}}.replace(/\D/g, "")
i did copy and paste your code..
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(..)
lets forget the converting.. it still printing all code instead of executing it..

Im using version 3.2.0
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
thanks for your support.. i will wait