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