Add join date check, notify if old user triggers detection

This commit is contained in:
Astra 2026-04-08 10:33:12 +01:00
parent 064f0e35b1
commit 1e91b97de3

37
bot.go
View file

@ -522,6 +522,34 @@ func handleNewMessage(ctx context.Context, api *tg.Client, alertPeer *tg.InputPe
return nil
}
// Extract sender ID early for join date check
fromID, ok := msg.FromID.(*tg.PeerUser)
if !ok {
return fmt.Errorf("could not determine sender")
}
senderID := int64(fromID.UserID)
// Check user join date - if over 24h old, send notify but don't delete
joinDate, err := getUserJoinDate(ctx, api, state.cfg.MonitoredChat, state.cfg.MonitoredChatAccessHash, senderID)
if err == nil {
if time.Since(joinDate) > 24*time.Hour {
// User joined over 24 hours ago - notify but don't action
user, ok := entities.Users[senderID]
if ok {
displayName := fullName(user.FirstName, user.LastName)
userDisplay := displayName
if user.Username != "" {
userDisplay += " (@" + user.Username + ")"
}
chatName := entities.Channels[chatID].Title
notifyMsg := fmt.Sprintf("🚨 Alert (Old User)\nScore: %.2f\nChat: %s (ID: %d)\nUser: %s (ID: %d)\nJoined: %v ago\n\n%s",
result.score, chatName, chatID, userDisplay, senderID, time.Since(joinDate), msg.Message)
notify(notifyMsg, state.cfg, fmt.Sprintf("Scam Alert: %s", chatName))
}
return nil
}
}
log.Printf("Matched message with score %.2f", result.score)
for key, values := range result.matchedKeywords {
for _, value := range values {
@ -534,7 +562,7 @@ func handleNewMessage(ctx context.Context, api *tg.Client, alertPeer *tg.InputPe
if !ok {
return fmt.Errorf("channel %d not found in entities", chatID)
}
_, err := api.ChannelsDeleteMessages(ctx, &tg.ChannelsDeleteMessagesRequest{
_, err = api.ChannelsDeleteMessages(ctx, &tg.ChannelsDeleteMessagesRequest{
Channel: &tg.InputChannel{
ChannelID: chatID,
AccessHash: channel.AccessHash,
@ -545,13 +573,6 @@ func handleNewMessage(ctx context.Context, api *tg.Client, alertPeer *tg.InputPe
log.Printf("Failed to delete message: %v", err)
}
// Get sender info
fromID, ok := msg.FromID.(*tg.PeerUser)
if !ok {
return fmt.Errorf("could not determine sender")
}
senderID := int64(fromID.UserID)
user, ok := entities.Users[senderID]
if !ok {
return fmt.Errorf("user %d not found in entities", senderID)