File size: 10,043 Bytes
aebc6bd
 
e31d1f9
aebc6bd
 
a229033
ab272f6
aebc6bd
a229033
 
 
 
 
 
 
 
aebc6bd
 
 
 
a229033
aebc6bd
 
 
 
 
 
 
 
 
 
 
 
a229033
 
aebc6bd
 
a229033
 
 
 
 
 
 
 
 
 
 
 
 
aebc6bd
 
 
 
 
 
 
 
a229033
aebc6bd
 
 
 
 
 
 
 
 
a229033
aebc6bd
 
a229033
aebc6bd
 
 
a229033
aebc6bd
 
 
 
 
a229033
aebc6bd
 
a229033
 
 
 
 
aebc6bd
 
 
 
 
 
a229033
 
aebc6bd
 
a229033
aebc6bd
a229033
aebc6bd
 
a229033
aebc6bd
a229033
aebc6bd
 
 
 
 
 
 
 
a229033
 
 
aebc6bd
 
a229033
 
 
aebc6bd
 
a229033
 
 
 
 
 
 
 
 
 
 
aebc6bd
 
 
 
 
 
a229033
 
aebc6bd
 
 
a229033
 
 
ab272f6
cecaf14
a229033
 
 
 
b6b6fb9
a229033
 
 
 
 
 
 
 
aebc6bd
 
a229033
 
 
 
aebc6bd
 
 
 
ab928ba
a229033
 
 
 
 
aebc6bd
a229033
aebc6bd
 
a229033
 
 
 
aebc6bd
 
a229033
 
 
aebc6bd
 
 
 
 
 
a229033
aebc6bd
 
 
a229033
 
 
 
aebc6bd
a229033
 
 
 
 
aebc6bd
a229033
 
 
 
 
 
 
 
 
aebc6bd
a229033
 
 
aebc6bd
a229033
 
 
 
 
 
 
 
 
aebc6bd
a229033
 
aebc6bd
a229033
 
 
 
 
 
 
 
 
aebc6bd
 
 
 
 
 
 
 
