Files
telegram-bot/modules/DebugTools/Handler.cs
T
2026-05-09 18:12:22 +03:00

42 lines
1.3 KiB
C#

using System.Text.Encodings.Web;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Utils;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Service;
using System.Text.Json;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
namespace West.TelegramBot.DebugTools;
public class Handler : BotEventHandler
{
private readonly UserService _userService;
private static readonly JsonSerializerOptions DumpOptions = new(JsonBotAPI.Options)
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
public Handler(UserService userService)
{
_userService = userService;
}
[Command("dump", CommandParseMode.Both)]
public async Task HandleDump()
{
var isChatAdmin = (Chat.Type == ChatType.Private || await Bot.IsUserAdminAsync(Chat, From));
if (!_userService.IsUserSuper(From) && !isChatAdmin)
{
return;
}
var message = RawUpdate.Message!.ReplyToMessage ?? RawUpdate.Message;
var text = HtmlString.Empty
.CodeWithStyle("language-json", JsonSerializer.Serialize(message, DumpOptions));
await Bot.SendHtmlStringAsync(Chat, text, replyTo: message.MessageId);
}
}