-
Notifications
You must be signed in to change notification settings - Fork 0
/
SenPlus.cs
57 lines (44 loc) · 1.61 KB
/
SenPlus.cs
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
namespace SenPlus.Core;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
public class SenPlus
{
public TelegramBotClient _Bot { get; private init; }
public Func<ITelegramBotClient, Update, CancellationToken, Task>? _HandleUpdate { get; set; }
public Func<ITelegramBotClient, Exception, CancellationToken, Task>? _HandlePollingError { get; set; }
public ReceiverOptions? _ReceiverOptions { get; set; }
public static Dictionary<string, Func<ITelegramBotClient, Update, CancellationToken, Task>>? _Commands { get; set; } = null;
public SenPlus(TelegramBotClient bot)
{
_Bot = bot;
}
public SenPlus(
TelegramBotClient Bot,
Func<ITelegramBotClient, Update, CancellationToken, Task>? HandleUpdate = null,
Func<ITelegramBotClient, Exception, CancellationToken, Task>? HandlePollingError = null,
ReceiverOptions? ReceiverOptions = null,
Dictionary<string, Func<ITelegramBotClient, Update, CancellationToken, Task>>? Commands = null)
{
_Bot = Bot;
_HandleUpdate = HandleUpdate;
_HandlePollingError = HandlePollingError;
_ReceiverOptions = ReceiverOptions;
_Commands = Commands;
}
public async Task StartReceivingAsync()
{
using CancellationTokenSource Cts = new();
_Bot.StartReceiving(
updateHandler: _HandleUpdate,
pollingErrorHandler: _HandlePollingError,
receiverOptions: _ReceiverOptions,
cancellationToken: Cts.Token
);
var me = await _Bot.GetMeAsync();
Console.WriteLine($"Start listening for @{me.Username}");
Console.ReadLine();
// Send cancellation request to stop bot
Cts.Cancel();
}
}