Created
October 30, 2021 08:19
-
-
Save crgrieve/14271669f2e30a56d14eb56f0a25c558 to your computer and use it in GitHub Desktop.
Discord notification for on publish in Umbraco
This file contains hidden or 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
using Newtonsoft.Json; | |
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; | |
using Umbraco.Cms.Core.Events; | |
using Umbraco.Cms.Core.Notifications; | |
namespace UmbracoHackathon.Notifications | |
{ | |
public class DiscordNotification : INotificationHandler<ContentPublishingNotification> | |
{ | |
public void Handle(ContentPublishingNotification notification) | |
{ | |
HttpClient client = new HttpClient(); | |
foreach (var contentItem in notification.PublishedEntities) | |
{ | |
var discordMessage = new DiscordMessage(){ | |
Content=contentItem.Name + " published", | |
Username = "UmbracoBot" | |
}; | |
var content = new StringContent(JsonConvert.SerializeObject(discordMessage), Encoding.UTF8, "application/json"); | |
client.PostAsync("YOUR_WEBHOOK_URL", content); | |
} | |
} | |
public class DiscordMessage | |
{ | |
[JsonProperty("content")] | |
public string Content { get; set; } = ""; | |
[JsonProperty("username")] | |
public string Username { get; set; } = ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment