chore: build
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// TelegramBot est l'instance globale accessible depuis le package db
|
||||
var TelegramBot *TelegramService
|
||||
|
||||
type TelegramService struct {
|
||||
@@ -96,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() {
|
||||
@@ -103,7 +148,7 @@ func (t *TelegramService) SetWebhook(webhookURL string) error {
|
||||
}
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"url": webhookURL,
|
||||
"url": webhookURL,
|
||||
"allowed_updates": []string{"message"},
|
||||
}
|
||||
if t.webhookSecret != "" {
|
||||
|
||||
Reference in New Issue
Block a user