Пример #1
0
        public ValidationFailure Test(TelegramSettings settings)
        {
            try
            {
                const string title = "Test Notification";
                const string body  = "This is a test message from Radarr";

                SendNotification(title, body, settings);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Unable to send test message");

                if (ex is WebException webException)
                {
                    return(new ValidationFailure("Connection", $"{webException.Status.ToString()}: {webException.Message}"));
                }
                else if (ex is Common.Http.HttpException restException && 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);
        }
Пример #2
0
        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");

                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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        public void SendNotification(string title, string message, TelegramSettings settings)
        {
            //Format text to add the title before and bold using markdown
            var text    = $"<b>{HttpUtility.HtmlEncode(title)}</b>\n{HttpUtility.HtmlEncode(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", "HTML");
            request.AddParameter("text", text);
            request.AddParameter("disable_notification", settings.SendSilently);

            client.ExecuteAndValidate(request);
        }
Пример #5
0
        public void SendNotification(string title, string message, TelegramSettings settings)
        {
            //Format text to add the title before and bold using markdown
            var text = $"<b>{HttpUtility.HtmlEncode(title)}</b>\n{HttpUtility.HtmlEncode(message)}";

            var requestBuilder = new HttpRequestBuilder(URL).Resource("bot{token}/sendmessage").Post();

            var request = requestBuilder.SetSegment("token", settings.BotToken)
                          .AddFormParameter("chat_id", settings.ChatId)
                          .AddFormParameter("parse_mode", "HTML")
                          .AddFormParameter("text", text)
                          .AddFormParameter("disable_notification", settings.SendSilently)
                          .Build();

            _httpClient.Post(request);
        }