forked from crawlab-team/crawlab-core
/
mobile.go
52 lines (44 loc) · 974 Bytes
/
mobile.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
package notification
import (
"errors"
"github.com/crawlab-team/go-trace"
"github.com/imroc/req"
)
type ResBody struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
}
func SendMobileNotification(webhook string, title string, content string) error {
// request header
header := req.Header{
"Content-Type": "application/json; charset=utf-8",
}
// request data
data := req.Param{
"msgtype": "markdown",
"markdown": req.Param{
"title": title,
"text": content,
"content": content,
},
"at": req.Param{
"atMobiles": []string{},
"isAtAll": false,
},
}
// perform request
res, err := req.Post(webhook, header, req.BodyJSON(&data))
if err != nil {
return trace.TraceError(err)
}
// parse response
var resBody ResBody
if err := res.ToJSON(&resBody); err != nil {
return trace.TraceError(err)
}
// validate response code
if resBody.ErrCode != 0 {
return errors.New(resBody.ErrMsg)
}
return nil
}