diff --git a/cmd/bots/internal/bots/environment.go b/cmd/bots/internal/bots/environment.go index 4ebd434d..9fc97b77 100644 --- a/cmd/bots/internal/bots/environment.go +++ b/cmd/bots/internal/bots/environment.go @@ -139,6 +139,11 @@ func (dscBot *DiscordBotEnvironment) SendMessage(msg string) { } } +func (dscBot *DiscordBotEnvironment) sendErrMsg(msg string, err error) { + errMsg := fmt.Sprintf("Bot Error - %s: %v", msg, err) + dscBot.SendMessage(errMsg) +} + func (dscBot *DiscordBotEnvironment) SendAlertMessage(msg generalMessaging.AlertMessage) { alertType := msg.AlertType() if !alertType.Exist() { @@ -146,23 +151,21 @@ func (dscBot *DiscordBotEnvironment) SendAlertMessage(msg generalMessaging.Alert slog.Uint64("alertType", uint64(alertType)), attrs.Error(errUnknownAlertType), ) - _, err := dscBot.Bot.ChannelMessageSend(dscBot.ChatID, errUnknownAlertType.Error()) - - if err != nil { - dscBot.logger.Error("Failed to send a message to discord", attrs.Error(err)) - } + dscBot.sendErrMsg("Failed to construct message, unknown alert type", errUnknownAlertType) return } nodes, err := messaging.RequestAllNodes(dscBot.requestType, dscBot.responsePairType) if err != nil { dscBot.logger.Error("Failed to get nodes list", attrs.Error(err)) + dscBot.sendErrMsg("Failed to get nodes list", err) } alertJSON := msg.Data() messageToBot, err := constructMessage(alertType, alertJSON, dscBot.TemplatesExtension(), nodes) if err != nil { dscBot.logger.Error("Failed to construct message", attrs.Error(err)) + dscBot.sendErrMsg("Failed to construct message", err) return } alertID := msg.ReferenceID() @@ -173,6 +176,10 @@ func (dscBot *DiscordBotEnvironment) SendAlertMessage(msg generalMessaging.Alert dscBot.logger.Error("Failed to get message ID by the given alertID: alertID hasn't been found", attrs.Stringer("alertID", alertID), ) + dscBot.sendErrMsg( + "Failed to get message ID by the given alertID", + errors.Errorf("alertID %s hasn't been found", alertID), + ) return } msgRef := &discordgo.MessageReference{MessageID: strconv.Itoa(messageID)} @@ -297,6 +304,11 @@ func NewTelegramBotEnvironment( } } +func (tgEnv *TelegramBotEnvironment) sendErrMsg(msg string, err error) { + errMsg := fmt.Sprintf("Bot Error - %s: %v", msg, err) + tgEnv.SendMessage(errMsg) +} + func (tgEnv *TelegramBotEnvironment) TemplatesExtension() ExpectedExtension { return HTML } func (tgEnv *TelegramBotEnvironment) Start(ctx context.Context) error { @@ -323,14 +335,7 @@ func (tgEnv *TelegramBotEnvironment) SendAlertMessage(msg generalMessaging.Alert tgEnv.logger.Error("Failed to construct message, unknown alert type", slog.Uint64("alertType", uint64(alertType)), attrs.Error(errUnknownAlertType), ) - _, err := tgEnv.Bot.Send( - chat, - errUnknownAlertType.Error(), - &telebot.SendOptions{ParseMode: telebot.ModeHTML}, - ) - if err != nil { - tgEnv.logger.Error("Failed to send a message to telegram", attrs.Error(err)) - } + tgEnv.sendErrMsg("Failed to construct message, unknown alert type", errUnknownAlertType) return } @@ -343,6 +348,7 @@ func (tgEnv *TelegramBotEnvironment) SendAlertMessage(msg generalMessaging.Alert messageToBot, err := constructMessage(alertType, alertJSON, tgEnv.TemplatesExtension(), nodes) if err != nil { tgEnv.logger.Error("Failed to construct message", attrs.Error(err)) + tgEnv.sendErrMsg("Failed to construct message", err) return } alertID := msg.ReferenceID() @@ -353,6 +359,10 @@ func (tgEnv *TelegramBotEnvironment) SendAlertMessage(msg generalMessaging.Alert tgEnv.logger.Error("Failed to get message ID by the given alertID: alertID hasn't been found", attrs.Stringer("alertID", alertID), ) + tgEnv.sendErrMsg( + "Failed to get message ID by the given alertID", + errors.Errorf("alertID %s hasn't been found", alertID), + ) return } opts := &telebot.SendOptions{ReplyTo: &telebot.Message{ID: messageID}, ParseMode: telebot.ModeHTML} diff --git a/cmd/bots/internal/bots/messaging/pubsub_client.go b/cmd/bots/internal/bots/messaging/pubsub_client.go index 64121bf2..4f5af2f3 100644 --- a/cmd/bots/internal/bots/messaging/pubsub_client.go +++ b/cmd/bots/internal/bots/messaging/pubsub_client.go @@ -3,10 +3,12 @@ package messaging import ( "context" "log/slog" + "time" "github.com/nats-io/nats.go" "github.com/pkg/errors" + "nodemon/pkg/entities" "nodemon/pkg/messaging" "nodemon/pkg/tools/logging/attrs" ) @@ -24,6 +26,13 @@ func StartSubMessagingClient(ctx context.Context, natsServerURL string, bot Bot, hndlErr := handleReceivedMessage(msg.Data, bot) if hndlErr != nil { logger.Error("Failed to handle received message from pubsub server", attrs.Error(hndlErr)) + 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 } } bot.SetAlertHandlerFunc(alertHandlerFunc) diff --git a/pkg/messaging/alert.go b/pkg/messaging/alert.go index 608caabb..ad0e396e 100644 --- a/pkg/messaging/alert.go +++ b/pkg/messaging/alert.go @@ -37,7 +37,7 @@ func NewAlertMessageFromAlert(alert entities.Alert) (AlertMessage, error) { func NewAlertMessageFromBytes(msgData []byte) (AlertMessage, error) { const minMsgSize = 1 + crypto.DigestSize if l := len(msgData); l < minMsgSize { - return AlertMessage{}, errors.Errorf("message has inssufficient length: want at least %d, got %d", minMsgSize, l) + return AlertMessage{}, errors.Errorf("message has insufficient length: want at least %d, got %d", minMsgSize, l) } referenceID, err := crypto.NewDigestFromBytes(msgData[1 : 1+crypto.DigestSize]) if err != nil {