Skip to content

Commit

Permalink
Basic discord post
Browse files Browse the repository at this point in the history
  • Loading branch information
ubre committed Apr 1, 2024
1 parent d459712 commit 21d2fd7
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions FishBot/Program.cs
Original file line number Diff line number Diff line change
@@ -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<string> embeds { get; set; }
public List<string> 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}");
}
}
}
Expand Down

0 comments on commit 21d2fd7

Please sign in to comment.