uptime kuma webhook

This commit is contained in:
Julian Graf 2023-08-01 19:18:28 +02:00
parent 5af06b63a9
commit 3a2733c1b6
3 changed files with 90 additions and 17 deletions

View File

@ -318,8 +318,8 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
// @Description All parameter can be set via query-parameter or the json body. Only UserID, UserKey and Title are required
// @Tags External
//
// @Param query_data query handler.UptimeKumaWebHook.query false " "
// @Param post_body body handler.UptimeKumaWebHook.body false " "
// @Param query_data query handler.UptimeKumaWebHook.query false " "
// @Param post_body body handler.UptimeKumaWebHook.uptimeKumaWebhookBody false " "
//
// @Success 200 {object} any
// @Failure 400 {object} ginresp.apiError
@ -333,11 +333,23 @@ func (h MessageHandler) UptimeKumaWebHook(g *gin.Context) ginresp.HTTPResponse {
UserID *models.UserID `form:"user_id" example:"7725"`
KeyToken *string `form:"key" example:"P3TNH8mvv14fm"`
}
type body struct {
Title string `json:"Title"`
Body string `json:"Body"`
type uptimeKumaWebhookBody struct {
Heartbeat struct {
Time string `json:"time"`
Msg string `json:"msg"`
Timezone string `json:"timezone"`
TimezoneOffset string `json:"timezoneOffset"`
LocalDateTime string `json:"localDateTime"`
} `json:"heartbeat"`
Monitor struct {
Name string `json:"name"`
Url *string `json:"url"`
} `json:"monitor"`
Msg string `json:"msg"`
}
var b body
var b uptimeKumaWebhookBody
var q query
ctx, httpErr := h.app.StartRequest(g, nil, &q, &b, nil)
@ -346,7 +358,22 @@ func (h MessageHandler) UptimeKumaWebHook(g *gin.Context) ginresp.HTTPResponse {
return *httpErr
}
okResp, errResp := h.sendMessageInternal(g, ctx, q.UserID, q.KeyToken, nil, &b.Title, &b.Body, langext.Ptr(1), nil, nil, nil)
title := fmt.Sprintf("[UptimeKuma] %v down!", b.Monitor.Name)
content := fmt.Sprintf("%v\n", b.Monitor.Name)
content += fmt.Sprintf("%v\n", b.Msg)
if b.Monitor.Url != nil {
content += fmt.Sprintf("url: %v\n", b.Monitor.Url)
}
content += "===== Heartbeat ======"
content += fmt.Sprintf("msg: %v\n", b.Heartbeat.Msg)
content += fmt.Sprintf("timestamp: %v\n", b.Heartbeat.Time)
content += fmt.Sprintf("timezone: %v\n", b.Heartbeat.Timezone)
content += fmt.Sprintf("timezone offset: %v\n", b.Heartbeat.TimezoneOffset)
content += fmt.Sprintf("local date time: %v\n", b.Heartbeat.TimezoneOffset)
okResp, errResp := h.sendMessageInternal(g, ctx, q.UserID, q.KeyToken, nil, &title, &content, langext.Ptr(1), nil, nil, nil)
if errResp != nil {
return *errResp

View File

@ -2757,7 +2757,7 @@
"name": "post_body",
"in": "body",
"schema": {
"$ref": "#/definitions/handler.UptimeKumaWebHook.body"
"$ref": "#/definitions/handler.UptimeKumaWebHook.uptimeKumaWebhookBody"
}
}
],
@ -3449,13 +3449,41 @@
}
}
},
"handler.UptimeKumaWebHook.body": {
"handler.UptimeKumaWebHook.uptimeKumaWebhookBody": {
"type": "object",
"properties": {
"Body": {
"type": "string"
"heartbeat": {
"type": "object",
"properties": {
"localDateTime": {
"type": "string"
},
"msg": {
"type": "string"
},
"time": {
"type": "string"
},
"timezone": {
"type": "string"
},
"timezoneOffset": {
"type": "string"
}
}
},
"Title": {
"monitor": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string"
}
}
},
"msg": {
"type": "string"
}
}

View File

@ -458,11 +458,29 @@ definitions:
user_id:
type: integer
type: object
handler.UptimeKumaWebHook.body:
handler.UptimeKumaWebHook.uptimeKumaWebhookBody:
properties:
Body:
type: string
Title:
heartbeat:
properties:
localDateTime:
type: string
msg:
type: string
time:
type: string
timezone:
type: string
timezoneOffset:
type: string
type: object
monitor:
properties:
name:
type: string
url:
type: string
type: object
msg:
type: string
type: object
handler.pingResponse:
@ -2600,7 +2618,7 @@ paths:
in: body
name: post_body
schema:
$ref: '#/definitions/handler.UptimeKumaWebHook.body'
$ref: '#/definitions/handler.UptimeKumaWebHook.uptimeKumaWebhookBody'
responses:
"200":
description: OK