From 41c8340924514c787a8ab5c7ff4c44519cea125f Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Thu, 4 Dec 2025 21:48:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D0=BD=D0=B0=D0=BB=D0=B8=D1=87=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B8=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=BE=20=D1=87=D0=B0=D1=82=D0=B5=20=D0=B2=20=D0=BE=D0=B1=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA=D0=B0=D1=85=20=D1=81?= =?UTF-8?q?=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9=20Telegram-?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0.=20=D0=A3=D0=BB=D1=83=D1=87=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BF=D1=83=D1=81=D1=82=D1=8B=D1=85=20?= =?UTF-8?q?=D1=82=D0=BE=D0=BA=D0=B5=D0=BD=D0=BE=D0=B2=20=D0=B2=20=D1=81?= =?UTF-8?q?=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=D1=85=20=D0=B8?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D1=80=D0=B5=D0=B4=D1=83=D0=BF=D1=80=D0=B5=D0=B6?= =?UTF-8?q?=D0=B4=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B4=D0=BB=D1=8F=20=D1=81?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B0=D0=B5=D0=B2,=20=D0=BA=D0=BE=D0=B3?= =?UTF-8?q?=D0=B4=D0=B0=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BF=D1=80=D0=B8=D1=85=D0=BE=D0=B4=D1=8F=D1=82=20?= =?UTF-8?q?=D0=B1=D0=B5=D0=B7=20=D1=87=D0=B0=D1=82=D0=B0.=20=D0=AD=D1=82?= =?UTF-8?q?=D0=B8=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B2=D1=8B=D1=88=D0=B0=D1=8E=D1=82=20=D1=83?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B9=D1=87=D0=B8=D0=B2=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D0=B8=20=D0=B8=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D0=B2=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=20=D0=BF=D1=80=D0=B8=20=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=BA=D0=B5=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/bot/bot.go | 20 ++++++++++++++++++++ internal/bot/handlers.go | 14 +++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 4c5d6ae..db9802d 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -210,6 +210,12 @@ func (b *Bot) handleUpdate(update tgbotapi.Update) { return } + // Check if chat is available + if update.Message.Chat == nil { + b.logger.Printf("[WARN] Received message without chat information") + return + } + // Update chatID for completion notifications b.chatMu.Lock() if b.chatID != update.Message.Chat.ID { @@ -228,6 +234,20 @@ func (b *Bot) handleUpdate(update tgbotapi.Update) { // Tokenize the update tokens := strings.Split(update.Message.Text, " ") + // Filter out empty tokens + nonEmptyTokens := make([]string, 0, len(tokens)) + for _, token := range tokens { + if token != "" { + nonEmptyTokens = append(nonEmptyTokens, token) + } + } + tokens = nonEmptyTokens + + // If no tokens after filtering, return + if len(tokens) == 0 { + return + } + // Preprocess message based on URL schema if len(tokens) > 0 && (strings.HasPrefix(tokens[0], "magnet") || strings.HasPrefix(tokens[0], "http")) { tokens = append([]string{"add"}, tokens...) diff --git a/internal/bot/handlers.go b/internal/bot/handlers.go index 3360f69..6731f14 100644 --- a/internal/bot/handlers.go +++ b/internal/bot/handlers.go @@ -126,6 +126,10 @@ func (b *Bot) handleClientError(chatID int64, prefix string, err error) { // handleTorrentList is a generic handler for listing torrents with filtering and formatting func (b *Bot) handleTorrentList(update tgbotapi.Update, errorPrefix, emptyMessage string, filter func(*transmission.Torrent) bool, format func(*transmission.Torrent) string) { + if update.Message == nil || update.Message.Chat == nil { + b.logger.Printf("[WARN] handleTorrentList called without valid message or chat") + return + } torrents, err := b.client.GetTorrents() if err != nil { b.handleClientError(update.Message.Chat.ID, errorPrefix, err) @@ -452,6 +456,10 @@ func (b *Bot) handleDownloadDir(update tgbotapi.Update, tokens []string) { } func (b *Bot) handleAdd(update tgbotapi.Update, tokens []string) { + if update.Message == nil || update.Message.Chat == nil { + b.logger.Printf("[WARN] handleAdd called without valid message or chat") + return + } if len(tokens) == 0 { b.SendMessage(update.Message.Chat.ID, "*add:* needs at least one URL", false) return @@ -856,7 +864,11 @@ func (b *Bot) handleVersion(update tgbotapi.Update) { } func (b *Bot) handleReceiveTorrent(update tgbotapi.Update) { - if update.Message.Document == nil { + if update.Message == nil || update.Message.Document == nil { + return + } + if update.Message.Chat == nil { + b.logger.Printf("[WARN] Received document without chat information") return }