discord webhooks

This commit is contained in:
Michael 2017-05-25 15:17:06 -04:00
parent 7c3d5156e1
commit 3f1236f22e
3 changed files with 51 additions and 0 deletions

View file

@ -145,6 +145,13 @@ public ActionResult AddRelease(PostDownloadViewModel model)
//Now we just save to the database...
db.Downloads.Add(download);
NotificationDaemon.ScreamToDiscord("New release: " + download.Name, $@"A new release of ShiftOS has been made!
Release name: {download.Name}
Stable: {download.IsStable}
Released on: {download.PostDate}", Url.Action("ViewRelease", "Downloads", new { id = download.Id }));
db.SaveChanges();
return RedirectToAction("Releases");

View file

@ -75,6 +75,7 @@ public enum AuditLogLevel
public class Configuration
{
public string Id { get; set; }
public string WebhookUrl { get; set; }
public string FeedbackEmail { get; set; }
public string SiteName { get; set; }
public string ReturnEmail { get; set; }

View file

@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.SignalR;
using Project_Unite.Models;
@ -110,6 +113,46 @@ public static void NotifyUser(string uid, string target, string title, string de
SendMessage(target, ComposeHtml(note));
}
internal static void ScreamToDiscord(string title, string desc, string url)
{
var db = new ApplicationDbContext();
var conf = db.Configs.FirstOrDefault();
if(conf != null)
{
if (!string.IsNullOrWhiteSpace(conf.WebhookUrl))
{
var wc = HttpWebRequest.Create(conf.WebhookUrl);
wc.Method = "POST";
wc.ContentType = "application/json";
string json = new JavaScriptSerializer().Serialize(new
{
content = $@"**{title}**
{desc}
Visit this URL to see more: {url}"
});
wc.ContentLength = json.Length;
using (var s = wc.GetRequestStream())
{
using(var writer = new StreamWriter(s))
{
writer.Write(json);
using(var r = wc.GetResponse())
{
using(var rs = r.GetResponseStream())
{
using(var reader = new StreamReader(rs))
{
string result = reader.ReadToEnd();
db.AuditLogs.Add(new AuditLog("system", AuditLogLevel.Admin, "Discord webhook sent. Result: " + result));
}
}
}
}
}
}
}
}
}
}