Обновил версию бото-либы до 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
+25 -9
View File
@@ -28,26 +28,42 @@ namespace ZaoBot
public void Run() public void Run()
{ {
var token = config.RootElement.GetProperty("token").GetString(); var token = config.RootElement.GetProperty("token").GetString();
openhab = new OpenHab.OpenHabClient(config.RootElement.GetProperty("openhab_url").GetString());
while (true)
{
shouldRestart = false; shouldRestart = false;
botClient = new TelegramBotClient(token); botClient = new TelegramBotClient(token);
openhab = new OpenHab.OpenHabClient(config.RootElement.GetProperty("openhab_url").GetString());
using CancellationTokenSource cts = new CancellationTokenSource();
var me = botClient.GetMeAsync().Result; var me = botClient.GetMeAsync().Result;
Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}."); Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
botClient.OnMessage += OnBotMessage; botClient.StartReceiving(
botClient.StartReceiving(); updateHandler: OnBotUpdateAsync,
pollingErrorHandler: OnPollingError
);
while (!shouldRestart) while (!shouldRestart)
{ {
Thread.Sleep(1000); 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 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; IUserState state = null;
if (userStates.ContainsKey(id)) if (userStates.ContainsKey(id))
{ {
@@ -57,15 +73,15 @@ namespace ZaoBot
{ {
state = new MainMenuUserState(botClient, config, openhab); state = new MainMenuUserState(botClient, config, openhab);
} }
state = await state.ProcessMessage(state, e); state = await state.ProcessMessage(state, updateInfo);
if (state == null) if (state == null)
{ {
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: e.Message.Chat, chatId: updateInfo.Message.Chat,
text: "Я запутался, давайте сначала" text: "Я запутался, давайте сначала"
); );
state = new MainMenuUserState(botClient, config, openhab); state = new MainMenuUserState(botClient, config, openhab);
state = await state.ProcessMessage(state, e); state = await state.ProcessMessage(state, updateInfo);
} }
userStates[id] = state; userStates[id] = state;
} }
+3 -6
View File
@@ -6,8 +6,6 @@ using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types.InputFiles;
using Telegram.Bot.Types.ReplyMarkups; using Telegram.Bot.Types.ReplyMarkups;
using ZaoBot.OpenHab; 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) if (e.Message.Text == CMD_BACK)
{ {
@@ -78,8 +76,7 @@ namespace ZaoBot
buttons[cameras.Count] = new KeyboardButton[] { CMD_BACK }; buttons[cameras.Count] = new KeyboardButton[] { CMD_BACK };
var replyKeyboardMarkup = new ReplyKeyboardMarkup( var replyKeyboardMarkup = new ReplyKeyboardMarkup(
buttons, buttons
resizeKeyboard: true
); );
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
@@ -102,7 +99,7 @@ namespace ZaoBot
{ {
await botClient.SendDocumentAsync( await botClient.SendDocumentAsync(
chatId: chat, 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 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; 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 = ""; var response = "";
if (e != null) if (e != null)
@@ -89,8 +89,7 @@ namespace ZaoBot
new KeyboardButton[] { CMD_LEFT_GATE, CMD_RIGHT_GATE }, new KeyboardButton[] { CMD_LEFT_GATE, CMD_RIGHT_GATE },
new KeyboardButton[] { CMD_STATUS }, new KeyboardButton[] { CMD_STATUS },
new KeyboardButton[] { CMD_CAMERAS }, new KeyboardButton[] { CMD_CAMERAS },
}, }
resizeKeyboard: true
); );
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
+2 -3
View File
@@ -27,7 +27,7 @@ namespace ZaoBot
this.openhab = openhab; 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 = ""; var response = "";
if (previous != this) if (previous != this)
@@ -59,8 +59,7 @@ namespace ZaoBot
new KeyboardButton[][] new KeyboardButton[][]
{ {
new KeyboardButton[] { CMD_BACK }, new KeyboardButton[] { CMD_BACK },
}, }
resizeKeyboard: true
); );
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
+1 -1
View File
@@ -6,7 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Telegram.Bot" Version="15.7.1" /> <PackageReference Include="Telegram.Bot" Version="19.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>