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:
1 parent
bed4b1687e
commit
4f847bd33d
2 files changed
+47
-8
No files matched your search
@@ -1332,11 +1332,17 @@ public class DatabaseService
|
||||
{
|
||||
using var conn = CreateConnection();
|
||||
var chatIdStr = chatId.ToString();
|
||||
// Erst exakte Chat-ID-Verknüpfung suchen
|
||||
var session = await conn.QuerySingleOrDefaultAsync<SessionRecord>(
|
||||
return await conn.QuerySingleOrDefaultAsync<SessionRecord>(
|
||||
"SELECT * FROM sessions WHERE telegram_bot_id = @botId AND telegram_chat_id = @chatIdStr AND is_deleted = 0",
|
||||
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)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
@@ -54,10 +55,23 @@ public class TelegramService
|
||||
|
||||
if (session == null)
|
||||
{
|
||||
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;
|
||||
// Gibt es genau 1 unverknüpfte Session für diesen Bot? → Auto-Link
|
||||
var unlinked = (await _db.FindUnlinkedSessionsForBot(bot.BotId)).ToList();
|
||||
if (unlinked.Count == 1)
|
||||
{
|
||||
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("/"))
|
||||
@@ -85,7 +99,26 @@ public class TelegramService
|
||||
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,
|
||||
$"📋 Deine Chat-ID: `{chatId}`\n\n" +
|
||||
"Bitte weise diese Chat-ID im Web-UI einer Session zu, " +
|
||||
|
||||
Reference in new issue
Block a user