Обновил версию бото-либы до 19.0

This commit is contained in:
ftc
2024-01-17 20:38:41 +03:00
parent 1d73b4d8be
commit 77d563d500
6 changed files with 39 additions and 28 deletions
+30 -14
View File
@@ -28,26 +28,42 @@ namespace ZaoBot
public void Run()
{
var token = config.RootElement.GetProperty("token").GetString();
shouldRestart = false;
botClient = new TelegramBotClient(token);
openhab = new OpenHab.OpenHabClient(config.RootElement.GetProperty("openhab_url").GetString());
var me = botClient.GetMeAsync().Result;
Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
botClient.OnMessage += OnBotMessage;
botClient.StartReceiving();
while (!shouldRestart)
while (true)
{
Thread.Sleep(1000);
shouldRestart = false;
botClient = new TelegramBotClient(token);
using CancellationTokenSource cts = new CancellationTokenSource();
var me = botClient.GetMeAsync().Result;
Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
botClient.StartReceiving(
updateHandler: OnBotUpdateAsync,
pollingErrorHandler: OnPollingError
);
while (!shouldRestart)
{
Thread.Sleep(1000);
}
}
}
private async void OnBotMessage(object sender, MessageEventArgs e)
private Task OnPollingError(ITelegramBotClient bot, Exception exception, CancellationToken cts)
{
Console.WriteLine("Error on polling: " + exception.Message);
Console.WriteLine("Will restart");
return Task.CompletedTask;
}
async Task OnBotUpdateAsync(ITelegramBotClient bot, Telegram.Bot.Types.Update updateInfo, CancellationToken cts)
{
if (updateInfo.Message == null) return;
try
{
if (e.Message.Text != null)
if (updateInfo.Message.Text != null)
{
var id = e.Message.Chat.Id;
var id = updateInfo.Message.Chat.Id;
IUserState state = null;
if (userStates.ContainsKey(id))
{
@@ -57,15 +73,15 @@ namespace ZaoBot
{
state = new MainMenuUserState(botClient, config, openhab);
}
state = await state.ProcessMessage(state, e);
state = await state.ProcessMessage(state, updateInfo);
if (state == null)
{
await botClient.SendTextMessageAsync(
chatId: e.Message.Chat,
chatId: updateInfo.Message.Chat,
text: "Я запутался, давайте сначала"
);
state = new MainMenuUserState(botClient, config, openhab);
state = await state.ProcessMessage(state, e);
state = await state.ProcessMessage(state, updateInfo);
}
userStates[id] = state;
}