diff --git a/notifier/notifier.go b/notifier/notifier.go index 46b81a825..6daa4b2e2 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -193,7 +193,7 @@ func (notifier *StandardNotifier) runSender(sender moira.Sender, ch chan Notific } } else { switch e := err.(type) { - case *moira.SenderBrokenContactError: + case moira.SenderBrokenContactError: log.Errorf("Cannot send to broken contact: %s", e.Error()) default: diff --git a/senders/telegram/send.go b/senders/telegram/send.go index a1507525b..af2c3bb12 100644 --- a/senders/telegram/send.go +++ b/senders/telegram/send.go @@ -37,9 +37,15 @@ func (sender *Sender) SendEvents(events moira.NotificationEvents, contact moira. sender.logger.Debugf("Calling telegram api with chat_id %s and message body %s", contact.Value, message) chat, err := sender.getChat(contact.Value) if err != nil { + if ok, errorBrokenContact := checkBrokenContactError(sender.logger, err); ok { + return errorBrokenContact + } return err } if err := sender.talk(chat, message, plots, msgType); err != nil { + if ok, errorBrokenContact := checkBrokenContactError(sender.logger, err); ok { + return errorBrokenContact + } return fmt.Errorf("failed to send message to telegram contact %s: %s. ", contact.Value, err) } return nil @@ -116,24 +122,37 @@ func (sender *Sender) getChat(username string) (*telebot.Chat, error) { // talk processes one talk func (sender *Sender) talk(chat *telebot.Chat, message string, plots [][]byte, messageType messageType) error { if messageType == Album { + sender.logger.Debug("talk as album") return sender.sendAsAlbum(chat, plots, message) } + sender.logger.Debug("talk as send message") return sender.sendAsMessage(chat, message) } func (sender *Sender) sendAsMessage(chat *telebot.Chat, message string) error { _, err := sender.bot.Send(chat, message) if err != nil { - if e, ok := err.(*telebot.APIError); ok { - if e.Code == 401 || e.Code == 400 && (e.Message == telebot.ErrBlockedByUser.Message || - e.Message == telebot.ErrEmptyChatID.Message || - e.Message == telebot.ErrChatNotFound.Message) { - return moira.NewSenderBrokenContactError(err) - } + sender.logger.Debugf("can't send event message [%s] to %v: %s", message, chat.ID, err.Error()) + } + return err +} + +func checkBrokenContactError(logger moira.Logger, err error) (bool, error) { + logger.Info("Check broken contact") + if err == nil { + return false, nil + } + if e, ok := err.(*telebot.APIError); ok { + logger.Debug("It's telebot.APIError from talk(): code = %d, msg = %s, desc = %s", e.Code, e.Message, e.Description) + if e.Code == telebot.ErrUnauthorized.Code { // all forbid errors + return true, moira.NewSenderBrokenContactError(err) } - return fmt.Errorf("can't send event message [%s] to %v: %s", message, chat.ID, err.Error()) } - return nil + if strings.HasPrefix(err.Error(), "failed to get username uuid") { + logger.Debug("It's error from getChat(): ", err) + return true, moira.NewSenderBrokenContactError(err) + } + return false, nil } func prepareAlbum(plots [][]byte, caption string) telebot.Album { @@ -151,9 +170,9 @@ func (sender *Sender) sendAsAlbum(chat *telebot.Chat, plots [][]byte, caption st _, err := sender.bot.SendAlbum(chat, album) if err != nil { - return fmt.Errorf("can't send event plots to %v: %s", chat.ID, err.Error()) + sender.logger.Debugf("can't send event plots to %v: %s", chat.ID, err.Error()) } - return nil + return err } func getMessageType(plots [][]byte) messageType {