kjkkkk
This commit is contained in:
parent
a7073cac11
commit
0cbcc682e4
@ -315,33 +315,62 @@ func WebhookReceiveHandler(db *gorm.DB) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("📩 Webhook reçu :", body)
|
fmt.Println("\n📩 Webhook reçu :", body)
|
||||||
|
|
||||||
// Exemple de parsing
|
|
||||||
entry := body["entry"].([]interface{})[0].(map[string]interface{})
|
entry := body["entry"].([]interface{})[0].(map[string]interface{})
|
||||||
change := entry["changes"].([]interface{})[0].(map[string]interface{})
|
change := entry["changes"].([]interface{})[0].(map[string]interface{})
|
||||||
value := change["value"].(map[string]interface{})
|
value := change["value"].(map[string]interface{})
|
||||||
|
|
||||||
|
recipientID := value["metadata"].(map[string]interface{})["phone_number_id"].(string)
|
||||||
|
var user models.User
|
||||||
|
if err := db.Where("whatsapp_phone_number_id = ?", recipientID).First(&user).Error; err != nil {
|
||||||
|
log.Println("❌ Utilisateur introuvable pour phone_number_id:", recipientID)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
messages := value["messages"].([]interface{})
|
messages := value["messages"].([]interface{})
|
||||||
if len(messages) > 0 {
|
if len(messages) > 0 {
|
||||||
msg := messages[0].(map[string]interface{})
|
msg := messages[0].(map[string]interface{})
|
||||||
from := msg["from"].(string)
|
from := msg["from"].(string)
|
||||||
msgType := msg["type"].(string)
|
msgType := msg["type"].(string)
|
||||||
|
msgID := msg["id"].(string)
|
||||||
|
|
||||||
var content string
|
var content string
|
||||||
if msgType == "text" {
|
if msgType == "text" {
|
||||||
content = msg["text"].(map[string]interface{})["body"].(string)
|
content = msg["text"].(map[string]interface{})["body"].(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("💬 Message reçu de %s : %s\n", from, content)
|
contextID := ""
|
||||||
|
if ctx, ok := msg["context"].(map[string]interface{}); ok {
|
||||||
|
contextID = fmt.Sprintf("%v", ctx["id"])
|
||||||
|
}
|
||||||
|
|
||||||
// TODO : enregistrer en base
|
conv := models.Conversation{
|
||||||
|
UserID: user.ID,
|
||||||
|
From: from,
|
||||||
|
To: recipientID,
|
||||||
|
MessageID: msgID,
|
||||||
|
Type: msgType,
|
||||||
|
Content: content,
|
||||||
|
Direction: "inbound",
|
||||||
|
Status: "received",
|
||||||
|
}
|
||||||
|
|
||||||
|
if contextID != "" {
|
||||||
|
conv.Content += fmt.Sprintf(" (réponse à %s)", contextID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Create(&conv).Error; err != nil {
|
||||||
|
log.Println("❌ Erreur enregistrement conversation:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func CreateUser(db *gorm.DB) http.HandlerFunc {
|
func CreateUser(db *gorm.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var user models.User
|
var user models.User
|
||||||
@ -474,9 +503,7 @@ func HandleTemplateTest(db *gorm.DB) http.HandlerFunc {
|
|||||||
http.Error(w, "Échec de l'appel API WhatsApp", http.StatusBadGateway)
|
http.Error(w, "Échec de l'appel API WhatsApp", http.StatusBadGateway)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close();
|
||||||
|
|
||||||
// Lire la réponse pour capturer le message ID ou statut
|
|
||||||
var respBody map[string]interface{}
|
var respBody map[string]interface{}
|
||||||
json.NewDecoder(resp.Body).Decode(&respBody)
|
json.NewDecoder(resp.Body).Decode(&respBody)
|
||||||
|
|
||||||
@ -487,10 +514,17 @@ func HandleTemplateTest(db *gorm.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = models.SaveMessageStatusError(db, user.ID, to, messageID, "sent", fmt.Sprintf("Template: %s", templateName))
|
description := fmt.Sprintf("Template: %s, Lang: %s, Params: %v", templateName, lang, params)
|
||||||
if err != nil {
|
db.Create(&models.Conversation{
|
||||||
fmt.Println("⚠️ Erreur enregistrement statut message:", err)
|
UserID: user.ID,
|
||||||
}
|
From: "bot",
|
||||||
|
To: to,
|
||||||
|
MessageID: messageID,
|
||||||
|
Type: "template",
|
||||||
|
Content: description,
|
||||||
|
Direction: "outbound",
|
||||||
|
Status: "sent",
|
||||||
|
})
|
||||||
|
|
||||||
w.WriteHeader(resp.StatusCode)
|
w.WriteHeader(resp.StatusCode)
|
||||||
json.NewEncoder(w).Encode(respBody)
|
json.NewEncoder(w).Encode(respBody)
|
||||||
|
|||||||
@ -40,7 +40,6 @@ func RoutesProtected(r *mux.Router, db *gorm.DB) {
|
|||||||
r.HandleFunc("/test/send", renders.TestMessagesPages)
|
r.HandleFunc("/test/send", renders.TestMessagesPages)
|
||||||
r.HandleFunc("/test/send2", renders.TestMessagesPages2)
|
r.HandleFunc("/test/send2", renders.TestMessagesPages2)
|
||||||
r.HandleFunc("/admin/user/{id}/conversation-thread", renders.AdminConversationThread(db)).Methods("GET")
|
r.HandleFunc("/admin/user/{id}/conversation-thread", renders.AdminConversationThread(db)).Methods("GET")
|
||||||
|
|
||||||
r.HandleFunc("/api/message/send2", handlers.HandleTemplateTest(db)).Methods("POST")
|
r.HandleFunc("/api/message/send2", handlers.HandleTemplateTest(db)).Methods("POST")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user