enhanced the discord notifications #3611

pull/3632/head
tidusjar 4 years ago
parent 51a886cef9
commit 31060fff61

@ -1,5 +1,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Ombi.Api.Discord.Models;
namespace Ombi.Api.Discord
@ -17,7 +18,7 @@ namespace Ombi.Api.Discord
public async Task SendMessage(DiscordWebhookBody body, string webhookId, string webhookToken)
{
var request = new Request($"webhooks/{webhookId}/{webhookToken}", BaseUrl, HttpMethod.Post);
request.AddJsonBody(body);
request.ApplicationJsonContentType();

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
namespace Ombi.Api.Discord.Models
{
@ -13,8 +14,32 @@ namespace Ombi.Api.Discord.Models
{
public string title { get; set; }
public string type => "rich"; // Always rich or embedded content
public string description { get; set; } // Don't really need to set this
public DiscordImage image { get; set; }
public string description { get; set; }
public DateTime timestamp => DateTime.Now;
public string color { get; set; }
public DiscordFooter footer { get; set; }
public DiscordImage thumbnail { get; set; }
public DiscordAuthor author { get; set; }
public List<DiscordField> fields { get; set; }
}
public class DiscordFooter
{
public string text { get; set; }
}
public class DiscordAuthor
{
public string name { get; set; }
public string url { get; set; }
public string iconurl { get; set; }
}
public class DiscordField
{
public string name { get; set; }
public string value { get; set; }
public bool inline { get; set; }
}
public class DiscordImage

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Ombi.Api.Discord;
@ -104,21 +105,65 @@ namespace Ombi.Notifications.Agents
username = settings.Username,
};
var fields = new List<DiscordField>();
if (model.Data.TryGetValue("RequestedUser", out var requestedUser))
{
if (requestedUser.HasValue())
{
fields.Add(new DiscordField { name = "Requsted By", value = requestedUser, inline = true });
}
}
if (model.Data.TryGetValue("DenyReason", out var denyReason))
{
if (denyReason.HasValue())
{
fields.Add(new DiscordField { name = "Denied Reason", value = denyReason, inline = true });
}
}
if (model.Data.TryGetValue("RequestStatus", out var status))
{
if (status.HasValue())
{
fields.Add(new DiscordField { name = "Status", value = status, inline = true });
}
}
var author = new DiscordAuthor
{
};
if (model.Data.TryGetValue("ApplicationUrl", out var appUrl))
{
author.url = appUrl;
}
if (model.Data.TryGetValue("ApplicationName", out var appName))
{
author.name = appName;
}
var embed = new DiscordEmbeds
{
fields = fields,
author = author
};
if (model.Data.TryGetValue("Title", out var title))
{
embed.title = title;
}
if (model.Data.TryGetValue("Overview", out var overview))
{
embed.description = overview;
}
string image;
if (model.Other.TryGetValue("image", out image))
{
discordBody.embeds = new List<DiscordEmbeds>
{
new DiscordEmbeds
{
image = new DiscordImage
{
url = image
}
}
};
embed.thumbnail = new DiscordImage { url = image };
}
discordBody.embeds = new List<DiscordEmbeds> { embed };
await Api.SendMessage(discordBody, settings.WebHookId, settings.Token);
}
catch (Exception e)
@ -148,6 +193,7 @@ namespace Ombi.Notifications.Agents
var notification = new NotificationMessage
{
Message = parsed.Message,
Data = parsed.Data.ToDictionary(x => x.Key, x => x.Value)
};
notification.Other.Add("image", parsed.Image);
await Send(notification, settings);

