|
| 1 | +package dingtalk |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/hmac" |
| 6 | + "crypto/sha256" |
| 7 | + "encoding/base64" |
| 8 | + "encoding/json" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "github.com/yi-nology/common/utils/xjson" |
| 12 | + "io" |
| 13 | + "io/ioutil" |
| 14 | + "net/http" |
| 15 | + "net/url" |
| 16 | + "strconv" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +type Robot struct { |
| 21 | + Key string |
| 22 | + RequestUrl string |
| 23 | + Client *http.Client |
| 24 | +} |
| 25 | + |
| 26 | +type Resp struct { |
| 27 | + ErrCode int `json:"errcode"` |
| 28 | + ErrMsg string `json:"errmsg"` |
| 29 | +} |
| 30 | + |
| 31 | +const ( |
| 32 | + WEBHOOK_URL = "https://oapi.dingtalk.com/robot/send?access_token=%s" |
| 33 | +) |
| 34 | + |
| 35 | +// New 获取一个 Robot instance |
| 36 | +func New(botKey string) *Robot { |
| 37 | + return &Robot{ |
| 38 | + Key: botKey, |
| 39 | + RequestUrl: fmt.Sprintf(WEBHOOK_URL, botKey), |
| 40 | + Client: &http.Client{Timeout: 5 * time.Second}, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// AddSign 更新SDK 支持Sign模式 |
| 45 | +func (r *Robot) AddSign(secret string) *Robot { |
| 46 | + if len(secret) == 0 { |
| 47 | + return r |
| 48 | + } |
| 49 | + microTimestamp := time.Now().UnixNano() / 1e6 |
| 50 | + h := hmac.New(sha256.New, []byte(secret)) |
| 51 | + io.WriteString(h, fmt.Sprintf("%d\n%s", microTimestamp, secret)) |
| 52 | + if r.RequestUrl != "" { |
| 53 | + r.RequestUrl = r.RequestUrl + "×tamp=" + strconv.Itoa(int(microTimestamp)) + "&sign=" + url.QueryEscape(base64.StdEncoding.EncodeToString(h.Sum(nil))) |
| 54 | + } |
| 55 | + return r |
| 56 | +} |
| 57 | + |
| 58 | +// Send 发送notification |
| 59 | +func (r *Robot) Send(message interface{}) (bool, error) { |
| 60 | + b, err := json.Marshal(message) |
| 61 | + if err != nil { |
| 62 | + return false, err |
| 63 | + } |
| 64 | + // log.Println(string(b)) |
| 65 | + return r.SendRaw(b) |
| 66 | +} |
| 67 | + |
| 68 | +func (r *Robot) SendRaw(msgBytes []byte) (bool, error) { |
| 69 | + resp, err := r.Client.Post(r.RequestUrl, "application/json", bytes.NewReader(msgBytes)) |
| 70 | + if err != nil { |
| 71 | + return false, err |
| 72 | + } |
| 73 | + |
| 74 | + defer resp.Body.Close() |
| 75 | + body, err := ioutil.ReadAll(resp.Body) |
| 76 | + if err != nil { |
| 77 | + return false, err |
| 78 | + } |
| 79 | + |
| 80 | + ret := &Resp{} |
| 81 | + |
| 82 | + if err := json.Unmarshal(body, &ret); err != nil { |
| 83 | + return false, err |
| 84 | + } |
| 85 | + |
| 86 | + if ret.ErrCode == 0 { |
| 87 | + return true, nil |
| 88 | + } else { |
| 89 | + return false, errors.New(ret.ErrMsg) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func (r *Robot) CheckMessage(msg string) bool { |
| 94 | + |
| 95 | + if len(msg) == 0 { |
| 96 | + return false |
| 97 | + } |
| 98 | + text := TextMessage{} |
| 99 | + markdown := MarkDownMessage{} |
| 100 | + link := LinkMessage{} |
| 101 | + feedcard := FeedCardMessage{} |
| 102 | + actionCard := ActionCardMessage{} |
| 103 | + if xjson.UnmarshalFromString(msg, &text) == nil { |
| 104 | + return true |
| 105 | + } |
| 106 | + if xjson.UnmarshalFromString(msg, &markdown) == nil { |
| 107 | + return true |
| 108 | + } |
| 109 | + if xjson.UnmarshalFromString(msg, &link) == nil { |
| 110 | + return true |
| 111 | + } |
| 112 | + if xjson.UnmarshalFromString(msg, &feedcard) == nil { |
| 113 | + return true |
| 114 | + } |
| 115 | + if xjson.UnmarshalFromString(msg, &actionCard) == nil { |
| 116 | + return true |
| 117 | + } |
| 118 | + return false |
| 119 | +} |
0 commit comments