ChatBot-Projekt/Chatbot_v0.8.1Beta/MainWindow.xaml.cs
2025-05-07 10:12:13 +02:00

241 lines
9.8 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using System.IO;
namespace Chatbot_WPF_Projekt
{
public partial class MainWindow : Window
{
// Variable für standort
private string currentLocation = "";
public MainWindow()
{
InitializeComponent();
}
// Methode für Senden-Button
private async void SendButton_Click(object sender, RoutedEventArgs e)
{
// Hole den Text und entferne führende Leerzeichen
string userInput = InputTextBox.Text.Trim();
// Überprüfe, ob der Text leer ist
if (string.IsNullOrEmpty(userInput))
{
MessageBox.Show("Bitte gib eine Nachricht ein!");
return;
}
// Füge die Nachricht des Benutzers zum Chat-Verlauf hinzu
ChatOutput.Text += $"Du: {userInput}\n";
// Deklariere die Variable botResponse
string botResponse = "";
// Überprüfe die Eingabe und reagiere entsprechend
if (userInput.ToLower() == "!hilfe")
{
botResponse = "Ich bin ein Chatbot. Du kannst folgende Befehle verwenden:\n!hilfe - Zeigt diese Nachricht\n!joke - Erzählt einen Witz\n!standort - Setzt deinen Standort\n!zeit - Zeigt die aktuelle Zeit am Standort\n!wetter - Zeigt das Wetter am Standort\n";
}
else if (userInput.ToLower() == "!joke")
{
string[] witze = new string[10];
witze[0] = "Warum können Geister so schlecht lügen? Weil man durch sie hindurchsieht.";
witze[1] = "Programmierer beim Bäcker: 'Ich hätte gern 2^3 Brötchen.'";
witze[2] = "Warum war der Entwickler pleite? Weil er in C# gearbeitet hat, aber nur in Cent bezahlt wurde.";
witze[3] = "Was sagt ein Bit zum anderen? 'Bist du noch ganz bei 0?'";
witze[4] = "Warum ging der Java-Entwickler ins Kloster? Er wollte finally blocken.";
witze[5] = "Wie nennt man einen falschen Programmierer? Einen *C#arlatan*.";
witze[6] = "Warum weinen Entwickler nie? Sie behandeln ihre Gefühle mit Try-Catch.";
witze[7] = "Was macht ein Informatiker beim Sport? Er läuft in Schleifen.";
witze[8] = "Warum ging der C#-Code nicht ins Kino? Zu viele Exceptions.";
witze[9] = "Was macht ein Arduino im Gefängnis? Er sitzt wegen Widerstands.";
Random zufall = new Random();
int zahl = zufall.Next(0, 10);
string output = witze[zahl];
botResponse = output;
}
else if (userInput.ToLower().StartsWith("!standort"))
{
string[] parts = userInput.Split(' ', 2);
if (parts.Length == 2)
{
currentLocation = parts[1].Trim();
botResponse = $"Standort wurde auf '{currentLocation}' gesetzt.";
}
else
{
botResponse = "Verwendung: !standort [stadtname]";
}
}
else if (userInput.ToLower() == "!zeit")
{
if (string.IsNullOrEmpty(currentLocation))
{
botResponse = "Bitte gib zuerst einen Standort mit !standort an.";
}
else
{
botResponse = $"Aktuelle Uhrzeit in {currentLocation}: {DateTime.Now.ToShortTimeString()}";
}
}
else if (userInput.ToLower() == "!wetter")
{
if (string.IsNullOrEmpty(currentLocation))
{
botResponse = "Bitte gib zuerst einen Standort mit !standort an.";
}
else
{
botResponse = await GetWeatherAsync(currentLocation);
}
}
else
{
botResponse = "Befehl nicht erkannt. Versuche es mit !hilfe.";
}
// Füge die Antwort des Bots zum Chat-Verlauf hinzu
ChatOutput.Text += $"Bot: {botResponse}\n";
// Eingabefeld leeren
InputTextBox.Clear();
}
// Methode für Enter-Taste, um eine Nachricht zu senden
private void InputTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
SendButton_Click(sender, e); // Gleich wie der Button
}
}
// Methode für Löschen-Button
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
ChatOutput.Text = string.Empty; // Lösche den Chatverlauf
}
private void Color_theme_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Colortheme Charcoleblack aktiviert!");
//dunkles Theme
this.Background = new SolidColorBrush(Color.FromRgb(30, 30, 30));
SendButton.Background = new SolidColorBrush(Color.FromRgb(70, 70, 70));
SendButton.Foreground = new SolidColorBrush(Color.FromRgb(230, 230, 230));
ClearButton.Background = new SolidColorBrush(Color.FromRgb(70, 70, 70));
ClearButton.Foreground = new SolidColorBrush(Color.FromRgb(230, 230, 230));
Color_theme.Background = new SolidColorBrush(Color.FromRgb(70, 70, 70));
Color_theme.Foreground = new SolidColorBrush(Color.FromRgb(230, 230, 230));
SaveButton.Background = new SolidColorBrush(Color.FromRgb(70, 70, 70));
SaveButton.Foreground = new SolidColorBrush(Color.FromRgb(230, 230, 230));
ChatOutput.Background = new SolidColorBrush(Color.FromRgb(70, 70, 70));
ChatOutput.Foreground = new SolidColorBrush(Color.FromRgb(230, 230, 230));
InputTextBox.Background = new SolidColorBrush(Color.FromRgb(70, 70, 70));
InputTextBox.Foreground = new SolidColorBrush(Color.FromRgb(230, 230, 230));
}
private void Color_theme_Unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Colortheme Perlwhite aktiviert!");
//helles Theme
this.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
SendButton.Background = new SolidColorBrush(Color.FromRgb(221, 221, 221));
SendButton.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
ClearButton.Background = new SolidColorBrush(Color.FromRgb(221, 221, 221));
ClearButton.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
SaveButton.Background = new SolidColorBrush(Color.FromRgb(221, 221, 221));
SaveButton.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
Color_theme.Background = new SolidColorBrush(Color.FromRgb(221, 221, 221));
Color_theme.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
ChatOutput.Background = new SolidColorBrush(Color.FromRgb(240, 240, 240));
ChatOutput.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
InputTextBox.Background = new SolidColorBrush(Color.FromRgb(240, 240, 240));
InputTextBox.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
try
{
// Hole das Verzeichnis, in dem die .exe liegt
string projectDirectory = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName;
// Erstelle Dateinamen mit Zeitstempel, z.B. Chat_2025-05-06_1532.txt
string fileName = $"Chat_{DateTime.Now:yyyy-MM-dd_HHmm}.txt";
// Kompletter Pfad zur Datei
string fullPath = System.IO.Path.Combine(projectDirectory, fileName);
// Schreibe den Chatverlauf in die Datei
System.IO.File.WriteAllText(fullPath, ChatOutput.Text);
MessageBox.Show($"Chatverlauf gespeichert als:\n{fileName}", "Gespeichert", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show("Fehler beim Speichern:\n" + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
//mehtohde für die wetter Api
private async Task<string> GetWeatherAsync(string city)
{
string apiKey = "";
string url = $"https://api.openweathermap.org/data/2.5/weather?q={Uri.EscapeDataString(city)}&units=metric&lang=de&appid={apiKey}";
using HttpClient client = new HttpClient();
try
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
using JsonDocument doc = JsonDocument.Parse(json);
JsonElement root = doc.RootElement;
double temp = root.GetProperty("main").GetProperty("temp").GetDouble();
string description = root.GetProperty("weather")[0].GetProperty("description").GetString();
return $"Aktuelles Wetter in {city}: {temp}°C, {description}";
}
catch (Exception ex)
{
return $"Fehler beim Abrufen der Wetterdaten: {ex.Message}";
}
}
}
}