This commit is contained in:
cangui 2025-05-09 16:03:44 +02:00
parent faf5053fa5
commit f47da5f2ce
2 changed files with 39 additions and 16 deletions

View File

@ -342,9 +342,14 @@ func WebhookReceiveHandler(db *gorm.DB) http.HandlerFunc {
content = msg["text"].(map[string]interface{})["body"].(string) content = msg["text"].(map[string]interface{})["body"].(string)
} }
contextID := "" var parentID *uint
if ctx, ok := msg["context"].(map[string]interface{}); ok { if ctx, ok := msg["context"].(map[string]interface{}); ok {
contextID = fmt.Sprintf("%v", ctx["id"]) contextID := fmt.Sprintf("%v", ctx["id"])
var parent models.Conversation
if err := db.Where("message_id = ?", contextID).First(&parent).Error; err == nil {
parentID = &parent.ID
content += fmt.Sprintf(" (réponse à %s)", contextID)
}
} }
conv := models.Conversation{ conv := models.Conversation{
@ -356,10 +361,7 @@ func WebhookReceiveHandler(db *gorm.DB) http.HandlerFunc {
Content: content, Content: content,
Direction: "inbound", Direction: "inbound",
Status: "received", Status: "received",
} ParentID: parentID,
if contextID != "" {
conv.Content += fmt.Sprintf(" (réponse à %s)", contextID)
} }
if err := db.Create(&conv).Error; err != nil { if err := db.Create(&conv).Error; err != nil {
@ -372,6 +374,7 @@ func WebhookReceiveHandler(db *gorm.DB) http.HandlerFunc {
} }
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
@ -504,7 +507,8 @@ 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()
var respBody map[string]interface{} var respBody map[string]interface{}
json.NewDecoder(resp.Body).Decode(&respBody) json.NewDecoder(resp.Body).Decode(&respBody)
@ -532,3 +536,4 @@ func HandleTemplateTest(db *gorm.DB) http.HandlerFunc {
} }
} }

View File

@ -1,14 +1,27 @@
{{ define "admin_conversations_thread.pages.tmpl" }} {{ define "admin_conversations_thread.pages.tmpl" }}
<div class="chat-thread"> <div class="chat-thread">
{{ $parentMap := dict }}
{{ range .Conversations }} {{ range .Conversations }}
<div class="bubble {{ if eq .Direction "inbound" }}left{{ else }}right{{ end }}"> {{ if not .ParentID }}
<div class="meta"> <div class="bubble {{ if eq .Direction "inbound" }}left{{ else }}right{{ end }}">
<small><strong>{{ .From }}</strong> — {{ .CreatedAt.Format "2006-01-02 15:04:05" }}</small> <div class="meta">
<small><strong>{{ .From }}</strong> — {{ .CreatedAt.Format "2006-01-02 15:04:05" }}</small>
</div>
<div class="content">{{ .Content }}</div>
</div> </div>
<div class="content">
{{ .Content }} {{/* Afficher les enfants */}}
</div> {{ range $i, $c := $.Conversations }}
</div> {{ if and $c.ParentID (eq (printf "%d" $c.ParentID) (printf "%d" .ID)) }}
<div class="bubble reply {{ if eq $c.Direction "inbound" }}left{{ else }}right{{ end }}">
<div class="meta">
<small><strong>{{ $c.From }}</strong> — {{ $c.CreatedAt.Format "2006-01-02 15:04:05" }}</small>
</div>
<div class="content">{{ $c.Content }}</div>
</div>
{{ end }}
{{ end }}
{{ end }}
{{ end }} {{ end }}
</div> </div>
@ -18,7 +31,7 @@
background: #f5f5f5; background: #f5f5f5;
border: 1px solid #ccc; border: 1px solid #ccc;
border-radius: 0.5rem; border-radius: 0.5rem;
max-height: 400px; max-height: 600px;
overflow-y: auto; overflow-y: auto;
} }
.bubble { .bubble {
@ -37,6 +50,11 @@
background-color: #d1e7dd; background-color: #d1e7dd;
float: right; float: right;
} }
.bubble.reply {
margin-left: 2rem;
margin-right: 2rem;
opacity: 0.9;
}
.meta { .meta {
font-size: 0.75rem; font-size: 0.75rem;
color: #666; color: #666;