forked from eternal-flame-AD/gotify-broadcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
47 lines (41 loc) · 1.14 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"errors"
"github.com/gotify/plugin-api"
"github.com/gin-gonic/gin"
)
func (c *Plugin) hasChannel(channel string) bool {
for _, ch := range c.config.Channels {
if ch.Name == channel {
return true
}
}
return false
}
type message struct {
Message string `json:"message" query:"message" form:"message"`
Title string `json:"title" query:"title" form:"title"`
Priority int `json:"priority" query:"priority" form:"priority"`
Extras map[string]interface{} `json:"extras" query:"-" form:"-"`
}
// RegisterWebhook implements plugin.Webhooker
func (c *Plugin) RegisterWebhook(basePath string, mux *gin.RouterGroup) {
c.basePath = basePath
mux.POST("/message", func(ctx *gin.Context) {
channel := ctx.Query("channel")
if !c.hasChannel(channel) {
ctx.AbortWithError(400, errors.New("channel not found"))
return
}
msg := new(message)
if err := ctx.Bind(msg); err == nil {
c.sendMessage(plugin.Message{
Message: msg.Message,
Title: msg.Title,
Priority: msg.Priority,
Extras: msg.Extras,
}, channel)
ctx.JSON(200, msg)
}
})
}