File size: 11,820 Bytes
2fdb240
9825317
c8449af
 
 
 
 
 
9825317
c8449af
 
 
9825317
c8449af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9825317
 
c8449af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9825317
c8449af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fdb240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9825317
 
 
 
c8449af
9825317
c8449af
 
 
2fdb240
c8449af
2fdb240
 
 
 
 
 
 
 
c8449af
 
 
2fdb240
c8449af
 
 
9825317
 
c8449af
 
 
 
 
9825317
c8449af
 
 
 
 
 
 
2fdb240
 
 
 
 
c8449af
 
 
 
 
 
 
2fdb240
 
c8449af
 
 
2fdb240
 
9825317
2fdb240
 
 
 
 
 
9825317
2fdb240
 
 
 
 
 
 
 
 
c8449af
 
2fdb240
 
 
 
 
 
 
 
 
 
 
 
 
 
c8449af
 
2fdb240
 
 
 
c8449af
 
2fdb240
c8449af
 
 
 
9825317
c8449af
 
2fdb240
c8449af
 
 
 
2fdb240
 
 
c8449af
 
2fdb240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8449af
 
 
 
 
9825317
2fdb240
c8449af
 
 
 
 
 
 
 
 
 
9825317
c8449af
 
 
 
 
 
9825317
2fdb240
c8449af
 
9825317
c8449af
9825317
c8449af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9825317
 
c8449af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fdb240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8449af
2fdb240
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!DOCTYPE html><html><head><base href="https://WhatsApp/ai.com">
<meta charset="UTF-8">
<title>AI-Powered WhatsApp Chat with Image Upload</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="theme-color" content="#075e54">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<style>
  /* Les styles restent identiques */
  body, html {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    background-color: #e5ddd5;
    height: 100%;
    overflow: hidden;
  }
  #chat-container {
    max-width: 100%;
    height: 100%;
    margin: 0 auto;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    box-shadow: 0 1px 1px 0 rgba(0,0,0,.06), 0 2px 5px 0 rgba(0,0,0,.2);
  }
  #chat-header {
    background-color: #075e54;
    color: #fff;
    padding: 10px;
    display: flex;
    align-items: center;
  }
  #chat-header img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
  }
  #chat-header h1 {
    margin: 0;
    font-size: 16px;
  }
  #chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background-image: url('https://web.whatsapp.com/img/bg-chat-tile-dark_a4be512e7195b6b733d9110b408f075d.png');
    background-repeat: repeat;
  }
  .message {
    max-width: 80%;
    margin-bottom: 10px;
    padding: 8px 12px;
    border-radius: 7.5px;
    position: relative;
    word-wrap: break-word;
    overflow-wrap: break-word;
  }
  .user-message {
    background-color: #dcf8c6;
    align-self: flex-end;
    margin-left: auto;
  }
  .ai-message {
    background-color: #fff;
    align-self: flex-start;
  }
  #chat-input {
    display: flex;
    padding: 10px;
    background-color: #f0f0f0;
  }
  #user-input {
    flex: 1;
    border: none;
    padding: 10px;
    border-radius: 20px;
    margin-right: 10px;
    font-size: 14px;
  }
  #send-button, #file-button {
    background-color: #075e54;
    color: #fff;
    border: none;
    padding: 10px;
    border-radius: 50%;
    cursor: pointer;
    margin-left: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  #send-button svg, #file-button svg {
    width: 24px;
    height: 24px;
    fill: #fff;
  }
  #file-input {
    display: none;
  }
  .message img {
    max-width: 100%;
    height: auto;
    border-radius: 7.5px;
    margin-top: 5px;
  }
  .typing-indicator {
    padding: 8px 12px;
    background-color: #fff;
    border-radius: 7.5px;
    margin-bottom: 10px;
    display: none;
  }
  .typing-indicator span {
    display: inline-block;
    width: 8px;
    height: 8px;
    background-color: #90a4ae;
    border-radius: 50%;
    margin-right: 5px;
    animation: typing 1s infinite;
  }
  @keyframes typing {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-5px); }
  }
  @media (min-width: 768px) {
    #chat-container {
      max-width: 600px;
    }
  }
  /* New styles for appointments and history */
  .appointment-card {
    background: #fff;
    border-radius: 10px;
    padding: 15px;
    margin: 10px 0;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  }
  .history-button {
    display: none; /* Hide the history button */
  }
  .history-panel {
    position: fixed;
    right: -300px; /* Keep it offscreen by default */
    top: 0;
    width: 300px;
    height: 100%;
    background: white;
    box-shadow: -2px 0 5px rgba(0,0,0,0.2);
    transition: right 0.3s ease;
    z-index: 1000;
    padding: 20px;
    display: none; /* Hide it completely */
  }
  .history-panel.active {
    right: 0;
    display: block; /* Show only when active */
  }
  /* Ajout de styles pour le support des emojis */
  .emoji {
    font-family: "Segoe UI Emoji", "Noto Color Emoji", "Apple Color Emoji", "Segoe UI Symbol", sans-serif;
  }
