-
Notifications
You must be signed in to change notification settings - Fork 92
/
webhook.go
139 lines (110 loc) · 2.86 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package wrobot
import (
"io"
"github.com/gin-gonic/gin"
"github.com/opentdp/wrest-chat/dbase/webhook"
"github.com/opentdp/wrest-chat/wclient"
"github.com/opentdp/wrest-chat/wclient/whapp"
)
type Webhook struct{}
// @Summary webhook列表
// @Produce json
// @Tags BOT::webhook
// @Success 200 {array} tables.Webhook
// @Router /bot/webhook/list [post]
func (*Webhook) list(c *gin.Context) {
if lst, err := webhook.FetchAll(); err == nil {
c.Set("Payload", lst)
} else {
c.Set("Error", err)
}
}
// @Summary 获取webhook
// @Produce json
// @Tags BOT::webhook
// @Param body body webhook.FetchWebhookParam true "获取webhook参数"
// @Success 200 {object} tables.Webhook
// @Router /bot/webhook/detail [post]
func (*Webhook) detail(c *gin.Context) {
var rq *webhook.FetchWebhookParam
if err := c.ShouldBind(&rq); err != nil {
c.Set("Error", err)
return
}
if res, err := webhook.Fetch(rq); err == nil {
c.Set("Payload", res)
} else {
c.Set("Error", err)
}
}
// @Summary 添加webhook
// @Produce json
// @Tags BOT::webhook
// @Param body body webhook.CreateWebhookParam true "添加webhook参数"
// @Success 200
// @Router /bot/webhook/create [post]
func (*Webhook) create(c *gin.Context) {
var rq *webhook.CreateWebhookParam
if err := c.ShouldBind(&rq); err != nil {
c.Set("Error", err)
return
}
if token, err := webhook.Create(rq); err == nil {
c.Set("Message", "添加成功")
c.Set("Payload", token)
} else {
c.Set("Error", err)
}
}
// @Summary 删除webhook
// @Produce json
// @Tags BOT::webhook
// @Param body body webhook.DeleteWebhookParam true "删除webhook"
// @Success 200
// @Router /bot/webhook/delete [post]
func (*Webhook) delete(c *gin.Context) {
var rq *webhook.DeleteWebhookParam
if err := c.ShouldBind(&rq); err != nil {
c.Set("Error", err)
return
}
if err := webhook.Delete(rq); err == nil {
c.Set("Message", "删除成功")
} else {
c.Set("Error", err)
}
}
// @Summary 接收webhook消息
// @Produce json
// @Tags BOT::webhook
// @Param token path string true "webhook token"
// @Param app path string true "webhook类型(例如: github, gitea)"
// @Param body body interface{} true "event报文"
// @Success 200
// @Router /bot/webhook/{token}/{app} [post]
func (w *Webhook) receive(c *gin.Context) {
token := c.Param("token")
app := c.Param("app")
hook, err := webhook.Fetch(&webhook.FetchWebhookParam{Token: token})
if err != nil {
c.Set("Error", err)
return
}
request, err := io.ReadAll(c.Request.Body)
if err != nil {
c.Set("Error", err)
return
}
wc := wclient.Register()
// 根据app类型不同,调用不同的处理方式,参照handler的注册
msg := whapp.Handler(c.Request.Header, app, string(request))
var res int32
if msg != "" {
res = wc.CmdClient.SendTxt(msg, hook.TargetId, "")
}
if res == 0 {
c.Set("Message", "OK")
} else {
c.Set("Error", "消息处理失败")
}
}