forked from zzjunior/register_lead
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
75 lines (66 loc) · 2.8 KB
/
api.js
File metadata and controls
75 lines (66 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
document.getElementById('enviar').addEventListener('click', function(event) {
event.preventDefault();
// Obter os valores do formulário
const name = document.getElementById('nome').value;
const number = document.getElementById('telefone').value;
const email = document.getElementById('seu_melhor_email_').value;
const TokenTEIA = document.getElementById('token').value;
const sourceTEIA = document.getElementById('source').value;
const storeTEIA = document.getElementById('store').value;
const comentario = document.getElementById('comentario').value;
const tags = document.getElementById('tags').value;
const parametrosExtras = document.getElementById('parametros_extras').value;
// CORRIGIR TAGS PRA FICAREM NO FORMATO ACEITO NA API tags= [ "tag1", "tag2", "tag3"]
let processedTags = [];
if (tags.trim()) {
processedTags = tags.split(',').map(tag => tag.trim()).filter(tag => tag);
}
// Processar parâmetros extras
let extraParams = {};
if (parametrosExtras.trim()) {
try {
extraParams = JSON.parse(parametrosExtras);
} catch (error) {
console.error('Erro ao processar parâmetros extras:', error);
document.getElementById('retorno-api').innerText = 'Erro: Parâmetros extras devem estar em formato JSON válido';
return;
}
}
// Dados do formulário
const formData = {
name: name, // NOME DO LEAD
phone: number, // NÚMERO DO LEAD
email: email, // EMAIL DO LEAD
product_id: 35, // AQUI O "ID" DO PRODUTO
source_id: sourceTEIA, // FONTE DE ORIGEM
//user_document: userDocumentValue, // CPF DO USUÁRIO À RECEBER O LEAD
store_id: storeTEIA, // LOJA DE ORIGEM
comment: comentario, // COMENTÁRIO OPCIONAL
tags: processedTags, // TAGS OPCIONAIS NO FORMATO CORRETO
...extraParams // PARÂMETROS EXTRAS ADICIONADOS DINAMICAMENTE
};
// URL da API
const apiUrl = "https://teiacrm.com.br/api/lead";
// Token de autenticação
const accessToken = TokenTEIA;
// Configurações da requisição
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify(formData)
};
// Envia a requisição
fetch(apiUrl, requestOptions)
.then(response => response.json())
.then(data => {
console.log('Lead enviado >>', data);
document.getElementById('retorno-api').innerText = JSON.stringify(data, null, 2);
})
.catch(error => {
console.error('Erro ao enviar:', error);
document.getElementById('retorno-api').innerText = 'Erro ao enviar: ' + error;
});
});