-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrouter.go
91 lines (82 loc) · 1.89 KB
/
router.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
package main
import (
"bytes"
"fmt"
"net/http"
"strconv"
"github.com/gofiber/fiber"
)
type Hook struct {
dashboardId string `json:"dashboardId"`
evalMatches string `json:"evalMatches"`
ImageUrl string `json:"imageUrl"`
Message string `json:"message"`
orgId string `json:"orgId"`
panelId string `json:"panelId"`
ruleId string `json:"ruleId"`
ruleName string `json:"ruleName"`
RuleUrl string `json:"ruleUrl"`
state string `json:"state"`
tags string `json:"tags"`
Title string `json:"title"`
}
var sent_count int = 0
func GwStat() func(c *fiber.Ctx) {
return func(c *fiber.Ctx) {
stat_msg := "G2WW Server created by Nova Kwok is running! \nParsed & forwarded " + strconv.Itoa(sent_count) + " messages to WeChat Work!"
c.Send(stat_msg)
return
}
}
func GwWorker() func(c *fiber.Ctx) {
return func(c *fiber.Ctx) {
h := new(Hook)
if err := c.BodyParser(h); err != nil {
fmt.Println(err)
c.Send("Error on JSON format")
return
}
// Send to WeChat Work
// {
// "msgtype": "news",
// "news": {
// "articles": [
// {
// "title": "%s",
// "description": "%s",
// "url": "%s",
// "picurl": "%s"
// }
// ]
// }
// }
url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + c.Params("key")
msgStr := fmt.Sprintf(`
{
"msgtype": "news",
"news": {
"articles": [
{
"title": "%s",
"description": "%s",
"url": "%s",
"picurl": "%s"
}
]
}
}
`, h.Title, h.Message, h.RuleUrl, h.ImageUrl)
jsonStr := []byte(msgStr)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
c.Send("Error sending to WeChat Work API")
return
}
defer resp.Body.Close()
c.Send(resp)
sent_count++
}
}