feat: режим отправки медиа через инлайн
This commit is contained in:
@@ -17,6 +17,9 @@ public sealed class BotOptions
|
||||
/// </summary>
|
||||
public string? ApiBaseUrl { get; set; }
|
||||
|
||||
/// <summary>Публичный URL Mini App — используется в кнопке inline-режима.</summary>
|
||||
public string MiniAppUrl { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Публичный URL хоста для регистрации webhook (только в режиме Webhook).</summary>
|
||||
public string WebhookHost { get; set; } = string.Empty;
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Queries.GetMediaItem;
|
||||
using MemeVault.Application.Queries.SearchMedia;
|
||||
using MemeVault.Bot.Options;
|
||||
using MemeVault.Domain.Enums;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.InlineQueryResults;
|
||||
@@ -8,37 +11,90 @@ using Telegram.Bot.Types.InlineQueryResults;
|
||||
namespace MemeVault.Bot.Telegram.InlineMode;
|
||||
|
||||
/// <summary>
|
||||
/// Обрабатывает inline-запросы: @bot #мем #кот -#толстый
|
||||
/// Обрабатывает inline-запросы двух видов:
|
||||
/// • обычный поиск — @bot #мем #кот -#толстый
|
||||
/// • точечная отправка — @bot send:<guid> (пользователь выбрал элемент в Mini App)
|
||||
/// </summary>
|
||||
public sealed class InlineQueryHandler
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ITelegramBotClient _bot;
|
||||
private readonly BotOptions _options;
|
||||
|
||||
public InlineQueryHandler(IMediator mediator, ITelegramBotClient bot)
|
||||
public InlineQueryHandler(IMediator mediator, ITelegramBotClient bot, IOptions<BotOptions> options)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_bot = bot;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
public async Task HandleAsync(InlineQuery inlineQuery, CancellationToken ct)
|
||||
{
|
||||
var userId = inlineQuery.From.Id;
|
||||
var rawQuery = inlineQuery.Query ?? string.Empty;
|
||||
|
||||
// Режим точечной отправки: Mini App выбрала конкретный элемент
|
||||
if (rawQuery.StartsWith("send:", StringComparison.OrdinalIgnoreCase)
|
||||
&& Guid.TryParse(rawQuery[5..], out var itemId))
|
||||
{
|
||||
await HandleSendAsync(inlineQuery.Id, userId, itemId, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Обычный поиск
|
||||
await HandleSearchAsync(inlineQuery.Id, userId, rawQuery, ct);
|
||||
}
|
||||
|
||||
// ── Точечная отправка ────────────────────────────────────────────────────
|
||||
|
||||
private async Task HandleSendAsync(string queryId, long userId, Guid itemId, CancellationToken ct)
|
||||
{
|
||||
var item = await _mediator.Send(new GetMediaItemQuery(RequesterTelegramId: userId, ItemId: itemId), ct);
|
||||
|
||||
InlineQueryResult[] answers = item is null
|
||||
? []
|
||||
: [ToInlineResult(item)!];
|
||||
|
||||
await _bot.AnswerInlineQuery(
|
||||
queryId,
|
||||
answers.OfType<InlineQueryResult>(),
|
||||
cacheTime: 0,
|
||||
isPersonal: true,
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
// ── Поиск ────────────────────────────────────────────────────────────────
|
||||
|
||||
private async Task HandleSearchAsync(string queryId, long userId, string rawQuery, CancellationToken ct)
|
||||
{
|
||||
var results = await _mediator.Send(
|
||||
new SearchMediaQuery(inlineQuery.From.Id, inlineQuery.Query, Limit: 10, IncludeNsfw: false), ct);
|
||||
new SearchMediaQuery(userId, rawQuery, Limit: 10, IncludeNsfw: false), ct);
|
||||
|
||||
var answers = results
|
||||
.Select(ToInlineResult)
|
||||
.OfType<InlineQueryResult>()
|
||||
.ToArray();
|
||||
|
||||
// Кнопка над результатами — открывает Mini App в inline-режиме
|
||||
InlineQueryResultsButton? button = string.IsNullOrEmpty(_options.MiniAppUrl)
|
||||
? null
|
||||
: new InlineQueryResultsButton
|
||||
{
|
||||
Text = "Открыть MemeVault",
|
||||
WebApp = new WebAppInfo { Url = _options.MiniAppUrl + "?mode=inline" }
|
||||
};
|
||||
|
||||
await _bot.AnswerInlineQuery(
|
||||
inlineQuery.Id,
|
||||
queryId,
|
||||
answers,
|
||||
button: button,
|
||||
cacheTime: 10,
|
||||
isPersonal: true,
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
// ── Конвертер ────────────────────────────────────────────────────────────
|
||||
|
||||
private static InlineQueryResult? ToInlineResult(MediaItemDto item)
|
||||
{
|
||||
var id = item.Id.ToString();
|
||||
@@ -71,6 +127,7 @@ public sealed class InlineQueryHandler
|
||||
{
|
||||
Caption = item.Caption,
|
||||
Title = item.InlineTitle,
|
||||
Description = item.InlineDescription ?? tagsLine,
|
||||
},
|
||||
|
||||
MediaType.Text => new InlineQueryResultArticle(
|
||||
|
||||
Reference in New Issue
Block a user