From cc278ecef5b287ad90494240d3d8a5fade3161c3 Mon Sep 17 00:00:00 2001 From: Xor290 Date: Fri, 12 Jun 2026 10:47:30 +0200 Subject: [PATCH] chore: build --- backend/gestion/handlers/telegram.go | 17 ++++++++-- backend/gestion/services/lbtelegram.go | 2 ++ backend/gestion/services/telegram.go | 46 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/backend/gestion/handlers/telegram.go b/backend/gestion/handlers/telegram.go index 4ad4d860..5cafb3ba 100644 --- a/backend/gestion/handlers/telegram.go +++ b/backend/gestion/handlers/telegram.go @@ -96,8 +96,21 @@ func handleLinkAccount(c *gin.Context, token string, chatID int64) { if services.LBTelegram != nil && services.LBTelegram.IsConfigured() { if err := services.LBTelegram.EnrollUser(chatID, username, role); err != nil { log.Printf("⚠️ [LB] enrollment échoué pour %s: %v", username, err) - // fallback: confirmation directe via le bot principal - if services.TelegramBot != nil { + } + + // Envoyer un message avec boutons vers les bots de notifications lbtelegram + if services.TelegramBot != nil && services.LBTelegram.Bot1Username != "" { + buttons := [][2]string{ + {"🔔 Activer les notifications", "https://t.me/" + services.LBTelegram.Bot1Username}, + } + if services.LBTelegram.Bot2Username != "" { + buttons = append(buttons, [2]string{"🔔 Bot secondaire", "https://t.me/" + services.LBTelegram.Bot2Username}) + } + if err := services.TelegramBot.SendMessageWithButtons(chatID, + "✅ Compte lié avec succès !\n\nPour recevoir vos notifications de livraison, appuyez sur le bouton ci-dessous pour démarrer notre bot de notifications :", + buttons, + ); err != nil { + log.Printf("⚠️ [TELEGRAM] Envoi message boutons échoué pour %s: %v", username, err) services.TelegramBot.SendMessage(chatID, "✅ Compte lié avec succès !\n\nVous recevrez désormais vos notifications ici.") } diff --git a/backend/gestion/services/lbtelegram.go b/backend/gestion/services/lbtelegram.go index 85195221..094d01d9 100644 --- a/backend/gestion/services/lbtelegram.go +++ b/backend/gestion/services/lbtelegram.go @@ -16,6 +16,7 @@ var LBTelegram *LBTelegramService type LBTelegramService struct { gatewayURL string Bot1Username string + Bot2Username string client *http.Client } @@ -27,6 +28,7 @@ func NewLBTelegramService() *LBTelegramService { svc := &LBTelegramService{ gatewayURL: url, Bot1Username: os.Getenv("LBTELEGRAM_BOT1_USERNAME"), + Bot2Username: os.Getenv("LBTELEGRAM_BOT2_USERNAME"), client: &http.Client{Timeout: 10 * time.Second}, } LBTelegram = svc diff --git a/backend/gestion/services/telegram.go b/backend/gestion/services/telegram.go index 1a671383..6a035ab4 100644 --- a/backend/gestion/services/telegram.go +++ b/backend/gestion/services/telegram.go @@ -95,6 +95,52 @@ func (t *TelegramService) SendMessage(chatID int64, text string) error { return nil } +// SendMessageWithButtons envoie un message HTML avec des boutons inline (URL buttons). +// buttons est une liste de paires [texte, url]. +func (t *TelegramService) SendMessageWithButtons(chatID int64, text string, buttons [][2]string) error { + if !t.IsConfigured() { + return fmt.Errorf("telegram non configuré") + } + + row := make([]map[string]string, 0, len(buttons)) + for _, b := range buttons { + row = append(row, map[string]string{"text": b[0], "url": b[1]}) + } + + payload := map[string]interface{}{ + "chat_id": chatID, + "text": text, + "parse_mode": "HTML", + "reply_markup": map[string]interface{}{ + "inline_keyboard": [][]map[string]string{row}, + }, + } + + body, err := json.Marshal(payload) + if err != nil { + return fmt.Errorf("marshal: %w", err) + } + + url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", t.botToken) + req, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) + if err != nil { + return fmt.Errorf("création requête: %w", err) + } + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("envoi: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("telegram API status %d", resp.StatusCode) + } + return nil +} + // SetWebhook enregistre l'URL webhook auprès de Telegram func (t *TelegramService) SetWebhook(webhookURL string) error { if !t.IsConfigured() {