Skip to content

Commit

Permalink
Handle all 403s
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Mar 21, 2022
1 parent 6cccb27 commit 74c1100
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions handler/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func (h Handler) check() error {
func (h Handler) sendText(subscriber int64, text string, sendOptions *telebot.SendOptions) error {
_, err := h.Bot.Send(telebot.ChatID(subscriber), text, sendOptions)

var telebotError *telebot.Error
var floodError *telebot.FloodError

if err != nil {
if errors.Is(err, telebot.ErrChatNotFound) {
log.Printf("Chat %d not found, will be deleted", subscriber)
Expand All @@ -143,11 +146,16 @@ func (h Handler) sendText(subscriber int64, text string, sendOptions *telebot.Se
h.DB.Subscribers.Delete(subscriber)
h.DB.Subscribers.Create(migratedTo)
return h.sendText(migratedTo, text, sendOptions)
} else if errors.As(err, &telebot.FloodError{}) {
retryAfter := err.(telebot.FloodError).RetryAfter
} else if errors.As(err, floodError) {
retryAfter := floodError.RetryAfter
log.Printf("%d: Flood error, retrying after: %d seconds", subscriber, retryAfter)
time.Sleep(time.Duration(retryAfter) * time.Second)
h.sendText(subscriber, text, sendOptions)
} else if errors.As(err, &telebotError) {
if telebotError.Code == 403 {
log.Printf("%d: %s, will be removed", subscriber, telebotError.Description)
h.DB.Subscribers.Delete(subscriber)
}
} else {
return err
}
Expand Down

0 comments on commit 74c1100

Please sign in to comment.