Skip to content

Commit

Permalink
don't filter reply in admin chat
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Dec 18, 2023
1 parent 60165b2 commit 737656c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 85 deletions.
25 changes: 10 additions & 15 deletions app/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,34 +256,21 @@ func (l *TelegramListener) adminChatMsgHandler(update tbapi.Update) error {
}
return string([]rune(inp)[:max]) + "..."
}
log.Printf("[DEBUG] message from admin chat: %+v", update)
log.Printf("[DEBUG] message from admin chat: msg id: %d, update id: %d, from: %s, sender: %s",
update.Message.MessageID, update.UpdateID, update.Message.From.UserName, update.Message.ForwardSenderName)

if update.Message.ForwardSenderName == "" && update.FromChat() == nil {
// this is a regular message from admin chat, not the forwarded one, ignore it
log.Printf("[DEBUG] message from admin chat, but not forwarded, ignore it, %+v", update.Message)
return nil
}

if update.Message.ReplyToMessage != nil {
// this is a reply to a message, ignore it
log.Printf("[DEBUG] message from admin chat, but reply to a message, ignore it, %+v", update.Message)
return nil
}

// this is a forwarded message from super to admin chat, it is an example of missed spam
// we need to update spam filter with this message
msgTxt := strings.ReplaceAll(update.Message.Text, "\n", " ")
log.Printf("[DEBUG] forwarded message from superuser %q to admin chat %d: %q",
update.Message.From.UserName, l.adminChatID, msgTxt)

// update spam samples
if !l.Dry {
if err := l.Bot.UpdateSpam(msgTxt); err != nil {
return fmt.Errorf("failed to update spam for %q: %w", msgTxt, err)
}
log.Printf("[INFO] spam updated with %q", shrink(update.Message.Text, 50))
}

// it would be nice to ban this user right away, but we don't have forwarded user ID here due to tg privacy limitation.
// it is empty in update.Message. To ban this user, we need to get the match on the message from the locator and ban from there.
info, ok := l.Locator.Message(update.Message.Text)
Expand All @@ -296,6 +283,14 @@ func (l *TelegramListener) adminChatMsgHandler(update tbapi.Update) error {
// remove user from the approved list
l.Bot.RemoveApprovedUsers(info.userID)

// update spam samples
if !l.Dry {
if err := l.Bot.UpdateSpam(msgTxt); err != nil {
return fmt.Errorf("failed to update spam for %q: %w", msgTxt, err)
}
log.Printf("[INFO] spam updated with %q", shrink(update.Message.Text, 50))
}

if l.Dry || l.TrainingMode {
return nil
}
Expand Down
70 changes: 0 additions & 70 deletions app/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,76 +434,6 @@ func TestTelegramListener_DoWithForwarded(t *testing.T) {
assert.Equal(t, int64(88), b.RemoveApprovedUsersCalls()[0].ID)
}

func TestTelegramListener_DoWithForwarded_Reply(t *testing.T) {
mockLogger := &mocks.SpamLoggerMock{SaveFunc: func(msg *bot.Message, response *bot.Response) {}}
mockAPI := &mocks.TbAPIMock{
GetChatFunc: func(config tbapi.ChatInfoConfig) (tbapi.Chat, error) {
return tbapi.Chat{ID: 123}, nil
},
SendFunc: func(c tbapi.Chattable) (tbapi.Message, error) {
return tbapi.Message{Text: c.(tbapi.MessageConfig).Text, From: &tbapi.User{UserName: "user"}}, nil
},
RequestFunc: func(c tbapi.Chattable) (*tbapi.APIResponse, error) {
return &tbapi.APIResponse{Ok: true}, nil
},
GetChatAdministratorsFunc: func(config tbapi.ChatAdministratorsConfig) ([]tbapi.ChatMember, error) { return nil, nil },
}
b := &mocks.BotMock{
OnMessageFunc: func(msg bot.Message) bot.Response {
t.Logf("on-message: %+v", msg)
if msg.Text == "text 123" && msg.From.Username == "user" {
return bot.Response{Send: true, Text: "bot's answer"}
}
return bot.Response{}
},
UpdateSpamFunc: func(msg string) error {
t.Logf("update-spam: %s", msg)
return nil
},
}

l := TelegramListener{
SpamLogger: mockLogger,
TbAPI: mockAPI,
Bot: b,
Group: "gr",
AdminGroup: "123",
StartupMsg: "startup",
SuperUsers: SuperUser{"umputun"},
Locator: NewLocator(10*time.Minute, 0),
}

ctx, cancel := context.WithTimeout(context.Background(), 500*time.Minute)
defer cancel()

l.Locator.AddMessage("text 123", 123, 88, 999999) // add message to locator

updMsg := tbapi.Update{
Message: &tbapi.Message{
Chat: &tbapi.Chat{ID: 123},
Text: "text 123",
From: &tbapi.User{UserName: "umputun", ID: 77},
Date: int(time.Date(2020, 2, 11, 19, 35, 55, 9, time.UTC).Unix()),
ForwardSenderName: "forwarded_name",
MessageID: 999999,
ReplyToMessage: &tbapi.Message{}, // reply to message
},
}

updChan := make(chan tbapi.Update, 1)
updChan <- updMsg
close(updChan)
mockAPI.GetUpdatesChanFunc = func(config tbapi.UpdateConfig) tbapi.UpdatesChannel { return updChan }

err := l.Do(ctx)
assert.EqualError(t, err, "telegram update chan closed")
assert.Equal(t, 0, len(mockLogger.SaveCalls()))
require.Equal(t, 1, len(mockAPI.SendCalls()))
assert.Equal(t, "startup", mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text)
require.Equal(t, 0, len(b.UpdateSpamCalls()), "no spam should be updated")
assert.Equal(t, 0, len(mockAPI.RequestCalls()), "no requests should be made")
}

func TestTelegramListener_DoWithAdminUnBan(t *testing.T) {
mockLogger := &mocks.SpamLoggerMock{}
mockAPI := &mocks.TbAPIMock{
Expand Down

0 comments on commit 737656c

Please sign in to comment.