Добавлена проверка на наличие информации о чате в обработчиках сообщений Telegram-бота. Улучшена фильтрация пустых токенов в сообщениях и добавлено логирование предупреждений для случаев, когда сообщения приходят без чата. Эти изменения повышают устойчивость и информативность бота при обработке обновлений.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -210,6 +210,12 @@ func (b *Bot) handleUpdate(update tgbotapi.Update) {
|
|||||||
return
|
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
|
// Update chatID for completion notifications
|
||||||
b.chatMu.Lock()
|
b.chatMu.Lock()
|
||||||
if b.chatID != update.Message.Chat.ID {
|
if b.chatID != update.Message.Chat.ID {
|
||||||
@@ -228,6 +234,20 @@ func (b *Bot) handleUpdate(update tgbotapi.Update) {
|
|||||||
// Tokenize the update
|
// Tokenize the update
|
||||||
tokens := strings.Split(update.Message.Text, " ")
|
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
|
// Preprocess message based on URL schema
|
||||||
if len(tokens) > 0 && (strings.HasPrefix(tokens[0], "magnet") || strings.HasPrefix(tokens[0], "http")) {
|
if len(tokens) > 0 && (strings.HasPrefix(tokens[0], "magnet") || strings.HasPrefix(tokens[0], "http")) {
|
||||||
tokens = append([]string{"add"}, tokens...)
|
tokens = append([]string{"add"}, tokens...)
|
||||||
|
|||||||
@@ -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
|
// 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) {
|
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()
|
torrents, err := b.client.GetTorrents()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.handleClientError(update.Message.Chat.ID, errorPrefix, err)
|
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) {
|
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 {
|
if len(tokens) == 0 {
|
||||||
b.SendMessage(update.Message.Chat.ID, "*add:* needs at least one URL", false)
|
b.SendMessage(update.Message.Chat.ID, "*add:* needs at least one URL", false)
|
||||||
return
|
return
|
||||||
@@ -856,7 +864,11 @@ func (b *Bot) handleVersion(update tgbotapi.Update) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) handleReceiveTorrent(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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user