| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using System.Net.Http; |
| | | 6 | | using SVETA.Api.Data.Domain; |
| | | 7 | | using SVETA.API.IntegrationTest.Common; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | |
| | | 10 | | namespace SVETA.Api.Helpers |
| | | 11 | | { |
| | | 12 | | public static class Telegram |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Токен |
| | | 16 | | /// </summary> |
| | 0 | 17 | | public static string Token { get; set; } |
| | | 18 | | /// <summary> |
| | | 19 | | /// id чата |
| | | 20 | | /// </summary> |
| | 0 | 21 | | public static string ChatId { get; set; } |
| | | 22 | | |
| | | 23 | | public static async Task SendNotification(string text) |
| | 560 | 24 | | { |
| | | 25 | | try |
| | 560 | 26 | | { |
| | 560 | 27 | | if (string.IsNullOrWhiteSpace(Token) || string.IsNullOrWhiteSpace(ChatId)) |
| | 560 | 28 | | return; |
| | 0 | 29 | | using (HttpClient client = new HttpClient()) |
| | 0 | 30 | | { |
| | 0 | 31 | | var url = $"https://api.telegram.org/bot{Token}/sendMessage"; |
| | 0 | 32 | | TelegramMessage message = new TelegramMessage |
| | 0 | 33 | | { |
| | 0 | 34 | | Text = text, |
| | 0 | 35 | | Chat_id = ChatId |
| | 0 | 36 | | }; |
| | 0 | 37 | | var mes = StringContentCreator.CreateContent(message); |
| | 0 | 38 | | var resp = await client.PostAsync(url, mes); |
| | 0 | 39 | | var content = await resp.Content.ReadAsStringAsync(); |
| | 0 | 40 | | } |
| | 0 | 41 | | } |
| | 0 | 42 | | catch (Exception e) |
| | 0 | 43 | | { |
| | 0 | 44 | | Console.WriteLine("Ошибка отправки сообщения в телеграм"); |
| | 0 | 45 | | Console.WriteLine(e.Message, e); |
| | 0 | 46 | | } |
| | 560 | 47 | | } |
| | | 48 | | } |
| | | 49 | | } |