@ -64,6 +64,8 @@ namespace Ombi.Notifications
}
AdditionalInformation = opts?.AdditionalInformation ?? string.Empty;
CalculateRequestStatus(req);
}
public void Setup(NotificationOptions opts, AlbumRequest req, CustomizationSettings s, UserNotificationPreferences pref)
@ -107,6 +109,7 @@ namespace Ombi.Notifications
PosterImage = (req?.Cover.HasValue() ?? false) ? req.Cover : req?.Disk ?? string.Empty;
AdditionalInformation = opts?.AdditionalInformation ?? string.Empty;
CalculateRequestStatus(req);
}
public void SetupNewsletter(CustomizationSettings s)
@ -197,6 +200,7 @@ namespace Ombi.Notifications
EpisodesList = epSb.ToString();
SeasonsList = seasonSb.ToString();
CalculateRequestStatus(req);
}
public void Setup(OmbiUser user, CustomizationSettings s)
@ -220,6 +224,30 @@ namespace Ombi.Notifications
Type = opts.Substitutes.TryGetValue("RequestType", out val) ? val.Humanize() : string.Empty;
}
private void CalculateRequestStatus(BaseRequest req)
{
RequestStatus = string.Empty;
if (req != null)
{
if (req.Available)
{
RequestStatus = "Available";
return;
}
if (req.Denied ?? false)
{
RequestStatus = "Denied";
return;
}
if (!req.Available && req.Approved)
{
RequestStatus = "Processing Request";
return;
}
RequestStatus = "Pending Approval";
}
}
// User Defined
public string RequestId { get; set; }
public string RequestedUser { get; set; }
@ -245,6 +273,7 @@ namespace Ombi.Notifications
public string UserPreference { get; set; }
public string DenyReason { get; set; }
public string AvailableDate { get; set; }
public string RequestStatus { get; set; }
// System Defined
private string LongDate => DateTime.Now.ToString("D");
@ -282,6 +311,7 @@ namespace Ombi.Notifications
{nameof(UserPreference),UserPreference},
{nameof(DenyReason),DenyReason},
{nameof(AvailableDate),AvailableDate},
{nameof(RequestStatus),RequestStatus},
};
}
}

@ -1,56 +1,55 @@

<settings-menu></settings-menu>
<settings-menu></settings-menu>
<div *ngIf="form" class="small-middle-container">
<fieldset>
<fieldset class="top-space">
<legend>Discord Notifications</legend>
<div class="col-md-6">
<form novalidate [formGroup]="form" (ngSubmit)="onSubmit(form)">
<div class="form-group">
<div class="checkbox">
<input type="checkbox" id="enable" formControlName="enabled">
<label for="enable">Enabled</label>
<div class="row">
<div class="col-md-8">
<form novalidate [formGroup]="form" (ngSubmit)="onSubmit(form)">
<div class="row">
<div class="col-md-12 col-12 col-sm-12">
<div>
<div class="md-form-field">
<mat-slide-toggle formControlName="enabled" id="enable">Enable</mat-slide-toggle>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="webhookUrl" class="control-label">Webhook Url</label>
<input type="text" class="form-control form-control-custom " id="webhookUrl" name="webhookUrl" formControlName="webhookUrl" [ngClass]="{'form-error': form.get('webhookUrl').hasError('required')}">
<small *ngIf="form.get('webhookUrl').hasError('required')" class="error-text">The Webhook Url is required</small>
</div>
<div class="form-group">
<label for="username" class="control-label">Username</label>
<div>
<input type="text" class="form-control form-control-custom " id="username" name="username" formControlName="username" pTooltip="Optional, this will override the username you used for the Webhook">
<div class="row">
<div class="md-form-field">
<mat-form-field appearance="outline">
<mat-label>Webhook Url</mat-label>
<input matInput formControlName="webhookUrl">
</mat-form-field>
<mat-form-field appearance="outline">
<mat-label>Username</mat-label>
<input matInput formControlName="username">
</mat-form-field>
</div>
</div>
</div>
<div class="form-group">
<div>
<button [disabled]="form.invalid" type="button" (click)="test(form)" class="btn btn-primary-outline">
Test
<div id="spinner"></div>
</button>
<div class="row">
<div class="form-group">
<div>
<button mat-raised-button type="button" class="btn-spacing" color="accent"
(click)="test(form)" [disabled]="form.invalid">Test <div id="spinner"></div>
</button>
</div>
</div>
<div class="form-group">
<div>
<button mat-raised-button type="submit" class="btn-spacing" color="primary"
[disabled]="form.invalid">Submit</button>
</div>
</div>
</div>
</div>
<div class="form-group">
<div>
<button [disabled]="form.invalid" type="submit" id="save" class="btn btn-primary-outline">Submit</button>
</div>
</div>
</form>
</div>
</form>
</div>
<div class="col-md-6">
<notification-templates [templates]="templates" [showSubject]="false"></notification-templates>
<div class="col-md-4">
<notification-templates [templates]="templates" [showSubject]="false"></notification-templates>
</div>
</div>
</fieldset>
</div>
Loading…
Cancel
Save