Skip to content

Instantly share code, notes, and snippets.

@crgrieve
Created October 30, 2021 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crgrieve/14271669f2e30a56d14eb56f0a25c558 to your computer and use it in GitHub Desktop.
Save crgrieve/14271669f2e30a56d14eb56f0a25c558 to your computer and use it in GitHub Desktop.
Discord notification for on publish in Umbraco
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