Whatsapp Numbers in Brazil, help me fix using JavaScript please

Describe the issue/error/question

============================
Hello my friend, thanks in advance for spending your time to help me.

Here in Brazil, Whatsapp makes a mess while treating numbers from different regions of the country.

Typically a phone number here is composed by DDI (+55) + DDD (Two digit number) + 9 + Phone number (8 digit number). Example: +55 11 9 1234 5678

That’s how Whatsapp use numbers with DDD minor or equal to 30, but when DDD is 31 or above, Whatsapp doesn’t use the number 9 between the DDD and the 8-digit phone number. So, for example on Whatsapp a number with DDD 34 looks like: +55 34 1234 5678.

What am I looking for?

My challenge than is to apply a JavaScript function to a node from Google Sheets that takes it in consideration before adding the number to my database and contacting a lead on Whatsapp.

So, if my input phone number comes as “48912345678” the JavaScript should remove the number 9 and respond as output “4812345678”.

If the input number comes as “1112345678”, the JavaScript should add the number 9 and respond as output “11912345678”.

If the input number comes as “11912345678” or “4812345678” it should keep the number as it is.

I tried to implement a solution using this JavaScript below, but couldn’t manage to make it run on N8N.

  let number = items[0].json.Telefone  //(from GoogleSheets Node)
  let numberDDI = 55
  let numberDDD = number.substr(2, 2);
  let numberUser = number.substr(-8, 8);


 if (numberDDI === "55" && parseInt(numberDDD) <= 30) {
    const numberFINAL = "55" + numberDDD + "9" + numberUser;
  }
  else if (numberDDI === "55" && parseInt(numberDDD) > 30) {
    const numberFINAL = "55" + numberDDD + numberUser;
  }

return [{items}]

My GoogleSheet input node is:

{
  "Data": 44903,
  "Hora": 0.7479166666666667,
  "Status": "aprovado",
  "Nome completo": "Pedro Otávio",
  "Telefone": 4899344232,
  "Email": "[email protected]",
  "Qual a sua data de nascimento?": 31335,
  "Qual o seu perfil no Instagram?": "@pedro.otavio",
  "Qual foi o seu faturamento médio mensal dos últimos 3 meses?": 55000,
  "Você já investe em anúncios?": "Não, mas estou pronto para investir.",
  "Quanto você pode investir mensalmente em anúncios?": 3000,
  "Nome da Empresa": "PO Company",
  "Qual área você atua e o que você vende?": "Produtos estéticos para homens\r\nVendo kits de beleza masculina",
  "Descreva de forma clara e objetiva quais estratégias de vendas você já utiliza?": "Atualmente funciona mais com o boca a boca, temos perfil no instagram onde fazemos publicações semanais e programa de afiliados.",
  "Por que aumentar seu faturamento é importante para você e seu futuro?": "Para alcançar meu sonho de ter uma marca renomada e entregar o produto com melhor qualidade para os meus clientes.",
  "Por que deveria escolher você?": "Sou uma pessoa dedicada e comprometida.",
  "Qual resultado espera da Consultoria Estratégica?": "Espero ter orientação e clareza.",
  "Use esse espaço caso queira me falar mais alguma coisa. (opcional)": "Muito obrigado por disponibilizar essa ajuda gratuita.",
  "Você já ouviu falar da minha Mentoria Avançada de Alavancagem de Negócios?": "Sim",
  "Page URL": "https://agenciacristal.com.br/ce",
  " IMPORTANTE: sua aplicação através deste questionário NÃO garante que você será aceito no programa. Vamos avaliar cautelosamente cada aplicação e entraremos em contato com os aprovados.": "",
  "Termo de Participação": "",
  "Primeiro nome": "Pedro",
  "Numero da Lead": 2
}

Please, give me directions or possibilites to solve this problem. Thanks again!

What is the error message (if any)?

ERROR: number.substr is not a function [line 3]

Share the output returned by the last node

TypeError: number.substr is not a function
    at /home/agenciacristal-flow/.nvm/versions/node/v16.18.1/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:3:26
    at /home/agenciacristal-flow/.nvm/versions/node/v16.18.1/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:15:2
    at VM2 Wrapper.apply (/home/agenciacristal-flow/.nvm/versions/node/v16.18.1/lib/node_modules/n8n/node_modules/vm2/lib/bridge.js:485:11)
    at Sandbox.run (/home/agenciacristal-flow/.nvm/versions/node/v16.18.1/lib/node_modules/n8n/node_modules/vm2/lib/nodevm.js:426:23)
    at Sandbox.runCodeAllItems (/home/agenciacristal-flow/.nvm/versions/node/v16.18.1/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Sandbox.js:53:42)
    at Sandbox.runCode (/home/agenciacristal-flow/.nvm/versions/node/v16.18.1/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Sandbox.js:30:62)
    at Object.execute (/home/agenciacristal-flow/.nvm/versions/node/v16.18.1/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:75:39)
    at Workflow.runNode (/home/agenciacristal-flow/.nvm/versions/node/v16.18.1/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:658:51)
    at /home/agenciacristal-flow/.nvm/versions/node/v16.18.1/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:585:68

Information on your n8n setup

  • n8n version: 0.203.1
  • Database you’re using (default: SQLite):
  • Running n8n with the execution process [own]:
  • Running n8n via [npm]:

Hi @ygorpguimaraes, welcome to the community!

I’ve moved your JS code into a Code node and made some small adjustments so it reads (and returns) items matching n8n’s data structure:

You might need to make some further tweaks to get the result right but this is hopefully a good starting point for you :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.