-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
289 lines (266 loc) · 11.8 KB
/
test.html
File metadata and controls
289 lines (266 loc) · 11.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
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
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basit JavaScript Bot</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
flex-direction: column;
}
.chat-container {
width: 90%;
max-width: 500px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
border-radius: 8px;
display: flex;
flex-direction: column;
height: 70vh;
max-height: 600px;
}
.chat-header {
background-color: #4CAF50; /* Yeşil bir tema */
color: white;
padding: 15px;
text-align: center;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
}
.chat-messages {
flex-grow: 1;
padding: 15px;
overflow-y: auto;
border-bottom: 1px solid #eee;
display: flex;
flex-direction: column;
}
.message {
margin-bottom: 10px;
padding: 8px 12px;
border-radius: 15px;
line-height: 1.4;
max-width: 75%;
word-wrap: break-word;
}
.user-message {
background-color: #dcf8c6; /* Kullanıcı mesajı için açık yeşil */
color: #333;
align-self: flex-end;
border-bottom-right-radius: 5px;
}
.bot-message {
background-color: #f1f0f0; /* Bot mesajı için gri */
color: #333;
align-self: flex-start;
border-bottom-left-radius: 5px;
}
.chat-input {
display: flex;
padding: 10px;
border-top: 1px solid #ddd;
background-color: #f9f9f9;
}
.chat-input input {
flex-grow: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 20px;
margin-right: 10px;
outline: none;
}
.chat-input button {
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 20px;
cursor: pointer;
font-weight: bold;
}
.chat-input button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>Basit Bilgi Botu</h1>
<div class="chat-container">
<div class="chat-header">
<h2>Sohbet</h2>
</div>
<div class="chat-messages" id="chatMessages">
<div class="message bot-message">Merhaba! Ülkelerin başkentlerini, şehirlerin plaka kodlarını veya basit matematik işlemlerini sorabilirsiniz. (Örn: "Türkiye başkenti", "Ankara plakası", "5 + 3")</div>
</div>
<div class="chat-input">
<input type="text" id="userInput" placeholder="Mesajınızı yazın..." autofocus>
<button onclick="sendMessage()">Gönder</button>
</div>
</div>
<script>
// --- VERİ DEPOLAMA ---
const capitals = {
"türkiye": "Ankara",
"almanya": "Berlin",
"fransa": "Paris",
"japonya": "Tokyo",
"amerika": "Washington D.C.",
"ingiltere": "Londra",
"italya": "Roma",
"ispanya": "Madrid",
"rusya": "Moskova",
"çin": "Pekin",
"azerbaycan": "Bakü"
// Daha fazla ülke eklenebilir
};
const licensePlates = {
"adana": "01", "adiyaman": "02", "afyonkarahisar": "03", "ağri": "04", "amasya": "05",
"ankara": "06", "antalya": "07", "artvin": "08", "aydin": "09", "balikesir": "10",
"bilecik": "11", "bingöl": "12", "bitlis": "13", "bolu": "14", "burdur": "15",
"bursa": "16", "çanakkale": "17", "çankiri": "18", "çorum": "19", "denizli": "20",
"diyarbakir": "21", "edirne": "22", "elaziğ": "23", "erzincan": "24", "erzurum": "25",
"eskişehir": "26", "gaziantep": "27", "giresun": "28", "gümüşhane": "29", "hakkari": "30",
"hatay": "31", "isparta": "32", "mersin": "33", "istanbul": "34", "izmir": "35",
"kars": "36", "kastamonu": "37", "kayseri": "38", "kirklareli": "39", "kirşehir": "40",
"kocaeli": "41", "konya": "42", "kütahya": "43", "malatya": "44", "manisa": "45",
"kahramanmaraş": "46", "mardin": "47", "muğla": "48", "muş": "49", "nevşehir": "50",
"niğde": "51", "ordu": "52", "rize": "53", "sakarya": "54", "samsun": "55",
"siirt": "56", "sinop": "57", "sivas": "58", "tekirdağ": "59", "tokat": "60",
"trabzon": "61", "tunceli": "62", "şanliurfa": "63", "uşak": "64", "van": "65",
"yozgat": "66", "zonguldak": "67", "aksaray": "68", "bayburt": "69", "karaman": "70",
"kirikkale": "71", "batman": "72", "şirnak": "73", "bartin": "74", "ardahan": "75",
"iğdir": "76", "yalova": "77", "karabük": "78", "kilis": "79", "osmaniye": "80",
"düzce": "81"
// Tüm iller eklendi
};
const defaultResponses = [
"Üzgünüm, bunu anlayamadım. Lütfen farklı bir şekilde sormayı deneyin.",
"Bu konuda bir bilgim yok maalesef.",
"Tam olarak ne demek istediğinizi çıkaramadım."
];
// --- BOT MANTIĞI ---
function getBotResponse(userInput) {
const lowerInput = userInput.toLowerCase().trim();
let cityForPlate = "";
let countryForCapital = "";
// 1. Basit Matematik İşlemleri
// Güvenlik nedeniyle eval() yerine daha kontrollü bir yapı kullanalım
// Örnek: "5 + 3", "10 * 2", "100 / 4", "50 - 10"
// Daha karmaşık ifadeler için (örn: "5 artı 3") regex genişletilebilir
const mathMatch = lowerInput.match(/^(\d+)\s*([\+\-\*\/])\s*(\d+)$/);
if (mathMatch) {
const num1 = parseFloat(mathMatch[1]);
const operator = mathMatch[2];
const num2 = parseFloat(mathMatch[3]);
let result;
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/':
if (num2 === 0) return "Sıfıra bölme hatası!";
result = num1 / num2;
break;
default: return "Geçersiz matematik operatörü.";
}
return `${num1} ${operator} ${num2} = ${result}`;
}
// 2. Plaka Kodu Sorgulama
// Örnek: "ankara plakası", "istanbul plaka kodu", "34 hangi şehir" (bu yönü eklemedik, sadece şehirden plakaya)
const plateMatch = lowerInput.match(/(.+?)\s*(?:ilinin|'nin|nin)?\s*(plakas(ı|i)|plaka kodu)\s*(nedir|kaçtır)?\??$/i);
if (plateMatch) {
cityForPlate = normalizeText(plateMatch[1].trim());
if (licensePlates[cityForPlate]) {
return `${capitalizeFirstLetter(cityForPlate)} ilinin plaka kodu: ${licensePlates[cityForPlate]}`;
}
}
// Sadece şehir adı ve "plakası"
const simplePlateMatch = lowerInput.match(/^(.+?)\s+plakas(ı|i)\??$/i);
if (simplePlateMatch && !cityForPlate) { // Eğer önceki daha spesifik regex eşleşmediyse
cityForPlate = normalizeText(simplePlateMatch[1].trim());
if (licensePlates[cityForPlate]) {
return `${capitalizeFirstLetter(cityForPlate)} ilinin plaka kodu: ${licensePlates[cityForPlate]}`;
}
}
// 3. Başkent Sorgulama
// Örnek: "türkiye başkenti", "fransa'nın başkenti nedir"
const capitalMatch = lowerInput.match(/(.+?)\s*(?:ülkesinin|'nin|nin)?\s*başkenti\s*(nedir|neresidir|hangisidir)?\??$/i);
if (capitalMatch) {
countryForCapital = normalizeText(capitalMatch[1].trim());
if (capitals[countryForCapital]) {
return `${capitalizeFirstLetter(countryForCapital)}'nin başkenti ${capitals[countryForCapital]}.`;
}
}
// Sadece ülke adı ve "başkenti"
const simpleCapitalMatch = lowerInput.match(/^(.+?)\s+başkenti\??$/i);
if (simpleCapitalMatch && !countryForCapital) {
countryForCapital = normalizeText(simpleCapitalMatch[1].trim());
if (capitals[countryForCapital]) {
return `${capitalizeFirstLetter(countryForCapital)}'nin başkenti ${capitals[countryForCapital]}.`;
}
}
// Selamlama ve basit cevaplar
if (["merhaba", "selam", "hey", "günaydın", "iyi günler"].some(word => lowerInput.includes(word))) {
return "Merhaba! Size nasıl yardımcı olabilirim?";
}
if (["nasılsın", "naber"].some(word => lowerInput.includes(word))) {
return "İyiyim, sorduğunuz için teşekkürler! Size nasıl yardımcı olabilirim?";
}
if (["teşekkür ederim", "teşekkürler", "sağ ol", "sağol"].some(word => lowerInput.includes(word))) {
return "Rica ederim! Başka bir sorunuz var mı?";
}
// Anlaşılmadıysa rastgele bir varsayılan cevap ver
return defaultResponses[Math.floor(Math.random() * defaultResponses.length)];
}
// Türkçe karakterleri normalleştirme ve küçük harfe çevirme
function normalizeText(text) {
return text.toLowerCase()
.replace(/ı/g, 'i')
.replace(/ğ/g, 'g')
.replace(/ü/g, 'u')
.replace(/ş/g, 's')
.replace(/ö/g, 'o')
.replace(/ç/g, 'c');
}
function capitalizeFirstLetter(string) {
if (!string) return string;
return string.charAt(0).toUpperCase() + string.slice(1);
}
// --- ARAYÜZ ETKİLEŞİMİ ---
const chatMessagesContainer = document.getElementById('chatMessages');
const userInputElement = document.getElementById('userInput');
userInputElement.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
sendMessage();
}
});
function displayMessage(text, sender) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', sender === 'user' ? 'user-message' : 'bot-message');
messageDiv.textContent = text; // textContent XSS'e karşı daha güvenlidir
chatMessagesContainer.appendChild(messageDiv);
chatMessagesContainer.scrollTop = chatMessagesContainer.scrollHeight; // En alta kaydır
}
function sendMessage() {
const messageText = userInputElement.value.trim();
if (messageText === '') return;
displayMessage(messageText, 'user');
userInputElement.value = ''; // Giriş alanını temizle
userInputElement.focus();
// Botun cevap vermesi için kısa bir gecikme ekleyelim (daha doğal görünmesi için)
setTimeout(() => {
const botReply = getBotResponse(messageText);
displayMessage(botReply, 'bot');
}, 500); // 0.5 saniye gecikme
}
</script>
</body>
</html>