Skip to content

Commit 1b0e477

Browse files
committed
Поправил баги в FileInlineConverter
1 parent 51c673a commit 1b0e477

File tree

9 files changed

+100
-16
lines changed

9 files changed

+100
-16
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using ConsoleExample.Models.CommandHeaders;
2+
using PRTelegramBot.Attributes;
3+
using PRTelegramBot.Builders.Keyboard;
4+
using PRTelegramBot.Extensions;
5+
using PRTelegramBot.Interfaces;
6+
using PRTelegramBot.Models;
7+
using PRTelegramBot.Models.CallbackCommands;
8+
using PRTelegramBot.Models.InlineButtons;
9+
using PRTelegramBot.Services.Messages;
10+
11+
namespace ConsoleExample.Examples.Builders
12+
{
13+
public class InlineKeyboard
14+
{
15+
16+
[ReplyMenuHandler("GInlineK")]
17+
public static async Task GenerateKeyboard(IBotContext context)
18+
{
19+
var keyboard = new InlineKeyboardBuilder();
20+
var cars = 13;
21+
for (int i = 0; i < cars; i++)
22+
{
23+
keyboard.AddButton(new InlineCallback<EntityTCommand<long>>($"Car {i}", SelectHeader.Car, new EntityTCommand<long> { EntityId = i }), newRow: true);
24+
}
25+
26+
var resultKeyboard = keyboard.Build();
27+
var option = new OptionMessage { MenuInlineKeyboardMarkup = resultKeyboard };
28+
await MessageSender.Send(context, "Select a car:", option);
29+
}
30+
31+
[InlineCallbackHandler<SelectHeader>(SelectHeader.Car)]
32+
public static async Task ExecuteAsync(IBotContext context)
33+
{
34+
var command = context.GetCommandByCallbackOrNull<EntityTCommand<long>>();
35+
await MessageSender.Send(context, $"Selected {command.Data.EntityId}");
36+
}
37+
}
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using PRTelegramBot.Attributes;
2+
3+
namespace ConsoleExample.Models.CommandHeaders
4+
{
5+
[InlineCommand]
6+
public enum SelectHeader
7+
{
8+
Car = 250
9+
}
10+
}

Examples/ConsoleExample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//Обход ограничения telegram на 64 байта для callback_data.
3232
.SetInlineMenuConverter(new FileInlineConverter())
3333
// ToonSerializerWrapper использует меньше байт при сериализации данных по сравнению с JsonSerializer.
34-
.SetInlineSerializer(new ToonSerializerWrapper())
34+
//.SetInlineSerializer(new ToonSerializerWrapper())
3535
.SetInitializeAction(() => { Console.WriteLine("Custom initialize complete."); })
3636
.AddBackgroundTask(new HelloWorldBackgroundTask())
3737
.AddBackgroundTask(new AttributeBackgroundTask())

PRTelegramBot/CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22

3-
## 2025-01-03 - v0.9.5
3+
## 2026-02-02 - v0.9.6
4+
5+
### 🧩 Common
6+
- Поправил баги в FileInlineConverter. Теперь корректно формируется билдер inline кнопок.
7+
8+
## 2026-01-03 - v0.9.5
49

510
### 🧩 Common
611
- В PRBotBase добавлен метод SetServiceProvider.
@@ -12,12 +17,12 @@
1217
3. ILoggerFactory из DI
1318
4. Встроенная фабрика логеров (fallback)
1419

15-
## 2025-01-03 - v0.9.4
20+
## 2026-01-03 - v0.9.4
1621

1722
### 🧩 Common
1823
- Telegram.Bot: обновлен до 22.8.1
1924

20-
## 2025-01-02 - v0.9.3
25+
## 2026-01-02 - v0.9.3
2126

2227
### 🧩 Common
2328
- Telegram.Bot: обновлен до 22.8.0

PRTelegramBot/Converters/Inline/FileInlineConverter.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using PRTelegramBot.Interfaces;
44
using PRTelegramBot.Models.CallbackCommands;
55
using PRTelegramBot.Models.InlineButtons;
6+
using PRTelegramBot.Utils;
67
using System.Collections.Concurrent;
78

89
namespace PRTelegramBot.Converters.Inline
@@ -29,15 +30,15 @@ public class FileInlineConverter : IInlineMenuConverter
2930
/// <inheritdoc />
3031
public string GenerateCallbackData(InlineCallback inlineCallback)
3132
{
32-
BaseConvert(inlineCallback.CommandType, () => CurrentScope.Bot.GetSerializer().Serialize(inlineCallback));
33-
return GetUserKey(inlineCallback.CommandType);
33+
var hash = BaseConvert(inlineCallback.CommandType, () => CurrentScope.Bot.GetSerializer().Serialize(inlineCallback));
34+
return GetKey(GetUserKey(inlineCallback.CommandType), hash);
3435
}
3536

3637
/// <inheritdoc />
3738
public string GenerateCallbackData<T>(InlineCallback<T> inlineCallback) where T : TCommandBase
3839
{
39-
BaseConvert(inlineCallback.CommandType, () => CurrentScope.Bot.GetSerializer().Serialize<InlineCallback<T>>(inlineCallback));
40-
return GetUserKey(inlineCallback.CommandType);
40+
var hash = BaseConvert(inlineCallback.CommandType, () => CurrentScope.Bot.GetSerializer().Serialize<InlineCallback<T>>(inlineCallback));
41+
return GetKey(GetUserKey(inlineCallback.CommandType), hash);
4142
}
4243

4344
/// <inheritdoc />
@@ -70,22 +71,23 @@ public string GenerateCallbackData<T>(InlineCallback<T> inlineCallback) where T
7071

7172
private string GetUserKey(Enum command)
7273
{
73-
return CurrentScope.Context.Update.GetInlineKey(command);
74+
return CurrentScope.Context.Update.GetKeyMappingUserTelegram();
7475
}
7576

7677
private string BaseConvert(Enum command, Func<string> convert)
7778
{
7879
try
7980
{
8081
var userKey = GetUserKey(command);
81-
var filePath = Path.Combine(basePath, $"{userKey}.json");
8282
var data = convert();
83+
var hashString = StringUtils.HashForFileName(data, 12);
84+
var filePath = Path.Combine(basePath, $"{GetKey(userKey, hashString)}.json");
8385

8486
if (!Directory.Exists(basePath))
8587
Directory.CreateDirectory(basePath);
8688

8789
File.WriteAllText(filePath, data);
88-
return data;
90+
return hashString;
8991
}
9092
catch(Exception ex)
9193
{
@@ -94,6 +96,11 @@ private string BaseConvert(Enum command, Func<string> convert)
9496
}
9597
}
9698

99+
private string GetKey(string userKey, string hash)
100+
{
101+
return $"{userKey}-{hash}";
102+
}
103+
97104
private string GetAppPath()
98105
{
99106
return AppContext.BaseDirectory;

PRTelegramBot/PRTelegramBot.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<LangVersion>latest</LangVersion>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<Nullable>enable</Nullable>
9-
<Version>0.9.5</Version>
9+
<Version>0.9.6</Version>
1010
<Authors>Ilya Samarin</Authors>
1111
<Company />
1212
<RepositoryType>git</RepositoryType>

PRTelegramBot/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![Static Badge](https://img.shields.io/badge/version-v0.9.5-brightgreen) ![Static Badge](https://img.shields.io/badge/telegram.bot-22.8.1-blue) ![NuGet Downloads](https://img.shields.io/nuget/dt/prtelegrambot) ![NuGet Version](https://img.shields.io/nuget/v/prtelegrambot)
1+
![Static Badge](https://img.shields.io/badge/version-v0.9.6-brightgreen) ![Static Badge](https://img.shields.io/badge/telegram.bot-22.8.1-blue) ![NuGet Downloads](https://img.shields.io/nuget/dt/prtelegrambot) ![NuGet Version](https://img.shields.io/nuget/v/prtelegrambot)
22

33

44
> Если проект был вам полезен, вы можете поддержать его развитие на Boosty:
@@ -15,7 +15,7 @@ TelegramBot v22.8.1 https://github.com/TelegramBots/Telegram.Bot
1515
# 📰 Описание
1616
Фреймворк с открытым исходным кодом с гибким и простым функционалом для создания ботов Telegram.
1717
Примеры https://github.com/prethink/PRTelegramBot/tree/master/Examples
18-
Примеры с видео: https://github.com/prethink/PRTelegramYoutube
18+
Примеры с видео: [https://github.com/prethink/PRTelegramYoutube ](https://github.com/prethink/PRTelegramYoutubeOld)
1919

2020
# 💎 Функционал
2121

PRTelegramBot/Utils/StringUtils.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Security.Cryptography;
2+
using System.Text;
3+
4+
namespace PRTelegramBot.Utils
5+
{
6+
/// <summary>
7+
/// Утилиты для работы со строками.
8+
/// </summary>
9+
public static class StringUtils
10+
{
11+
/// <summary>
12+
/// Хеш для имени файла.
13+
/// </summary>
14+
/// <param name="input">Строка.</param>
15+
/// <param name="limit">Строка.</param>
16+
/// <returns>Хэш.</returns>
17+
public static string HashForFileName(string input, int limit = 16)
18+
{
19+
using var sha = SHA256.Create();
20+
var bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
21+
return Convert.ToHexString(bytes).ToLowerInvariant().Substring(0, limit);
22+
}
23+
}
24+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![Static Badge](https://img.shields.io/badge/version-v0.9.5-brightgreen) ![Static Badge](https://img.shields.io/badge/telegram.bot-22.8.1-blue) ![NuGet Downloads](https://img.shields.io/nuget/dt/prtelegrambot) ![NuGet Version](https://img.shields.io/nuget/v/prtelegrambot)
1+
![Static Badge](https://img.shields.io/badge/version-v0.9.6-brightgreen) ![Static Badge](https://img.shields.io/badge/telegram.bot-22.8.1-blue) ![NuGet Downloads](https://img.shields.io/nuget/dt/prtelegrambot) ![NuGet Version](https://img.shields.io/nuget/v/prtelegrambot)
22

33

44
> Если проект был вам полезен, вы можете поддержать его развитие на Boosty:
@@ -15,7 +15,7 @@ TelegramBot v22.8.1 https://github.com/TelegramBots/Telegram.Bot
1515
# 📰 Описание
1616
Фреймворк с открытым исходным кодом с гибким и простым функционалом для создания ботов Telegram.
1717
Примеры https://github.com/prethink/PRTelegramBot/tree/master/Examples
18-
Примеры с видео: [https://github.com/prethink/PRTelegramYoutube ](https://github.com/prethink/PRTelegramYoutubeOld)
18+
Примеры с видео: https://github.com/prethink/PRTelegramYoutube
1919

2020
# 💎 Функционал
2121

0 commit comments

Comments
 (0)