Skip to content

Commit

Permalink
move dry logic to higher level
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Dec 6, 2023
1 parent ff277c5 commit 1166ae1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
12 changes: 5 additions & 7 deletions app/bot/spam.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,13 @@ func (s *SpamFilter) OnMessage(msg Message) (response Response) {

if similaritySpam || isEmojiSpam || stopWordsSpam || s.isCasSpam(msg.From.ID) || classifiedSpam {
log.Printf("[INFO] user %s detected as spammer, msg: %q", displayUsername, msg.Text)
msgPrefix := s.SpamMsg
if s.Dry {
return Response{
Text: s.SpamDryMsg + fmt.Sprintf(": %q (%d)", displayUsername, msg.From.ID),
Send: true, ReplyTo: msg.ID,
}
msgPrefix = s.SpamDryMsg
}
return Response{Text: s.SpamMsg + fmt.Sprintf(": %q (%d)", displayUsername, msg.From.ID),
Send: true, ReplyTo: msg.ID, BanInterval: permanentBanDuration, DeleteReplyTo: true,
User: User{Username: msg.From.Username, ID: msg.From.ID, DisplayName: msg.From.DisplayName},
spamRespMsg := fmt.Sprintf("%s: %q (%d)", msgPrefix, displayUsername, msg.From.ID)
return Response{Text: spamRespMsg, Send: true, ReplyTo: msg.ID, BanInterval: permanentBanDuration,
DeleteReplyTo: true, User: User{Username: msg.From.Username, ID: msg.From.ID, DisplayName: msg.From.DisplayName},
}
}

Expand Down
9 changes: 8 additions & 1 deletion app/events/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type TelegramListener struct {
SuperUsers SuperUser
StartupMsg string
NoSpamReply bool
Dry bool

AdminURL string
AdminSecret string
Expand Down Expand Up @@ -171,7 +172,7 @@ func (l *TelegramListener) procEvents(update tbapi.Update) error {

errs := new(multierror.Error)
isBanInvoked := resp.Send && resp.BanInterval > 0
// some bots may request direct ban for given duration
// some bots may request a direct ban for given duration
if isBanInvoked {
log.Printf("[DEBUG] ban initiated for %+v", resp)
l.SpamLogger.Save(msg, &resp)
Expand Down Expand Up @@ -292,6 +293,12 @@ func (l *TelegramListener) banUserOrChannel(duration time.Duration, chatID, user
// you do not want to accidentally get into this 30-second window of a lifetime ban.
// In practice BanDuration is equal to ten minutes,
// so this `if` statement is unlikely to be evaluated to true.

if l.Dry {
log.Printf("[INFO] dry run: ban %d for %v", userID, duration)
return nil
}

if duration < 30*time.Second {
duration = 1 * time.Minute
}
Expand Down
5 changes: 5 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func main() {
}

func execute(ctx context.Context) error {
if opts.Dry {
log.Print("[WARN] dry mode, no actual bans")
}

tbAPI, err := tbapi.NewBotAPI(opts.Telegram.Token)
if err != nil {
return fmt.Errorf("can't make telegram bot, %w", err)
Expand Down Expand Up @@ -142,6 +146,7 @@ func execute(ctx context.Context) error {
AdminGroup: opts.Admin.Group,
AdminURL: opts.Admin.URL,
AdminListenAddr: opts.Admin.Address,
Dry: opts.Dry,
}

if err := tgListener.Do(ctx); err != nil {
Expand Down

0 comments on commit 1166ae1

Please sign in to comment.