ip
This commit is contained in:
@@ -4,12 +4,15 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
||||
)
|
||||
|
||||
@@ -204,3 +207,61 @@ func sendTelegramMessage(botToken, chatID, text string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ─── Webhook portal (set + info) ─────────────────────────────────────────────
|
||||
|
||||
// SetPortalWebhook registra el webhook del portal en la API de Telegram para el bot indicado.
|
||||
// POST /app/telegram/:id/set-portal-webhook
|
||||
func SetPortalWebhook(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 32)
|
||||
if err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "ID inválido"})
|
||||
}
|
||||
cfg, err := models.GetTelegramConfigByID(uint(id))
|
||||
if err != nil {
|
||||
return c.Status(404).JSON(fiber.Map{"error": "Configuración no encontrada"})
|
||||
}
|
||||
|
||||
// Construir URL base desde la configuración de la app
|
||||
baseURL := app.Http.Server.Url
|
||||
if len(baseURL) > 0 && baseURL[len(baseURL)-1] == '/' {
|
||||
baseURL = baseURL[:len(baseURL)-1]
|
||||
}
|
||||
webhookURL := baseURL + "/webhooks/telegram-portal"
|
||||
|
||||
apiURL := fmt.Sprintf("https://api.telegram.org/bot%s/setWebhook", cfg.BotToken)
|
||||
payload, _ := json.Marshal(map[string]string{"url": webhookURL})
|
||||
resp, err := http.Post(apiURL, "application/json", bytes.NewReader(payload)) //nolint:noctx
|
||||
if err != nil {
|
||||
return c.Status(502).JSON(fiber.Map{"ok": false, "error": err.Error()})
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
|
||||
var tgResp map[string]interface{}
|
||||
_ = json.Unmarshal(raw, &tgResp)
|
||||
log.Printf("[SetPortalWebhook] bot=%d url=%s resp=%s", id, webhookURL, string(raw))
|
||||
return c.JSON(fiber.Map{"ok": tgResp["ok"], "webhook_url": webhookURL, "telegram": tgResp})
|
||||
}
|
||||
|
||||
// GetPortalWebhookInfo consulta el estado actual del webhook en Telegram.
|
||||
// GET /app/telegram/:id/webhook-info
|
||||
func GetPortalWebhookInfo(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 32)
|
||||
if err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "ID inválido"})
|
||||
}
|
||||
cfg, err := models.GetTelegramConfigByID(uint(id))
|
||||
if err != nil {
|
||||
return c.Status(404).JSON(fiber.Map{"error": "Configuración no encontrada"})
|
||||
}
|
||||
apiURL := fmt.Sprintf("https://api.telegram.org/bot%s/getWebhookInfo", cfg.BotToken)
|
||||
resp, err := http.Get(apiURL) //nolint:noctx
|
||||
if err != nil {
|
||||
return c.Status(502).JSON(fiber.Map{"ok": false, "error": err.Error()})
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var result map[string]interface{}
|
||||
_ = json.NewDecoder(resp.Body).Decode(&result)
|
||||
return c.JSON(result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user