New: Telegram notifications

Closes #1355
pull/1570/head
ARTbird309 8 years ago committed by Mark McDowall
parent 740fc9154f
commit 4a149c356b

@ -0,0 +1,15 @@
using System;
namespace NzbDrone.Core.Notifications.Telegram
{
public class InvalidResponseException : Exception
{
public InvalidResponseException()
{
}
public InvalidResponseException(string message) : base(message)
{
}
}
}

@ -0,0 +1,65 @@
using System.Collections.Generic;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Notifications.Telegram
{
public class Telegram : NotificationBase<TelegramSettings>
{
private readonly ITelegramProxy _proxy;
public Telegram(ITelegramProxy proxy)
{
_proxy = proxy;
}
public override string Link
{
get { return "https://telegram.org/"; }
}
public override void OnGrab(GrabMessage grabMessage)
{
const string title = "Episode Grabbed";
_proxy.SendNotification(title, grabMessage.Message, Settings);
}
public override void OnDownload(DownloadMessage message)
{
const string title = "Episode Downloaded";
_proxy.SendNotification(title, message.Message, Settings);
}
public override void OnRename(Series series)
{
}
public override string Name
{
get
{
return "Telegram";
}
}
public override bool SupportsOnRename
{
get
{
return false;
}
}
public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(_proxy.Test(Settings));
return new ValidationResult(failures);
}
}
}

@ -0,0 +1,14 @@
using Newtonsoft.Json;
namespace NzbDrone.Core.Notifications.Telegram
{
public class TelegramError
{
public bool Ok { get; set; }
[JsonProperty(PropertyName = "error_code")]
public int ErrorCode { get; set; }
public string Description { get; set; }
}
}

@ -0,0 +1,72 @@
using System;
using System.Net;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Serializer;
using RestSharp;
using NzbDrone.Core.Rest;
namespace NzbDrone.Core.Notifications.Telegram
{
public interface ITelegramProxy
{
void SendNotification(string title, string message, TelegramSettings settings);
ValidationFailure Test(TelegramSettings settings);
}
public class TelegramProxy : ITelegramProxy
{
private readonly Logger _logger;
private const string URL = "https://api.telegram.org";
public TelegramProxy(Logger logger)
{
_logger = logger;
}
public void SendNotification(string title, string message, TelegramSettings settings)
{
//Format text to add the title before and bold using markdown
var text = $"*{title}*\n{message}";
var client = RestClientFactory.BuildClient(URL);
var request = new RestRequest("bot{token}/sendmessage", Method.POST);
request.AddUrlSegment("token", settings.BotToken);
request.AddParameter("chat_id", settings.ChatId);
request.AddParameter("parse_mode", "Markdown");
request.AddParameter("text", text);
client.ExecuteAndValidate(request);
}
public ValidationFailure Test(TelegramSettings settings)
{
try
{
const string title = "Test Notification";
const string body = "This is a test message from Sonarr";
SendNotification(title, body, settings);
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to send test message: " + ex.Message);
var restException = ex as RestException;
if (restException != null && restException.Response.StatusCode == HttpStatusCode.BadRequest)
{
var error = Json.Deserialize<TelegramError>(restException.Response.Content);
var property = error.Description.ContainsIgnoreCase("chat not found") ? "ChatId" : "BotToken";
return new ValidationFailure(property, error.Description);
}
return new ValidationFailure("BotToken", "Unable to send test message");
}
return null;
}
}
}

@ -0,0 +1,40 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Telegram
{
public class TelegramSettingsValidator : AbstractValidator<TelegramSettings>
{
public TelegramSettingsValidator()
{
RuleFor(c => c.BotToken).NotEmpty();
RuleFor(c => c.ChatId).NotEmpty();
}
}
public class TelegramSettings : IProviderConfig
{
private static readonly TelegramSettingsValidator Validator = new TelegramSettingsValidator();
[FieldDefinition(0, Label = "Bot Token", HelpLink = "https://core.telegram.org/bots")]
public string BotToken { get; set; }
[FieldDefinition(1, Label = "Chat ID", HelpLink = "http://stackoverflow.com/a/37396871/882971", HelpText = "You must start a conversation with the bot or add it to your group to receive messages")]
public string ChatId { get; set; }
public bool IsValid
{
get
{
return !string.IsNullOrWhiteSpace(ChatId) && !string.IsNullOrWhiteSpace(BotToken);
}
}
public NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

@ -842,6 +842,10 @@
<Compile Include="Notifications\Synology\SynologyIndexer.cs" />
<Compile Include="Notifications\Synology\SynologyIndexerProxy.cs" />
<Compile Include="Notifications\Synology\SynologyIndexerSettings.cs" />
<Compile Include="Notifications\Telegram\InvalidResponseException.cs" />
<Compile Include="Notifications\Telegram\Telegram.cs" />
<Compile Include="Notifications\Telegram\TelegramService.cs" />
<Compile Include="Notifications\Telegram\TelegramSettings.cs" />
<Compile Include="Notifications\Twitter\OAuthToken.cs" />
<Compile Include="Notifications\Twitter\TwitterException.cs" />
<Compile Include="Notifications\Webhook\WebhookEpisode.cs" />
@ -1173,6 +1177,7 @@
<Link>libsqlite3.0.dylib</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Compile Include="Notifications\Telegram\TelegramError.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

Loading…
Cancel
Save