Skip to content

Commit a4a88ba

Browse files
author
escalopa
committed
fix: forbidden user error
1 parent 09e81a3 commit a4a88ba

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

serverless/reminder/internal/handler/handler.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"slices"
77
"strconv"
8+
"strings"
89
"sync"
910
"time"
1011

@@ -22,6 +23,7 @@ type (
2223
GetSubscribersByOffset(ctx context.Context, botID int64, offset int32) (chatIDs []int64, _ error)
2324
GetPrayerDay(ctx context.Context, botID int64, date time.Time) (prayerDay *domain.PrayerDay, _ error)
2425
SetReminderMessageID(ctx context.Context, botID int64, chatID int64, reminderMessageID int32) error
26+
DeleteChat(ctx context.Context, botID int64, chatID int64) error
2527
}
2628

2729
Handler struct {
@@ -215,6 +217,17 @@ func (h *Handler) remindUser(
215217
ParseMode: models.ParseModeMarkdown,
216218
})
217219
if err != nil {
220+
if strings.HasPrefix(err.Error(), bot.ErrorForbidden.Error()) {
221+
// bot was blocked or user is deactivated so delete chat
222+
err = h.db.DeleteChat(ctx, chat.BotID, chat.ChatID)
223+
if err != nil {
224+
log.Error("remindUser: delete chat", log.Err(err), log.BotID(chat.BotID), log.ChatID(chat.ChatID))
225+
return fmt.Errorf("remindUser: delete chat: %v", err)
226+
}
227+
log.Warn("remindUser: delete chat", log.BotID(chat.BotID), log.ChatID(chat.ChatID))
228+
return nil
229+
}
230+
218231
log.Error("remindUser: send message", log.Err(err), log.BotID(chat.BotID), log.ChatID(chat.ChatID))
219232
return fmt.Errorf("remindUser: send message: %v", err)
220233
}

serverless/reminder/internal/service/db.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,25 @@ func (db *DB) SetReminderMessageID(ctx context.Context, botID int64, chatID int6
253253

254254
return err
255255
}
256+
257+
func (db *DB) DeleteChat(ctx context.Context, botID int64, chatID int64) error {
258+
query := `
259+
DECLARE $bot_id AS Int64;
260+
DECLARE $chat_id AS Int64;
261+
262+
DELETE FROM chats
263+
WHERE bot_id = $bot_id AND chat_id = $chat_id;
264+
`
265+
266+
params := table.NewQueryParameters(
267+
table.ValueParam("$bot_id", types.Int64Value(botID)),
268+
table.ValueParam("$chat_id", types.Int64Value(chatID)),
269+
)
270+
271+
err := db.client.Do(ctx, func(ctx context.Context, s table.Session) error {
272+
_, _, err := s.Execute(ctx, writeTx, query, params)
273+
return err
274+
})
275+
276+
return err
277+
}

0 commit comments

Comments
 (0)