feat: add Gotify support to sendMsg handler
CI / CI (pull_request) Successful in 3m10s

- Add sendGotify method to send notifications to Gotify server
- Support format: 'gotify' in SendMsgRequest
- Extract message ID from Gotify response
- Configurable via GOTIFY_URL and GOTIFY_TOKEN env vars
- Fallback graceful error if Gotify not configured
This commit is contained in:
Admin Bot
2026-09-15 02:36:02 +09:00
parent bff46fefe7
commit d5c7440655
+72
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"net/smtp"
"os"
"strings"
)
// SendMsgRequest represents a sendMsg API request.
@@ -78,6 +79,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
resp = h.sendEmail(req)
case "sms":
resp = h.sendSMS(req)
case "gotify":
resp = h.sendGotify(req)
default:
resp = SendMsgResponse{
Status: "error",
@@ -158,3 +161,72 @@ func (h *Handler) sendSMS(req SendMsgRequest) SendMsgResponse {
Error: "SMS not implemented yet",
}
}
// sendGotify sends a notification via Gotify server.
func (h *Handler) sendGotify(req SendMsgRequest) SendMsgResponse {
if h.gotifyURL == "" || h.gotifyToken == "" {
return SendMsgResponse{
Status: "error",
Error: "Gotify not configured (missing GOTIFY_URL or GOTIFY_TOKEN)",
}
}
// Construct Gotify message
gotifyReq := map[string]interface{}{
"title": req.Title,
"message": req.Message,
"priority": req.Priority,
}
// Marshal to JSON
body, err := json.Marshal(gotifyReq)
if err != nil {
log.Printf("error marshaling Gotify request: %v", err)
return SendMsgResponse{
Status: "error",
Error: "failed to marshal request: " + err.Error(),
}
}
// POST to Gotify
gotifyEndpoint := fmt.Sprintf("%s/message?token=%s", h.gotifyURL, h.gotifyToken)
resp, err := http.Post(gotifyEndpoint, "application/json", strings.NewReader(string(body)))
if err != nil {
log.Printf("error sending to Gotify: %v", err)
return SendMsgResponse{
Status: "error",
Error: "failed to send to Gotify: " + err.Error(),
}
}
defer resp.Body.Close()
// Check response status
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
log.Printf("Gotify returned status %d", resp.StatusCode)
return SendMsgResponse{
Status: "error",
Error: fmt.Sprintf("Gotify returned status %d", resp.StatusCode),
}
}
// Parse response
var gotifyResp map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&gotifyResp); err != nil {
log.Printf("error decoding Gotify response: %v", err)
return SendMsgResponse{
Status: "success",
MessageID: "gotify-sent",
}
}
// Extract message ID if available
msgID := "gotify-sent"
if id, ok := gotifyResp["id"]; ok {
msgID = fmt.Sprintf("gotify-%v", id)
}
return SendMsgResponse{
Status: "success",
MessageID: msgID,
}
}