feat: add telegram mini app dashboard
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace GmRelay.Bot.Infrastructure.Telegram;
|
||||
|
||||
public sealed class TelegramMiniAppMenuButtonService(
|
||||
ITelegramBotClient bot,
|
||||
IConfiguration configuration,
|
||||
ILogger<TelegramMiniAppMenuButtonService> logger) : IHostedService
|
||||
{
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var miniAppUrl = configuration["Telegram:MiniAppUrl"];
|
||||
if (string.IsNullOrWhiteSpace(miniAppUrl))
|
||||
{
|
||||
logger.LogInformation("Telegram Mini App URL is not configured; menu button setup skipped.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Uri.TryCreate(miniAppUrl, UriKind.Absolute, out var uri) ||
|
||||
(uri.Scheme != Uri.UriSchemeHttps && !uri.IsLoopback))
|
||||
{
|
||||
logger.LogWarning("Telegram Mini App URL {MiniAppUrl} is not a valid HTTPS URL.", miniAppUrl);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await bot.SetChatMenuButton(
|
||||
menuButton: new MenuButtonWebApp
|
||||
{
|
||||
Text = "Dashboard",
|
||||
WebApp = new WebAppInfo(miniAppUrl)
|
||||
},
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
logger.LogInformation("Telegram Mini App menu button configured for {MiniAppUrl}.", miniAppUrl);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to configure Telegram Mini App menu button.");
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using GmRelay.Bot.Features.Sessions.RescheduleSession;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
|
||||
namespace GmRelay.Bot.Infrastructure.Telegram;
|
||||
|
||||
@@ -30,6 +31,7 @@ public sealed class UpdateRouter(
|
||||
HandleRescheduleTimeInputHandler rescheduleTimeInputHandler,
|
||||
HandleRescheduleVoteHandler rescheduleVoteHandler,
|
||||
ITelegramBotClient bot,
|
||||
IConfiguration configuration,
|
||||
ILogger<UpdateRouter> logger) : ITelegramUpdateHandler
|
||||
{
|
||||
public async Task RouteAsync(Update update, CancellationToken ct)
|
||||
@@ -188,10 +190,7 @@ public sealed class UpdateRouter(
|
||||
switch (command)
|
||||
{
|
||||
case "/start":
|
||||
await bot.SendMessage(
|
||||
chatId: message.Chat.Id,
|
||||
text: "GM-Relay Bot ready. Use /help for commands.",
|
||||
cancellationToken: ct);
|
||||
await SendStartMessageAsync(message, ct);
|
||||
break;
|
||||
|
||||
case "/newsession":
|
||||
@@ -236,4 +235,24 @@ public sealed class UpdateRouter(
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendStartMessageAsync(Message message, CancellationToken ct)
|
||||
{
|
||||
var miniAppUrl = configuration["Telegram:MiniAppUrl"];
|
||||
if (string.IsNullOrWhiteSpace(miniAppUrl))
|
||||
{
|
||||
await bot.SendMessage(
|
||||
chatId: message.Chat.Id,
|
||||
text: "GM-Relay Bot ready. Use /help for commands.",
|
||||
cancellationToken: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await bot.SendMessage(
|
||||
chatId: message.Chat.Id,
|
||||
text: "GM-Relay Bot ready. Откройте dashboard внутри Telegram или используйте /help для команд.",
|
||||
replyMarkup: new InlineKeyboardMarkup(
|
||||
InlineKeyboardButton.WithWebApp("Открыть dashboard", new WebAppInfo(miniAppUrl))),
|
||||
cancellationToken: ct);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ builder.Services.AddSingleton<HandleRescheduleVoteHandler>();
|
||||
// ── Telegram infrastructure ──────────────────────────────────────────
|
||||
builder.Services.AddSingleton<UpdateRouter>();
|
||||
builder.Services.AddSingleton<ITelegramUpdateHandler>(sp => sp.GetRequiredService<UpdateRouter>());
|
||||
builder.Services.AddHostedService<TelegramMiniAppMenuButtonService>();
|
||||
builder.Services.AddHostedService<TelegramBotService>();
|
||||
|
||||
// ── Session scheduler ────────────────────────────────────────────────
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
}
|
||||
},
|
||||
"Telegram": {
|
||||
"BotToken": ""
|
||||
"BotToken": "",
|
||||
"MiniAppUrl": ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user