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 }