</style>
</head>
<body>
<div id="chat-container">
  <div id="chat-header">
    <img src="https://cdn-icons-png.flaticon.com/512/4712/4712109.png" alt="Logo zone - corporate stylized icon with square background" style="width: 40px; height: 40px; object-fit: cover; border-radius: 5px;">
    <h1>
  ASSISTANT AI +226 56888879
  <span style="display: inline-block; margin-left: 5px; background-color: #0099ff; color: white; font-size: 0.6em; padding: 2px 5px; border-radius: 50%; vertical-align: middle; transform: translateY(-0.5mm);" title="Assistant IA Vérifié"></span>
</h1>
    <button class="history-button" id="history-button">
      <svg width="24" height="24" viewBox="0 0 24 24" fill="white">
        <path d="M13 3c-4.97 0-9 4.03-9 9H1l3.89 3.89.07.14L9 12H6c0-3.87 3.13-7 7-7s7 3.13 7 7-3.13 7-7 7c-1.93 0-3.68-.79-4.94-2.06l-1.42 1.42C8.27 19.99 10.51 21 13 21c4.97 0 9-4.03 9-9s-4.03-9-9-9zm-1 5v5l4.25 2.52.77-1.28-3.52-2.09V8z"></path>
      </svg>
    </button>
  </div>
  <div id="chat-messages">
    <div class="typing-indicator" id="typing-indicator">
      <span></span><span></span><span></span>
    </div>
  </div>
  <div id="chat-input">
    <input type="text" id="user-input" placeholder="Écrivez votre message">
    <button id="file-button" title="Joindre une image">
      <svg viewBox="0 0 24 24">
        <path d="M16.5 6v11.5c0 2.21-1.79 4-4 4s-4-1.79-4-4V5a2.5 2.5 0 0 1 5 0v10.5c0 .55-.45 1-1 1s-1-.45-1-1V6H10v9.5a2.5 2.5 0 0 0 5 0V5c0-2.21-1.79-4-4-4S7 2.79 7 5v12.5c0 3.04 2.46 5.5 5.5 5.5s5.5-2.46 5.5-5.5V6h-1.5z"></path>
      </svg>
    </button>
    <input type="file" id="file-input" accept="image/*">
    <button id="send-button" title="Envoyer">
      <svg viewBox="0 0 24 24">
        <path d="M1.101 21.757 23.8 12.028 1.101 2.3l.011 7.912 13.623 1.816-13.623 1.817-.011 7.912z"></path>
      </svg>
    </button>
  </div>
</div>

<div class="history-panel" id="history-panel">
  <h2>Historique des conversations</h2>
  <div id="conversation-history"></div>
</div>

<script>
const chatMessages = document.getElementById('chat-messages');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
const fileButton = document.getElementById('file-button');
const fileInput = document.getElementById('file-input');
const typingIndicator = document.getElementById('typing-indicator');
const historyButton = document.getElementById('history-button');
const historyPanel = document.getElementById('history-panel');

const BACKEND_URL = 'http://your-backend-url/api';

// Store conversation history
let conversationHistory = [];

// Welcome messages
const welcomeMessages = [
    "Bonjour! 👋 Je suis votre assistant virtuel. Comment puis-je vous aider aujourd'hui?",
    "Je peux vous aider à prendre des rendez-vous, répondre à vos questions, et plus encore!",
    "N'hésitez pas à partager des images ou à me poser des questions."
];

function saveToHistory(message, isUser) {
    const timestamp = new Date().toISOString();
    conversationHistory.push({
        message,
        isUser,
        timestamp
    });
    localStorage.setItem('chatHistory', JSON.stringify(conversationHistory));
    updateHistoryPanel();
}

