fix: create Telegram topics for template batches
PR Checks / test-and-build (pull_request) Successful in 12m56s
PR Checks / test-and-build (pull_request) Successful in 12m56s
Create a Telegram forum topic when Web creates a batch from a campaign template, persist thread ownership on the generated sessions, and send the batch schedule into that topic. Bump version -> 3.1.1
This commit is contained in:
@@ -73,7 +73,7 @@
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="nav-version">v3.1.0</div>
|
||||
<div class="nav-version">v3.1.1</div>
|
||||
</div>
|
||||
</Authorized>
|
||||
<NotAuthorized>
|
||||
|
||||
@@ -3,6 +3,7 @@ using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Exceptions;
|
||||
using GmRelay.Web.Services;
|
||||
|
||||
namespace GmRelay.Web.Services;
|
||||
@@ -95,6 +96,7 @@ internal sealed record WebBatchSessionRow(
|
||||
string NotificationMode,
|
||||
bool TopicCreatedByBot = false);
|
||||
internal sealed record WebTemplateGroupDto(long TelegramChatId);
|
||||
internal sealed record WebTemplateTopicDestination(int? MessageThreadId, bool TopicCreatedByBot);
|
||||
|
||||
public sealed class SessionService(
|
||||
NpgsqlDataSource dataSource,
|
||||
@@ -1186,6 +1188,10 @@ public sealed class SessionService(
|
||||
throw new SessionAccessDeniedException(groupId, "0");
|
||||
}
|
||||
|
||||
var topicDestination = await ResolveTemplateBatchTopicAsync(group.TelegramChatId, template.Title);
|
||||
var messageThreadId = topicDestination.MessageThreadId;
|
||||
var topicCreatedByBot = topicDestination.TopicCreatedByBot;
|
||||
|
||||
var schedule = BatchSchedulePlanner.BuildRecurringSchedule(
|
||||
firstScheduledAt,
|
||||
template.SessionCount,
|
||||
@@ -1197,8 +1203,8 @@ public sealed class SessionService(
|
||||
{
|
||||
var sessionId = await conn.ExecuteScalarAsync<Guid>(
|
||||
"""
|
||||
INSERT INTO sessions (batch_id, group_id, title, join_link, scheduled_at, status, max_players, notification_mode)
|
||||
VALUES (@BatchId, @GroupId, @Title, @JoinLink, @ScheduledAt, @Status, @MaxPlayers, @NotificationMode)
|
||||
INSERT INTO sessions (batch_id, group_id, title, join_link, scheduled_at, status, thread_id, topic_created_by_bot, max_players, notification_mode)
|
||||
VALUES (@BatchId, @GroupId, @Title, @JoinLink, @ScheduledAt, @Status, @ThreadId, @TopicCreatedByBot, @MaxPlayers, @NotificationMode)
|
||||
RETURNING id
|
||||
""",
|
||||
new
|
||||
@@ -1209,6 +1215,8 @@ public sealed class SessionService(
|
||||
template.JoinLink,
|
||||
ScheduledAt = scheduledAt,
|
||||
Status = SessionStatus.Planned,
|
||||
ThreadId = messageThreadId,
|
||||
TopicCreatedByBot = topicCreatedByBot,
|
||||
template.MaxPlayers,
|
||||
template.NotificationMode
|
||||
},
|
||||
@@ -1223,6 +1231,7 @@ public sealed class SessionService(
|
||||
var renderResult = TelegramSessionBatchRenderer.Render(view);
|
||||
var batchMessage = await bot.SendMessage(
|
||||
chatId: group.TelegramChatId,
|
||||
messageThreadId: messageThreadId,
|
||||
text: renderResult.Text,
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
|
||||
replyMarkup: renderResult.Markup);
|
||||
@@ -1242,6 +1251,34 @@ public sealed class SessionService(
|
||||
template.NotificationMode);
|
||||
}
|
||||
|
||||
private async Task<WebTemplateTopicDestination> ResolveTemplateBatchTopicAsync(long telegramChatId, string title)
|
||||
{
|
||||
var chat = await bot.GetChat(chatId: telegramChatId);
|
||||
if (!chat.IsForum)
|
||||
{
|
||||
return new WebTemplateTopicDestination(null, TopicCreatedByBot: false);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var topic = await bot.CreateForumTopic(
|
||||
chatId: telegramChatId,
|
||||
name: $"🎲 Игры: {title}");
|
||||
return new WebTemplateTopicDestination(topic.MessageThreadId, TopicCreatedByBot: true);
|
||||
}
|
||||
catch (ApiRequestException ex) when (IsMissingForumTopicRightsError(ex.Message))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Не удалось создать Telegram topic. Сделайте бота admin и включите право Manage Topics, затем повторите действие.",
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsMissingForumTopicRightsError(string apiError) =>
|
||||
apiError.Contains("not enough rights", StringComparison.OrdinalIgnoreCase) ||
|
||||
apiError.Contains("CHAT_ADMIN_REQUIRED", StringComparison.OrdinalIgnoreCase) ||
|
||||
apiError.Contains("not an administrator", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
private async Task<List<WebDirectNotificationRecipient>> LoadSessionDirectRecipientsAsync(
|
||||
Npgsql.NpgsqlConnection conn,
|
||||
Guid sessionId)
|
||||
|
||||
Reference in New Issue
Block a user