-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
185 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Newtonsoft.Json; | ||
using Notion.Client; | ||
|
||
namespace TrickFireDiscordBot.Notion | ||
{ | ||
public class Automation | ||
{ | ||
[JsonProperty("source")] | ||
public AutomationSource? Source { get; set; } = null; | ||
|
||
[JsonProperty("data")] | ||
public IObject? Data { get; set; } = null; | ||
} | ||
|
||
public class AutomationSource | ||
{ | ||
[JsonProperty("type")] | ||
public string Type { get; set; } = ""; | ||
|
||
[JsonProperty("automation_id")] | ||
public string AutomationId { get; set; } = ""; | ||
|
||
|
||
[JsonProperty("action_id")] | ||
public string ActionId { get; set; } = ""; | ||
|
||
[JsonProperty("event_id")] | ||
public string EventId { get; set; } = ""; | ||
|
||
[JsonProperty("attempt")] | ||
public int Attempt { get; set; } = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Newtonsoft.Json; | ||
using Notion.Client; | ||
using System.Net; | ||
using TrickFireDiscordBot.Discord; | ||
using TrickFireDiscordBot.Notion; | ||
|
||
namespace TrickFireDiscordBot | ||
{ | ||
public class RoleSyncer(NotionClient notionClient, DiscordBot discordBot, WebhookListener listener) | ||
{ | ||
public const string WebhookEndpoint = "/members"; | ||
|
||
public NotionClient NotionClient { get; } = notionClient; | ||
public DiscordBot DiscordBot { get; } = discordBot; | ||
public WebhookListener WebhookListener { get; } = listener; | ||
|
||
public void Start() | ||
{ | ||
WebhookListener.OnWebhookReceived += OnWebhook; | ||
} | ||
|
||
public void Stop() | ||
{ | ||
WebhookListener.OnWebhookReceived -= OnWebhook; | ||
} | ||
|
||
private void OnWebhook(HttpListenerRequest request) | ||
{ | ||
if (request.RawUrl == null || !request.RawUrl.StartsWith(WebhookEndpoint)) | ||
{ | ||
return; | ||
} | ||
|
||
using StreamReader reader = new(request.InputStream); | ||
Automation? automation = JsonConvert.DeserializeObject<Automation>(reader.ReadToEnd()); | ||
if (automation == null || automation.Data is not Page page) | ||
{ | ||
return; | ||
} | ||
|
||
Console.WriteLine(JsonConvert.SerializeObject(page, Formatting.Indented)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Microsoft.Extensions.Logging; | ||
using System.Net; | ||
using System.Text; | ||
|
||
namespace TrickFireDiscordBot | ||
{ | ||
public class WebhookListener : IDisposable | ||
{ | ||
public event Action<HttpListenerRequest>? OnWebhookReceived; | ||
public ILogger Logger { get; } | ||
|
||
private readonly HttpListener _listener = new(); | ||
private bool _isRunning = false; | ||
|
||
public WebhookListener(ILogger logger, params string[] prefixes) | ||
{ | ||
Logger = logger; | ||
foreach (string prefix in prefixes) | ||
{ | ||
_listener.Prefixes.Add(prefix); | ||
} | ||
} | ||
|
||
public void Start() | ||
{ | ||
_ = Task.Run(async () => | ||
{ | ||
try | ||
{ | ||
await LongThread(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.LogError(ex, "Webhook Listener error"); | ||
} | ||
}); | ||
} | ||
|
||
public void Stop() | ||
{ | ||
_listener.Stop(); | ||
} | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
GC.SuppressFinalize(this); | ||
_listener.Close(); | ||
} | ||
|
||
public void Close() | ||
{ | ||
((IDisposable)this).Dispose(); | ||
} | ||
|
||
private async Task LongThread() | ||
{ | ||
_isRunning = true; | ||
_listener.Start(); | ||
|
||
while (_isRunning) | ||
{ | ||
try | ||
{ | ||
// Wait for request | ||
HttpListenerContext ctx = await _listener.GetContextAsync(); | ||
|
||
// Read request body | ||
OnWebhookReceived?.Invoke(ctx.Request); | ||
|
||
// Return ok response on request | ||
ctx.Response.StatusCode = (int)HttpStatusCode.OK; | ||
ctx.Response.OutputStream.Write(Encoding.UTF8.GetBytes("Ok")); | ||
ctx.Response.OutputStream.Close(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.LogError(ex, "Webhook Listener loop error"); | ||
} | ||
} | ||
} | ||
} | ||
} |