-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
155 lines (118 loc) · 4.73 KB
/
app.js
File metadata and controls
155 lines (118 loc) · 4.73 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// ======================================================
// WhatsApp Web - Front conectado à API hospedada
// Autor: Roger Ribeiro de Oliveira
// ======================================================
const API_BASE = "https://api-whatsapp-toxy.onrender.com/v1/whatsapp";
const USER_NUMBER = 11987876567;
const contactsContainer = document.querySelector(".contacts-chat");
const chatHeader = document.querySelector(".contact-header");
const chatContainer = document.querySelector(".messages");
const messageInput = document.querySelector(".input-send-message input");
const sendButton = document.querySelector("#send-message");
let selectedContact = null;
async function pegarContatos() {
try {
const response = await fetch(`${API_BASE}/users/${USER_NUMBER}/contacts`);
if (!response.ok) throw new Error("Erro ao buscar contatos");
const data = await response.json();
return data.userInfo[0]?.contacts || [];
} catch (err) {
console.error("Erro ao pegar contatos:", err);
return [];
}
}
async function pegarMensagens(contactNumber) {
try {
const url = `${API_BASE}/users/messages/contact?number=${USER_NUMBER}&contact=${contactNumber}`;
const response = await fetch(url);
if (!response.ok) throw new Error("Erro ao buscar mensagens");
const data = await response.json();
const messages =
data.userInfo?.exchangedMessages?.messages ||
data.userInfo?.exchangedMessages ||
[];
return messages;
} catch (err) {
console.error("Erro ao pegar mensagens:", err);
return [];
}
}
async function renderContacts(contacts) {
contactsContainer.textContent = "";
for (const contact of contacts) {
const messages = await pegarMensagens(contact.number);
const lastMsg = messages[messages.length - 1];
const contactDiv = document.createElement("div");
contactDiv.classList.add("contact");
const img = document.createElement("img");
img.src = "./src/img/nopfpUser.jfif";
const infoDiv = document.createElement("div");
infoDiv.classList.add("info");
const contactInfo = document.createElement("div");
contactInfo.classList.add("contact-info");
const spanName = document.createElement("span");
spanName.textContent = contact.name || "Desconhecido";
const pTime = document.createElement("p");
pTime.textContent = lastMsg?.time || "";
const lastMessage = document.createElement("span");
lastMessage.classList.add("last-message");
lastMessage.textContent = lastMsg?.content || "Clique para conversar";
contactInfo.appendChild(spanName);
contactInfo.appendChild(pTime);
infoDiv.appendChild(contactInfo);
infoDiv.appendChild(lastMessage);
contactDiv.appendChild(img);
contactDiv.appendChild(infoDiv);
contactDiv.addEventListener("click", () => selectContact(contact));
contactsContainer.appendChild(contactDiv);
}
}
function renderHeader(contact) {
const contactName = chatHeader.querySelector(".contact-name");
const profileImg = chatHeader.querySelector(".profile-contact-picture");
if (!contactName || !profileImg) {
console.error("Elementos do cabeçalho não encontrados");
return;
}
contactName.textContent = contact.name || contact.number;
profileImg.src = "./src/img/nopfpUser.jfif";;
}
function renderChat(messages) {
chatContainer.textContent = "";
if (!messages || messages.length === 0) {
const empty = document.createElement("div");
empty.classList.add("alert");
empty.textContent = "Nenhuma mensagem encontrada.";
chatContainer.appendChild(empty);
return;
}
messages.forEach((msg) => {
const msgDiv = document.createElement("div");
const isUser = msg.sender === "me";
msgDiv.classList.add(isUser ? "user-message" : "contact-message");
const textSpan = document.createElement("span");
textSpan.textContent = msg.content;
const timeSpan = document.createElement("span");
timeSpan.textContent = msg.time || "";
msgDiv.appendChild(textSpan);
msgDiv.appendChild(timeSpan);
chatContainer.appendChild(msgDiv);
});
chatContainer.scrollTop = chatContainer.scrollHeight;
}
async function selectContact(contact) {
currentContact = contact;
renderHeader(contact);
const messages = await pegarMensagens(contact.number);
renderChat(messages, contact);
}
async function init() {
const contacts = await pegarContatos();
if (!contacts.length) {
console.error("Nenhum contato encontrado para este usuário.");
return;
}
await renderContacts(contacts);
selectContact(contacts[0]);
}
init();