Добавлена проверка на наличие информации о чате в обработчиках сообщений Telegram-бота. Улучшена фильтрация пустых токенов в сообщениях и добавлено логирование предупреждений для случаев, когда сообщения приходят без чата. Эти изменения повышают устойчивость и информативность бота при обработке обновлений.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Struchkov Mark
2025-12-04 21:48:37 +03:00
parent caeb5f06bd
commit 41c8340924
2 changed files with 33 additions and 1 deletions

View File

@@ -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...)

View File

@@ -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
}