From 8a94298a8027194b7bd87ed9b1feaca1da5f718e Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Sat, 1 Feb 2020 23:15:53 +0000 Subject: [PATCH] Finished whatsapp --- src/Ombi.Api.Twilio/WhatsAppApi.cs | 6 --- .../ClientApp/src/app/interfaces/IUser.ts | 3 ++ .../services/applications/tester.service.ts | 5 +++ .../twilio/whatsapp.component.html | 2 +- .../twilio/whatsapp.component.ts | 6 +-- .../V1/External/TesterController.cs | 41 ++++++++++++++++++- 6 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Ombi.Api.Twilio/WhatsAppApi.cs b/src/Ombi.Api.Twilio/WhatsAppApi.cs index 0e5ce4347..5290a02ec 100644 --- a/src/Ombi.Api.Twilio/WhatsAppApi.cs +++ b/src/Ombi.Api.Twilio/WhatsAppApi.cs @@ -8,14 +8,8 @@ namespace Ombi.Api.Twilio { public class WhatsAppApi : IWhatsAppApi { - public async Task SendMessage(WhatsAppModel message, string accountSid, string authToken) { - // Find your Account Sid and Token at twilio.com/console - // DANGER! This is insecure. See http://twil.io/secure - //const string accountSid = "AC8a1b6ab0d9f351be8210ccc8f7930d27"; - //const string authToken = "f280272092780a770f7cd4fb0beed125"; - TwilioClient.Init(accountSid, authToken); var response =await MessageResource.CreateAsync( diff --git a/src/Ombi/ClientApp/src/app/interfaces/IUser.ts b/src/Ombi/ClientApp/src/app/interfaces/IUser.ts index 0816ad42e..9b0500891 100644 --- a/src/Ombi/ClientApp/src/app/interfaces/IUser.ts +++ b/src/Ombi/ClientApp/src/app/interfaces/IUser.ts @@ -91,6 +91,7 @@ export interface INotificationPreferences { } export enum INotificationAgent { + Email = 0, Discord = 1, Pushbullet = 2, @@ -99,4 +100,6 @@ export enum INotificationAgent { Slack = 5, Mattermost = 6, Mobile = 7, + Gotify = 8, + WhatsApp = 9 } diff --git a/src/Ombi/ClientApp/src/app/services/applications/tester.service.ts b/src/Ombi/ClientApp/src/app/services/applications/tester.service.ts index 1b7c55821..6c1d9ff6c 100644 --- a/src/Ombi/ClientApp/src/app/services/applications/tester.service.ts +++ b/src/Ombi/ClientApp/src/app/services/applications/tester.service.ts @@ -24,6 +24,7 @@ import { ISlackNotificationSettings, ISonarrSettings, ITelegramNotifcationSettings, + IWhatsAppSettings, } from "../../interfaces"; @Injectable() @@ -52,6 +53,10 @@ export class TesterService extends ServiceHelpers { return this.http.post(`${this.url}mattermost`, JSON.stringify(settings), {headers: this.headers}); } + public whatsAppTest(settings: IWhatsAppSettings): Observable { + return this.http.post(`${this.url}whatsapp`, JSON.stringify(settings), {headers: this.headers}); + } + public slackTest(settings: ISlackNotificationSettings): Observable { return this.http.post(`${this.url}slack`, JSON.stringify(settings), {headers: this.headers}); } diff --git a/src/Ombi/ClientApp/src/app/settings/notifications/twilio/whatsapp.component.html b/src/Ombi/ClientApp/src/app/settings/notifications/twilio/whatsapp.component.html index 666cc4cfd..1b6b155b8 100644 --- a/src/Ombi/ClientApp/src/app/settings/notifications/twilio/whatsapp.component.html +++ b/src/Ombi/ClientApp/src/app/settings/notifications/twilio/whatsapp.component.html @@ -28,7 +28,7 @@
- +
diff --git a/src/Ombi/ClientApp/src/app/settings/notifications/twilio/whatsapp.component.ts b/src/Ombi/ClientApp/src/app/settings/notifications/twilio/whatsapp.component.ts index e77653d38..80222e426 100644 --- a/src/Ombi/ClientApp/src/app/settings/notifications/twilio/whatsapp.component.ts +++ b/src/Ombi/ClientApp/src/app/settings/notifications/twilio/whatsapp.component.ts @@ -25,11 +25,11 @@ export class WhatsAppComponent { return; } - this.testerService.mattermostTest(form.value).subscribe(x => { + this.testerService.whatsAppTest(form.get("whatsAppSettings").value).subscribe(x => { if (x) { - this.notificationService.success( "Successfully sent a Mattermost message, please check the appropriate channel"); + this.notificationService.success( "Successfully sent a WhatsApp message, please check the appropriate channel"); } else { - this.notificationService.error("There was an error when sending the Mattermost message. Please check your settings"); + this.notificationService.error("There was an error when sending the WhatsApp message. Please check your settings"); } }); diff --git a/src/Ombi/Controllers/V1/External/TesterController.cs b/src/Ombi/Controllers/V1/External/TesterController.cs index 5a52adfb1..969e619ea 100644 --- a/src/Ombi/Controllers/V1/External/TesterController.cs +++ b/src/Ombi/Controllers/V1/External/TesterController.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Ombi.Api.CouchPotato; using Ombi.Api.Emby; @@ -10,7 +11,9 @@ using Ombi.Api.Plex; using Ombi.Api.Radarr; using Ombi.Api.SickRage; using Ombi.Api.Sonarr; +using Ombi.Api.Twilio; using Ombi.Attributes; +using Ombi.Core.Authentication; using Ombi.Core.Models.UI; using Ombi.Core.Notifications; using Ombi.Core.Settings.Models.External; @@ -22,6 +25,7 @@ using Ombi.Notifications.Models; using Ombi.Schedule.Jobs.Ombi; using Ombi.Settings.Settings.Models.External; using Ombi.Settings.Settings.Models.Notifications; +using Ombi.Store.Entities; namespace Ombi.Controllers.V1.External { @@ -40,7 +44,7 @@ namespace Ombi.Controllers.V1.External IPushbulletNotification pushbullet, ISlackNotification slack, IPushoverNotification po, IMattermostNotification mm, IPlexApi plex, IEmbyApi emby, IRadarrApi radarr, ISonarrApi sonarr, ILogger log, IEmailProvider provider, ICouchPotatoApi cpApi, ITelegramNotification telegram, ISickRageApi srApi, INewsletterJob newsletter, IMobileNotification mobileNotification, - ILidarrApi lidarrApi, IGotifyNotification gotifyNotification) + ILidarrApi lidarrApi, IGotifyNotification gotifyNotification, IWhatsAppApi whatsAppApi, OmbiUserManager um) { Service = service; DiscordNotification = notification; @@ -62,6 +66,8 @@ namespace Ombi.Controllers.V1.External MobileNotification = mobileNotification; LidarrApi = lidarrApi; GotifyNotification = gotifyNotification; + WhatsAppApi = whatsAppApi; + UserManager = um; } private INotificationService Service { get; } @@ -84,7 +90,8 @@ namespace Ombi.Controllers.V1.External private INewsletterJob Newsletter { get; } private IMobileNotification MobileNotification { get; } private ILidarrApi LidarrApi { get; } - + private IWhatsAppApi WhatsAppApi { get; } + private OmbiUserManager UserManager {get;} /// /// Sends a test message to discord using the provided settings @@ -459,5 +466,35 @@ namespace Ombi.Controllers.V1.External return false; } } + + [HttpPost("whatsapp")] + public async Task WhatsAppTest([FromBody] WhatsAppSettingsViewModel settings) + { + try + { + + var user = await UserManager.Users.Include(x => x.UserNotificationPreferences).FirstOrDefaultAsync(x => x.UserName == HttpContext.User.Identity.Name); + + + var status = await WhatsAppApi.SendMessage(new WhatsAppModel { + From = settings.From, + Message = "This is a test from Ombi!", + To = user.UserNotificationPreferences.FirstOrDefault(x => x.Agent == NotificationAgent.WhatsApp).Value + }, settings.AccountSid, settings.AuthToken); + if (status.HasValue()) + { + return true; + } + else + { + return false; + } + } + catch (Exception e) + { + Log.LogError(LoggingEvents.Api, e, "Could not test Lidarr"); + return false; + } + } } } \ No newline at end of file