feat(cache): 🎉 add message caching functionality
Some checks failed
CI / Test (1.23) (push) Failing after -27m1s
CI / Lint (push) Successful in -26m31s
CI / Build (push) Successful in -27m3s
CI / Test (1.22) (push) Failing after -24m58s

* Implement MessageCache to store events when no webhooks are available.
* Add configuration options for enabling cache, setting data path, max age, and max events.
* Create API endpoints for managing cached events, including listing, replaying, and deleting.
* Integrate caching into the hooks manager to store events when no active webhooks are found.
* Enhance logging for better traceability of cached events and operations.
This commit is contained in:
Hein
2026-01-30 16:00:34 +02:00
parent 3901bbb668
commit c4d974d6ce
9 changed files with 1535 additions and 30 deletions

View File

@@ -10,16 +10,21 @@ import (
// BusinessAPIWebhook handles both verification (GET) and webhook events (POST)
func (h *Handlers) BusinessAPIWebhook(w http.ResponseWriter, r *http.Request) {
accountID := extractAccountIDFromPath(r.URL.Path)
if r.Method == http.MethodGet {
logging.Info("WhatsApp webhook verification request", "account_id", accountID, "method", "GET")
h.businessAPIWebhookVerify(w, r)
return
}
if r.Method == http.MethodPost {
logging.Info("WhatsApp webhook event received", "account_id", accountID, "method", "POST")
h.businessAPIWebhookEvent(w, r)
return
}
logging.Warn("WhatsApp webhook invalid method", "account_id", accountID, "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
@@ -29,6 +34,7 @@ func (h *Handlers) businessAPIWebhookVerify(w http.ResponseWriter, r *http.Reque
// Extract account ID from URL path
accountID := extractAccountIDFromPath(r.URL.Path)
if accountID == "" {
logging.Warn("WhatsApp webhook verification missing account ID")
http.Error(w, "Account ID required in path", http.StatusBadRequest)
return
}
@@ -94,10 +100,13 @@ func (h *Handlers) businessAPIWebhookEvent(w http.ResponseWriter, r *http.Reques
// Extract account ID from URL path
accountID := extractAccountIDFromPath(r.URL.Path)
if accountID == "" {
logging.Warn("WhatsApp webhook event missing account ID")
http.Error(w, "Account ID required in path", http.StatusBadRequest)
return
}
logging.Info("WhatsApp webhook processing started", "account_id", accountID)
// Get the client from the manager
client, exists := h.whatsappMgr.GetClient(accountID)
if !exists {
@@ -128,6 +137,8 @@ func (h *Handlers) businessAPIWebhookEvent(w http.ResponseWriter, r *http.Reques
return
}
logging.Info("WhatsApp webhook processed successfully", "account_id", accountID)
// Return 200 OK to acknowledge receipt
w.WriteHeader(http.StatusOK)
writeBytes(w, []byte("OK"))