mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-28 21:12:43 +02:00

committed by
Mark McDowall

parent
740fc9154f
commit
4a149c356b
72
src/NzbDrone.Core/Notifications/Telegram/TelegramService.cs
Normal file
72
src/NzbDrone.Core/Notifications/Telegram/TelegramService.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user