Обновил версию бото-либы до 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;
}
+3 -6
View File
@@ -6,8 +6,6 @@ using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types.InputFiles;
using Telegram.Bot.Types.ReplyMarkups;
using ZaoBot.OpenHab;
@@ -45,7 +43,7 @@ namespace ZaoBot
}
}
public async Task<IUserState> ProcessMessage(IUserState previous, MessageEventArgs e)
public async Task<IUserState> ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e)
{
if (e.Message.Text == CMD_BACK)
{
@@ -78,8 +76,7 @@ namespace ZaoBot
buttons[cameras.Count] = new KeyboardButton[] { CMD_BACK };
var replyKeyboardMarkup = new ReplyKeyboardMarkup(
buttons,
resizeKeyboard: true
buttons
);
await botClient.SendTextMessageAsync(
@@ -102,7 +99,7 @@ namespace ZaoBot
{
await botClient.SendDocumentAsync(
chatId: chat,
document: new InputOnlineFile(content: stream, fileName: cam.name + ".jpg")
document: Telegram.Bot.Types.InputFile.FromStream(stream: stream, fileName: cam.name + ".jpg")
);
}
}
+1 -1
View File
@@ -8,6 +8,6 @@ namespace ZaoBot
{
interface IUserState
{
Task<IUserState> ProcessMessage(IUserState previous, MessageEventArgs e);
Task<IUserState> ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e);
}
}
+2 -3
View File
@@ -31,7 +31,7 @@ namespace ZaoBot
this.openhab = openhab;
}
public async Task<IUserState> ProcessMessage(IUserState previous, MessageEventArgs e)
public async Task<IUserState> ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e)
{
var response = "";
if (e != null)
@@ -89,8 +89,7 @@ namespace ZaoBot
new KeyboardButton[] { CMD_LEFT_GATE, CMD_RIGHT_GATE },
new KeyboardButton[] { CMD_STATUS },
new KeyboardButton[] { CMD_CAMERAS },
},
resizeKeyboard: true
}
);
await botClient.SendTextMessageAsync(
+2 -3
View File
@@ -27,7 +27,7 @@ namespace ZaoBot
this.openhab = openhab;
}
public async Task<IUserState> ProcessMessage(IUserState previous, MessageEventArgs e)
public async Task<IUserState> ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e)
{
var response = "";
if (previous != this)
@@ -59,8 +59,7 @@ namespace ZaoBot
new KeyboardButton[][]
{
new KeyboardButton[] { CMD_BACK },
},
resizeKeyboard: true
}
);
await botClient.SendTextMessageAsync(
+1 -1
View File
@@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="15.7.1" />
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
</ItemGroup>
</Project>