Guilhermes
This commit is contained in:
94
GuilhermesApp/Pages/ChatPage.xaml.cs
Normal file
94
GuilhermesApp/Pages/ChatPage.xaml.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Maui.Controls.Shapes;
|
||||
|
||||
namespace GuilhermesApp.Pages;
|
||||
|
||||
public partial class ChatPage : ContentPage
|
||||
{
|
||||
private const string ApiKey = "AIzaSyBfQIHWK57GDhk4N-xYg_bEVWyQ6X1EmwA";
|
||||
private const string Url = $"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key={ApiKey}";
|
||||
private HttpClient _httpClient = new HttpClient();
|
||||
|
||||
public ChatPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
AdicionarBalao("Olá! Sou o assistente da GuilhermesApp. Como te posso ajudar hoje?", false);
|
||||
}
|
||||
|
||||
private async void OnEnviarClicked(object sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(MensagemEntry.Text)) return;
|
||||
|
||||
string textoUser = MensagemEntry.Text;
|
||||
MensagemEntry.Text = "";
|
||||
AdicionarBalao(textoUser, true);
|
||||
|
||||
EnviarBtn.IsEnabled = false;
|
||||
|
||||
try
|
||||
{
|
||||
var requestBody = new { contents = new[] { new { parts = new[] { new { text = textoUser } } } } };
|
||||
var content = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await _httpClient.PostAsync(Url, content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
|
||||
// Verifica se o pedido teve sucesso
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var respostaBot = doc.RootElement
|
||||
.GetProperty("candidates")[0]
|
||||
.GetProperty("content")
|
||||
.GetProperty("parts")[0]
|
||||
.GetProperty("text").GetString();
|
||||
|
||||
AdicionarBalao(respostaBot, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Lê a mensagem de erro real da Google
|
||||
string erroGoogle = "Erro desconhecido.";
|
||||
if (doc.RootElement.TryGetProperty("error", out var errorElement) &&
|
||||
errorElement.TryGetProperty("message", out var msgElement))
|
||||
{
|
||||
erroGoogle = msgElement.GetString() ?? "Erro desconhecido";
|
||||
}
|
||||
AdicionarBalao($"A API recusou: {erroGoogle}", false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AdicionarBalao($"Erro interno: {ex.Message}", false);
|
||||
}
|
||||
|
||||
EnviarBtn.IsEnabled = true;
|
||||
}
|
||||
// Cria os balões visuais
|
||||
private void AdicionarBalao(string? texto, bool isUser)
|
||||
{
|
||||
var label = new Label
|
||||
{
|
||||
Text = texto,
|
||||
TextColor = Colors.Black,
|
||||
Padding = 15
|
||||
};
|
||||
|
||||
var border = new Border
|
||||
{
|
||||
StrokeShape = new RoundRectangle { CornerRadius = new CornerRadius(15) },
|
||||
BackgroundColor = isUser ? Color.FromArgb("#D1E8FF") : Color.FromArgb("#F3F4F6"),
|
||||
Stroke = Colors.Transparent,
|
||||
Margin = new Thickness(isUser ? 50 : 0, 5, isUser ? 0 : 50, 5),
|
||||
HorizontalOptions = isUser ? LayoutOptions.End : LayoutOptions.Start,
|
||||
Content = label
|
||||
};
|
||||
|
||||
ChatStack.Children.Add(border);
|
||||
|
||||
// Faz scroll automático para o fundo
|
||||
ChatScroll.ScrollToAsync(ChatStack, ScrollToPosition.End, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user