Repository navigation
Expand file tree
/
Copy pathNotificar.cs
More file actions
65 lines (55 loc) · 2.97 KB
/
Copy pathNotificar.cs
File metadata and controls
65 lines (55 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Threading;
using System.Threading.Tasks;
namespace JLOrdaz.TelegramBotMessage
{
public sealed class TelegramNotifier : IDisposable
{
private readonly TelegramMessage telegramMessage;
private readonly string chatId;
/// <summary>
/// Initializes a new instance of the <see cref="TelegramNotifier"/> class.
/// </summary>
/// <param name="botToken">The token for the Telegram bot.</param>
/// <param name="chatId">The chat ID to send messages to.</param>
public TelegramNotifier(string botToken, string chatId)
{
if (string.IsNullOrWhiteSpace(chatId))
{
throw new ArgumentException("A chat id is required.", nameof(chatId));
}
telegramMessage = new TelegramMessage(botToken);
this.chatId = chatId;
}
/// <summary>
/// Sends a message to the specified chat.
/// </summary>
/// <param name="message">The message to send.</param>
/// <param name="cancellationToken">A token that can be used to cancel the operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response from the Telegram API.</returns>
public Task<string> SendMessageAsync(string message, CancellationToken cancellationToken = default)
=> telegramMessage.SendMessage(chatId, message, cancellationToken);
/// <summary>
/// Sends an informational message to the specified chat.
/// </summary>
/// <param name="message">The message to send.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response from the Telegram API.</returns>
public Task<string> SendInfoMessageAsync(string message, CancellationToken cancellationToken = default)
=> SendMessageAsync($"ℹ {message}", cancellationToken);
/// <summary>
/// Sends a warning message to the specified chat.
/// </summary>
/// <param name="message">The message to send.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response from the Telegram API.</returns>
public Task<string> SendWarningMessageAsync(string message, CancellationToken cancellationToken = default)
=> SendMessageAsync($"⚠ {message}", cancellationToken);
/// <summary>
/// Sends an error message to the specified chat.
/// </summary>
/// <param name="message">The message to send.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response from the Telegram API.</returns>
public Task<string> SendErrorMessageAsync(string message, CancellationToken cancellationToken = default)
=> SendMessageAsync($"❗ {message}", cancellationToken);
public void Dispose() => telegramMessage.Dispose();
}
}