a229033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aebc6bd
 
 
a229033
aebc6bd
a229033
aebc6bd
 
 
 
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
<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>مكتبة أستاذ ريمون - دردشة مباشرة</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css
    <style>
        :root {
            --primary-color: #1a237e;
            --secondary-color: #283593;
            --accent-color: #3949ab;
            --background-light: #e8eaf6;
            --text-color: #121212;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f0f2f5;
        }
        .chat-container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
            padding: 20px;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
            padding: 15px;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            border-radius: 10px;
            position: relative;
        }
        .header h1 {
            margin: 0;
            font-size: 24px;
        }
        .customer-service-icon {
            position: absolute;
            left: 20px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 2.5em;
            color: #fff;
        }
        .chat-box {
            height: 400px;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 8px;
            background-color: var(--background-light);
        }
        .input-container {
            display: flex;
            gap: 10px;
            margin-bottom: 10px;
        }
        input {
            flex: 1;
            padding: 12px;
            border: 2px solid var(--primary-color);
            border-radius: 8px;
            font-size: 16px;
            background-color: white;
        }
        button {
            padding: 12px 25px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: bold;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: var(--accent-color);
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        .message {
            margin-bottom: 12px;
            padding: 12px;
            border-radius: 8px;
            max-width: 80%;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            white-space: pre-wrap;
        }
        .user-message {
            background-color: #fff;
            margin-left: 20%;
            border-right: 4px solid var(--accent-color);
        }
        .bot-message {
            background-color: #fff;
            margin-right: 20%;
            border-left: 4px solid var(--primary-color);
        }
        .faq-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            gap: 10px;
            margin-bottom: 15px;
        }
        .faq-button {
            background-color: var(--background-light);
            border: 1px solid var(--primary-color);
            padding: 10px;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
            color: var(--primary-color);
        }
        .faq-button:hover {
            background-color: var(--primary-color);
            color: white;
        }
        .typing-indicator {
            display: none;
            margin-bottom: 12px;
            padding: 12px;
            border-radius: 8px;
            max-width: 80%;
            margin-right: 20%;
            background-color: #f0f0f0;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="header">
            <i class="fas fa-headset customer-service-icon"></i>
            <h1>مكتبة أستاذ ريمون</h1>
            <p>خدمة العملاء - متوفرين على مدار الساعة</p>
        </div>
        <div class="faq-container">
            <button class="faq-button" onclick="askFAQ('اسعار الورق')">
                <i class="fas fa-file-alt"></i> أسعار الورق
            </button>
            <button class="faq-button" onclick="askFAQ('تحدث عن مكتبتك التي اسمها مكتبة استاذ ريمون')">
                <i class="fas fa-store"></i> عن المكتبة
            </button>
            <button class="faq-button" onclick="askFAQ('قائمة المنتجات')">
                <i class="fas fa-list"></i> قائمة المنتجات
            </button>
            <button class="faq-button" onclick="askFAQ('عروضكم')">
                <i class="fas fa-percentage"></i> العروض
            </button>
            <button class="faq-button" onclick="askFAQ('طرق الدفع')">
                <i class="fas fa-credit-card"></i> طرق الدفع
            </button>
        </div>
        <div class="chat-box" id="chatBox">
            <div class="typing-indicator" id="typingIndicator">جاري الكتابة...</div>
        </div>
        <div class="input-container">
            <input type="text" id="userInput" placeholder="اكتب سؤالك هنا..." onkeypress="handleKeyPress(event)">
            <button id="sendButton" onclick="sendMessage()">
                <i class="fas fa-paper-plane"></i> إرسال
            </button>
        </div>
    </div>

    <script>
        const API_URL = 'https://g2mgow5tgbxsjy-7777.proxy.runpod.net/proxy/4000/chat/stream';
        let isWaitingForResponse = false;

        function handleKeyPress(event) {
            if (event.key === 'Enter' && !isWaitingForResponse) {
                sendMessage();
            }
        }

        function askFAQ(question) {
            if (!isWaitingForResponse) {
                addMessage(question, 'user-message');
                sendToAPI(question);
            }
        }

        async function sendMessage() {
            if (isWaitingForResponse) return;

            const input = document.getElementById('userInput');
            const message = input.value.trim();
            
            if (message) {
                addMessage(message, 'user-message');
                input.value = '';
                await sendToAPI(message);
            }
        }

        async function sendToAPI(message) {
            const sendButton = document.getElementById('sendButton');
            const userInput = document.getElementById('userInput');
            const typingIndicator = document.getElementById('typingIndicator');

            try {
                isWaitingForResponse = true;
                sendButton.disabled = true;
                userInput.disabled = true;
                typingIndicator.style.display = 'block';

                const response = await fetch(API_URL, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        message: message
                    })
                });

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const reader = response.body.getReader();
                let accumulatedResponse = '';

                while (true) {
                    const {value, done} = await reader.read();
                    if (done) break;
                    
                    const chunk = new TextDecoder().decode(value);
                    accumulatedResponse += chunk;

                    // Update the chat with the accumulated response
                    updateLastBotMessage(accumulatedResponse);
                }

            } catch (error) {
                console.error('Error:', error);
                addMessage('عذراً، حدث خطأ في الاتصال. يرجى المحاولة مرة أخرى.', 'bot-message');
            } finally {
                isWaitingForResponse = false;
                sendButton.disabled = false;
                userInput.disabled = false;
                typingIndicator.style.display = 'none';
            }
        }

        function addMessage(text, className) {
            const chatBox = document.getElementById('chatBox');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${className}`;
            messageDiv.textContent = text;
            chatBox.insertBefore(messageDiv, document.getElementById('typingIndicator'));
            chatBox.scrollTop = chatBox.scrollHeight;
        }

        function updateLastBotMessage(text) {
            const chatBox = document.getElementById('chatBox');
            let lastBotMessage = chatBox.querySelector('.bot-message:last-of-type');
            
            if (!lastBotMessage) {
                lastBotMessage = document.createElement('div');
                lastBotMessage.className = 'message bot-message';
                chatBox.insertBefore(lastBotMessage, document.getElementById('typingIndicator'));
            }
            
            lastBotMessage.textContent = text;
            chatBox.scrollTop = chatBox.scrollHeight;
        }

        // Welcome message
        window.onload = function() {
            addMessage("مرحباً بك في مكتبة أستاذ ريمون! كيف يمكنني مساعدتك اليوم؟", 'bot-message');
        }
    </script>
</body>
</html>