From 21d2fd7170264be0e2549233165b54a50e7fadf2 Mon Sep 17 00:00:00 2001 From: ubre Date: Mon, 1 Apr 2024 23:07:13 +0200 Subject: [PATCH] Basic discord post --- FishBot/Program.cs | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/FishBot/Program.cs b/FishBot/Program.cs index ce84cf7..3f323dd 100644 --- a/FishBot/Program.cs +++ b/FishBot/Program.cs @@ -1,32 +1,48 @@ using System; -using System.Device.Gpio; -using System.Threading; +using System.Net.Http; +using System.Text.Json; +using System.Collections.Generic; +using System.Threading.Tasks; class Program { - public static void Main(string[] args) + public class DiscordWebhookMessage { - Console.WriteLine("Running."); + public string content { get; set; } + public List embeds { get; set; } + public List attachments { get; set; } = new(); + } - int buttonPin = 17; - int ledPin = 18; + static async Task Main(string[] args) + { + string url = "https://discord.com/api/webhooks/1038085680967991376/Hr5tpsUExHuGqcAuslDnpxPKjn3Dokr1Z1_BBYGih1wu07SiqIZ-zc6pcUojMRFvde1z"; - using var controller = new GpioController(); + var messageData = new DiscordWebhookMessage + { + content = "Test" + }; - controller.OpenPin(buttonPin, PinMode.Input); - controller.OpenPin(ledPin, PinMode.Output); + await SendPostRequest(url, messageData); + } - while (true) + static async Task SendPostRequest(string url, DiscordWebhookMessage messageData) + { + using (var httpClient = new HttpClient()) { - if (controller.Read(buttonPin) == PinValue.High) + var json = JsonSerializer.Serialize(messageData); + Console.Write( json ); + var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); + var response = await httpClient.PostAsync(url, content); + + if (response.IsSuccessStatusCode) { - controller.Write(ledPin, PinValue.High); - Console.WriteLine("ON"); + string responseContent = await response.Content.ReadAsStringAsync(); + Console.WriteLine("POST request was successful. Response content:"); + Console.WriteLine(responseContent); } else { - controller.Write(ledPin, PinValue.Low); - Console.WriteLine("OFF"); + Console.WriteLine($"POST request failed. Status code: {response.StatusCode}"); } } }