-
Notifications
You must be signed in to change notification settings - Fork 79
/
handler_webhook.go
70 lines (61 loc) · 1.67 KB
/
handler_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
package robot
import (
"fmt"
"github.com/opentdp/wrest-chat/dbase/setting"
"github.com/opentdp/wrest-chat/dbase/webhook"
"github.com/opentdp/wrest-chat/wcferry"
)
func webhookHandler() []*Handler {
cmds := []*Handler{}
if len(setting.ApiEndpoint) < 10 {
return cmds
}
cmds = append(cmds, &Handler{
Level: 7,
Order: 210,
Roomid: "*",
Command: "/webhook",
Describe: "创建 Webhook",
Callback: func(msg *wcferry.WxMsg) string {
target := msg.Sender
if msg.IsGroup {
target = msg.GetRoomid()
}
// 已存在
item, err := webhook.Fetch(&webhook.FetchWebhookParam{
TargetId: target,
})
if err == nil {
return fmt.Sprintf("webhook 调用地址: /bot/webhook/%s/{type}", item.Token)
}
// 创建新的 Webhook
token, err := webhook.Create(&webhook.CreateWebhookParam{
TargetId: target,
Remark: fmt.Sprintf("由用户[%s]通过指令创建", msg.Sender),
})
if err != nil {
return "webhook 创建失败," + err.Error()
}
return fmt.Sprintf("webhook 已添加\nToken: %s\n调用地址: /bot/webhook/%s/{type}\ntype 为不同类型的应用发送的webhook(如github, gitea)\n自定义的请填写text直接原样发送body", token, token)
},
})
cmds = append(cmds, &Handler{
Level: 7,
Order: 211,
Roomid: "*",
Command: "/webhook:rm",
Describe: "删除 Webhook",
Callback: func(msg *wcferry.WxMsg) string {
target := msg.Sender
if msg.IsGroup {
target = msg.GetRoomid()
}
res := webhook.Delete(&webhook.DeleteWebhookParam{TargetId: target})
if res != nil {
return "webhook 删除失败," + res.Error()
}
return "webhook 删除成功"
},
})
return cmds
}