Add check for extended_latin and links

This commit is contained in:
Astra 2026-02-26 21:36:59 +00:00
parent af46dbe003
commit 4f6f70afad

162
bot.go
View file

@ -313,95 +313,101 @@ func handleNewMessage(ctx context.Context, api *tg.Client, alertPeer *tg.InputPe
return nil return nil
} }
log.Printf("Matched message with score %.2f", result.score) if result.score == 1.0 ||
for key, values := range result.matchedKeywords { (result.score == 1.0 &&
for _, value := range values { result.matchedKeywords["extended_latin"] != nil &&
log.Printf(" %s: %s", key, value) len(result.matchedKeywords["links"]) > 0) {
log.Printf("Matched message with score %.2f", result.score)
for key, values := range result.matchedKeywords {
for _, value := range values {
log.Printf(" %s: %s", key, value)
}
} }
}
// Delete the message from supergroup // Delete the message from supergroup
channel, ok := entities.Channels[chatID] channel, ok := entities.Channels[chatID]
if !ok { if !ok {
return fmt.Errorf("channel %d not found in entities", chatID) 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{ Channel: &tg.InputChannel{
ChannelID: chatID, ChannelID: chatID,
AccessHash: channel.AccessHash, AccessHash: channel.AccessHash,
}, },
ID: []int{msg.ID}, ID: []int{msg.ID},
}) })
if err != nil { if err != nil {
log.Printf("Failed to delete message: %v", err) log.Printf("Failed to delete message: %v", err)
} }
// Get sender info // Get sender info
var senderID int64 var senderID int64
if fromID, ok := msg.FromID.(*tg.PeerUser); ok { if fromID, ok := msg.FromID.(*tg.PeerUser); ok {
senderID = int64(fromID.UserID) senderID = int64(fromID.UserID)
} else { } else {
return fmt.Errorf("could not determine sender") return fmt.Errorf("could not determine sender")
} }
user, ok := entities.Users[senderID] user, ok := entities.Users[senderID]
if !ok { if !ok {
return fmt.Errorf("user %d not found in entities", senderID) return fmt.Errorf("user %d not found in entities", senderID)
} }
displayName := user.FirstName displayName := user.FirstName
if user.LastName != "" { if user.LastName != "" {
displayName += " " + user.LastName displayName += " " + user.LastName
} }
displayName = strings.TrimSpace(displayName) displayName = strings.TrimSpace(displayName)
username := "no username" username := "no username"
if user.Username != "" { if user.Username != "" {
username = "@" + user.Username username = "@" + user.Username
} }
// Restrict sender in supergroup // Restrict sender in supergroup
_, err = api.ChannelsEditBanned(ctx, &tg.ChannelsEditBannedRequest{ _, err = api.ChannelsEditBanned(ctx, &tg.ChannelsEditBannedRequest{
Channel: &tg.InputChannel{ Channel: &tg.InputChannel{
ChannelID: chatID, ChannelID: chatID,
AccessHash: channel.AccessHash, AccessHash: channel.AccessHash,
}, },
Participant: &tg.InputPeerUser{ Participant: &tg.InputPeerUser{
UserID: senderID, UserID: senderID,
AccessHash: user.AccessHash, AccessHash: user.AccessHash,
}, },
BannedRights: tg.ChatBannedRights{ BannedRights: tg.ChatBannedRights{
SendMessages: true, SendMessages: true,
SendMedia: true, SendMedia: true,
SendStickers: true, SendStickers: true,
SendGifs: true, SendGifs: true,
UntilDate: 0, UntilDate: 0,
}, },
}) })
if err != nil { if err != nil {
log.Printf("Failed to restrict user %d: %v", senderID, err) log.Printf("Failed to restrict user %d: %v", senderID, err)
} }
// Get supergroup name // Get supergroup name
chatName := channel.Title chatName := channel.Title
// Build and send alert message // Build and send alert message
matchMessage := fmt.Sprintf("🚨 Matched\n**Score**: %.2f\n**Chat**: %s (ID: %d)\n**User**: %s (%s) (ID: %d)\n", matchMessage := fmt.Sprintf("🚨 Matched\n**Score**: %.2f\n**Chat**: %s (ID: %d)\n**User**: %s (%s) (ID: %d)\n",
result.score, escapeMarkdown(chatName), chatID, escapeMarkdown(displayName+" ("+username+")"), username, senderID) result.score, escapeMarkdown(chatName), chatID, escapeMarkdown(displayName+" ("+username+")"), username, senderID)
// Send ntfy notification if config set // Send ntfy notification if config set
if cfg.NtfyToken != "" || cfg.NtfyTopic != "" { if cfg.NtfyToken != "" || cfg.NtfyTopic != "" {
notify(matchMessage, cfg.NtfyTopic, fmt.Sprintf("Scam Alert: %s", chatName), 5, cfg.NtfyToken) notify(matchMessage, cfg.NtfyTopic, fmt.Sprintf("Scam Alert: %s", chatName), 5, cfg.NtfyToken)
} }
// Send alert message to alert chat // Send alert message to alert chat
_, err = api.MessagesSendMessage(ctx, &tg.MessagesSendMessageRequest{ _, err = api.MessagesSendMessage(ctx, &tg.MessagesSendMessageRequest{
Peer: alertPeer, Peer: alertPeer,
Message: matchMessage, Message: matchMessage,
RandomID: rand.Int63(), RandomID: rand.Int63(),
}) })
if err != nil { if err != nil {
log.Printf("Failed to send alert message: %v", err) log.Printf("Failed to send alert message: %v", err)
}
} }
return nil return nil