Skip to content

Commit

Permalink
fix telegram sender detecting broken contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Kolkov committed Mar 3, 2021
1 parent 3e33653 commit 9c75d2e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
39 changes: 29 additions & 10 deletions senders/telegram/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 9c75d2e

Please sign in to comment.