lellele
This commit is contained in:
parent
9b0f118a7e
commit
2aa1739b3f
@ -307,6 +307,41 @@ func WebhookVerifyHandler() http.HandlerFunc {
|
|||||||
w.Write([]byte("Invalid verify token"))
|
w.Write([]byte("Invalid verify token"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func WebhookReceiveHandler(db *gorm.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var body map[string]interface{}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||||
|
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("📩 Webhook reçu :", body)
|
||||||
|
|
||||||
|
// Exemple de parsing
|
||||||
|
entry := body["entry"].([]interface{})[0].(map[string]interface{})
|
||||||
|
change := entry["changes"].([]interface{})[0].(map[string]interface{})
|
||||||
|
value := change["value"].(map[string]interface{})
|
||||||
|
|
||||||
|
messages := value["messages"].([]interface{})
|
||||||
|
if len(messages) > 0 {
|
||||||
|
msg := messages[0].(map[string]interface{})
|
||||||
|
from := msg["from"].(string)
|
||||||
|
msgType := msg["type"].(string)
|
||||||
|
|
||||||
|
var content string
|
||||||
|
if msgType == "text" {
|
||||||
|
content = msg["text"].(map[string]interface{})["body"].(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("💬 Message reçu de %s : %s\n", from, content)
|
||||||
|
|
||||||
|
// TODO : enregistrer en base
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
@ -21,6 +21,7 @@ func RoutesPublic(r *mux.Router, db *gorm.DB) {
|
|||||||
// Page de login
|
// Page de login
|
||||||
r.HandleFunc("/login", renders.Login)
|
r.HandleFunc("/login", renders.Login)
|
||||||
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookVerifyHandler()).Methods("GET")
|
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookVerifyHandler()).Methods("GET")
|
||||||
|
r.HandleFunc("/api/whatsapp/webhook", handlers.WebhookReceiveHandler(db)).Methods("POST")
|
||||||
|
|
||||||
// Endpoint d'API pour se logger
|
// Endpoint d'API pour se logger
|
||||||
r.HandleFunc("/api/login", handlers.LoginHandler(db)).Methods("POST")
|
r.HandleFunc("/api/login", handlers.LoginHandler(db)).Methods("POST")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user