Skip to content

Send bot internal error message in case of errors.#545

Open
nickeskov wants to merge 1 commit intomainfrom
send-bot-internal-error-msg
Open

Send bot internal error message in case of errors.#545
nickeskov wants to merge 1 commit intomainfrom
send-bot-internal-error-msg

Conversation

@nickeskov
Copy link
Copy Markdown
Collaborator

No description provided.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves bot-side observability by emitting “internal error” notifications to the bot chat when message handling or alert rendering fails, so operators can see bot/messaging failures directly in Discord/Telegram.

Changes:

  • Fixes a typo in an alert deserialization error message.
  • On pubsub message handling failures, generates and sends an InternalErrorAlert to the bot.
  • Adds helper methods to send bot internal error messages to Discord/Telegram chats from SendAlertMessage error paths.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/messaging/alert.go Corrects typo in “insufficient length” error message.
cmd/bots/internal/bots/messaging/pubsub_client.go Sends an internal error alert to the bot when a pubsub message fails to parse/handle.
cmd/bots/internal/bots/environment.go Adds sendErrMsg helpers and uses them to notify chats on alert rendering/sending errors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +29 to +35
internalAlert := entities.NewInternalErrorAlert(time.Now().Unix(), hndlErr)
alertMsg, iErr := messaging.NewAlertMessageFromAlert(internalAlert)
if iErr != nil { // must never happen
logger.Error("Failed to create alert message for internal alert", attrs.Error(iErr))
return
}
bot.SendAlertMessage(alertMsg) // send alert about something not working in the messaging service
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the NATS subscription handler, any parse/handle error now triggers sending an InternalErrorAlert to the bot chat. If the pubsub server publishes malformed/unexpected messages (or an attacker can publish to the subject), this can easily turn into an alert storm and drown out real alerts. Consider adding a simple rate-limit/deduplication (e.g., per error type within a time window) or restricting internal alerts to specific error classes that indicate a real service failure (vs. a single bad message).

Copilot uses AI. Check for mistakes.
}

func (tgEnv *TelegramBotEnvironment) sendErrMsg(msg string, err error) {
errMsg := fmt.Sprintf("Bot Error - %s: %v", msg, err)
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TelegramBotEnvironment.sendErrMsg builds a raw string and routes it through SendMessage(), which always sends with ParseMode HTML. Since error strings can contain characters like '<', '&', etc., Telegram may reject the message with an entity parsing error, defeating the purpose of notifying on failures. Consider escaping the error text for HTML (or sending error messages with ParseMode disabled / as preformatted text).

Suggested change
errMsg := fmt.Sprintf("Bot Error - %s: %v", msg, err)
escapedMsg := htmltmpl.HTMLEscapeString(msg)
escapedErr := htmltmpl.HTMLEscapeString(fmt.Sprint(err))
errMsg := fmt.Sprintf("Bot Error - %s: %s", escapedMsg, escapedErr)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants