yao/agent/robot/events/integrations/telegram/dedup.go
Max ea9e070f29 Enhance robot integration with Telegram and improve event handling
- Add Telegram integration support by introducing a dispatcher for handling Telegram events and messages.
- Implement event notifications for robot configuration changes (creation, update, deletion) to facilitate integration with external services.
- Refactor the robot initialization process to load robots into cache and start the dispatcher, improving the overall system setup.
- Update the delivery event structure to include additional metadata for better context during message handling.
- Enhance logging capabilities for better observability during robot execution and event processing.
2026-03-01 22:03:25 +08:00

48 lines
1 KiB
Go

package telegram
import (
"sync"
"time"
)
const (
dedupTTL = 24 * time.Hour
dedupCleanInterval = time.Hour
)
// dedupStore is a lightweight in-memory deduplication store with TTL.
// Used for message-level dedup (same update_id won't be processed twice).
type dedupStore struct {
m sync.Map // key -> int64 (unix timestamp)
}
func newDedupStore() *dedupStore {
return &dedupStore{}
}
// markSeen returns true if this is the first time the key is seen.
func (d *dedupStore) markSeen(key string) bool {
now := time.Now().Unix()
_, loaded := d.m.LoadOrStore(key, now)
return !loaded
}
// cleaner periodically removes expired entries. Runs until stopCh is closed.
func (d *dedupStore) cleaner(stopCh <-chan struct{}) {
ticker := time.NewTicker(dedupCleanInterval)
defer ticker.Stop()
for {
select {
case <-stopCh:
return
case <-ticker.C:
cutoff := time.Now().Add(-dedupTTL).Unix()
d.m.Range(func(key, value any) bool {
if ts, ok := value.(int64); ok && ts < cutoff {
d.m.Delete(key)
}
return true
})
}
}
}