feat(cache): 🎉 add message caching functionality
* 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:
@@ -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"))
|
||||
|
||||
Reference in New Issue
Block a user