function updateHistoryPanel() {
    const historyContainer = document.getElementById('conversation-history');
    historyContainer.innerHTML = '';
    
    conversationHistory.forEach(item => {
        const messageDiv = document.createElement('div');
        messageDiv.classList.add('history-message');
        const date = new Date(item.timestamp).toLocaleString();
        messageDiv.innerHTML = `
            <small>${date}</small><br>
            <strong>${item.isUser ? 'Vous' : 'Assistant'}</strong>: ${item.message}
        `;
        historyContainer.appendChild(messageDiv);
    });
}

historyButton.addEventListener('click', () => {
    historyPanel.classList.toggle('active');
});

function addMessage(content, isUser, imageData = null) {
    const messageDiv = document.createElement('div');
    messageDiv.classList.add('message', isUser ? 'user-message' : 'ai-message');

    if (imageData) {
        const img = document.createElement('img');
        img.src = imageData;
        img.alt = 'Image envoyée';
        messageDiv.appendChild(img);
    } else {
        messageDiv.textContent = content;
    }

    chatMessages.appendChild(messageDiv);
    chatMessages.scrollTop = chatMessages.scrollHeight;
    
    // Save to history
    saveToHistory(imageData ? '[Image]' : content, isUser);
}

// Load history from localStorage
function loadHistory() {
    const savedHistory = localStorage.getItem('chatHistory');
    if (savedHistory) {
        conversationHistory = JSON.parse(savedHistory);
        updateHistoryPanel();
    }
}

// Initialize
window.onload = () => {
    loadHistory();
    // Display welcome messages with delay
    welcomeMessages.forEach((message, index) => {
        setTimeout(() => {
            addMessage(message, false);
        }, index * 1000);
    });
};

async function sendMessageToBackend(message, imageData = null) {
    try {
        showTypingIndicator();

        const formData = new FormData();
        // Encode le message avant l'envoi
        formData.append('message', encodeURIComponent(message));
        if (imageData) {
            formData.append('image', imageData);
        }

        const response = await fetch(`${BACKEND_URL}/chat`, {
            method: 'POST',
            body: formData
        });

        if (!response.ok) {
            throw new Error('Erreur réseau');
        }

        const data = await response.json();
        hideTypingIndicator();
        
        if (data.response) {
            // Décode la réponse du serveur
            addMessage(decodeURIComponent(data.response), false);
        }
    } catch (error) {
        console.error('Erreur:', error);
        hideTypingIndicator();
        addMessage("Désolé, une erreur s'est produite. Veuillez réessayer.", false);
    }
}

async function sendMessage() {
    const userMessage = userInput.value.trim();
    if (!userMessage) return;

    addMessage(userMessage, true);
    userInput.value = '';
    
    await sendMessageToBackend(userMessage);
}

function handleImageUpload(file) {
    if (file) {
        const reader = new FileReader();
        reader.onload = async function(e) {
            addMessage('', true, e.target.result);
            await sendMessageToBackend('', file);
        };
        reader.readAsDataURL(file);
    }
}

// Event Listeners
sendButton.addEventListener('click', sendMessage);

userInput.addEventListener('keypress', (e) => {
    if (e.key === 'Enter' && !e.shiftKey) {
        e.preventDefault();
        sendMessage();
    }
});

fileButton.addEventListener('click', () => fileInput.click());

fileInput.addEventListener('change', (event) => {
    handleImageUpload(event.target.files[0]);
});

// Gestion du drag and drop
chatMessages.addEventListener('dragover', (e) => {
    e.preventDefault();
    e.stopPropagation();
});

chatMessages.addEventListener('drop', (e) => {
    e.preventDefault();
    e.stopPropagation();
    const file = e.dataTransfer.files[0];
    if (file && file.type.startsWith('image/')) {
        handleImageUpload(file);
    }
});

// Fonction pour encoder correctement les caractères spéciaux
function encodeMessage(message) {
    return encodeURIComponent(message);
}

// Fonction pour décoder les caractères spéciaux
function decodeMessage(message) {
    return decodeURIComponent(message);
}

function showTypingIndicator() {
    typingIndicator.style.display = 'block';
    chatMessages.scrollTop = chatMessages.scrollHeight;
}

function hideTypingIndicator() {
    typingIndicator.style.display = 'none';
}

</script>
</body></html>