Telegram: Auto-Link bei /start und normalen Nachrichten

Wenn eine Telegram-Nachricht von einem nicht verknüpften Chat eingeht:
- Sucht nach Sessions mit diesem Bot, die noch keine Chat-ID haben
- Genau 1 unverknüpfte Session → automatisch zuweisen + Bestätigung
- 0 oder mehrere → Chat-ID anzeigen, manuell im Web-UI zuweisen

Neue DB-Methode: FindUnlinkedSessionsForBot(botId)
This commit is contained in:
Meleki committed 2026-03-08 23:00:13 +01:00
1 parent bed4b1687e
commit 4f847bd33d
2 files changed
+47 -8

No files matched your search

+9 -3
View File
@@ -1332,11 +1332,17 @@ public class DatabaseService
{ {
using var conn = CreateConnection(); using var conn = CreateConnection();
var chatIdStr = chatId.ToString(); var chatIdStr = chatId.ToString();
// Erst exakte Chat-ID-Verknüpfung suchen return await conn.QuerySingleOrDefaultAsync<SessionRecord>(
var session = await conn.QuerySingleOrDefaultAsync<SessionRecord>(
"SELECT * FROM sessions WHERE telegram_bot_id = @botId AND telegram_chat_id = @chatIdStr AND is_deleted = 0", "SELECT * FROM sessions WHERE telegram_bot_id = @botId AND telegram_chat_id = @chatIdStr AND is_deleted = 0",
new { botId, chatIdStr }); new { botId, chatIdStr });
return session; }
public async Task<IEnumerable<SessionRecord>> FindUnlinkedSessionsForBot(string botId)
{
using var conn = CreateConnection();
return await conn.QueryAsync<SessionRecord>(
"SELECT * FROM sessions WHERE telegram_bot_id = @botId AND (telegram_chat_id IS NULL OR telegram_chat_id = '') AND is_deleted = 0",
new { botId });
} }
public async Task<IEnumerable<SessionRecord>> GetUserSessions(string userId) public async Task<IEnumerable<SessionRecord>> GetUserSessions(string userId)
+38 -5
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
@@ -54,10 +55,23 @@ public class TelegramService
if (session == null) if (session == null)
{ {
await SendMessage(chatId, bot.BotToken, // Gibt es genau 1 unverknüpfte Session für diesen Bot? → Auto-Link
$"🔒 Keine Session mit diesem Chat verknüpft.\n\nDeine Chat-ID: `{chatId}`\nBitte weise diese Chat-ID im Web-UI einer Session zu.", var unlinked = (await _db.FindUnlinkedSessionsForBot(bot.BotId)).ToList();
parseMode: "Markdown"); if (unlinked.Count == 1)
return; {
session = unlinked[0];
await _db.UpdateSessionTelegramChat(session.SessionId, chatId.ToString());
var sessionName = session.Name ?? session.SessionId;
await SendMessage(chatId, bot.BotToken,
$"✅ Automatisch verbunden mit Session: {sessionName}");
}
else
{
await SendMessage(chatId, bot.BotToken,
$"🔒 Keine Session mit diesem Chat verknüpft.\n\nDeine Chat-ID: `{chatId}`\nBitte weise diese Chat-ID im Web-UI einer Session zu.",
parseMode: "Markdown");
return;
}
} }
if (text.StartsWith("/")) if (text.StartsWith("/"))
@@ -85,7 +99,26 @@ public class TelegramService
return; return;
} }
// Nicht verknüpft – Chat-ID anzeigen // Nicht verknüpft – gibt es Sessions mit diesem Bot ohne Chat-ID?
var unlinked = (await _db.FindUnlinkedSessionsForBot(bot.BotId)).ToList();
if (unlinked.Count == 1)
{
// Genau 1 Session ohne Chat-ID → automatisch zuweisen
var target = unlinked[0];
await _db.UpdateSessionTelegramChat(target.SessionId, chatId.ToString());
var sessionName = target.Name ?? target.SessionId;
await SendMessage(chatId, bot.BotToken,
$"✅ *Automatisch verbunden mit Session: {EscapeMd(sessionName)}*\n\n" +
"Du kannst jetzt einfach drauflos schreiben\\!\n\n" +
"Befehle:\n" +
"/clear – History archivieren\n" +
"/status – Aktuelle Session\\-Info",
parseMode: "MarkdownV2");
return;
}
// Keine oder mehrere unverknüpfte Sessions → Chat-ID anzeigen
await SendMessage(chatId, bot.BotToken, await SendMessage(chatId, bot.BotToken,
$"📋 Deine Chat-ID: `{chatId}`\n\n" + $"📋 Deine Chat-ID: `{chatId}`\n\n" +
"Bitte weise diese Chat-ID im Web-UI einer Session zu, " + "Bitte weise diese Chat-ID im Web-UI einer Session zu, " +