-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add alert sending timeout and dingtalk robot (#40)
- Loading branch information
Showing
11 changed files
with
201 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package dingtalk | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// Robot represents a dingtalk custom robot that can send messages to groups. | ||
type Robot struct { | ||
webHook string | ||
secret string | ||
} | ||
|
||
func NewRobot(webhook, secrect string) *Robot { | ||
return &Robot{webHook: webhook, secret: secrect} | ||
} | ||
|
||
// SendMarkdown send a markdown type message. | ||
func (r Robot) SendMarkdown(ctx context.Context, title, text string, atMobiles []string, isAtAll bool) error { | ||
return r.send(ctx, &markdownMessage{ | ||
MsgType: msgTypeMarkdown, | ||
Markdown: markdownParams{ | ||
Title: title, | ||
Text: text, | ||
}, | ||
At: atParams{ | ||
AtMobiles: atMobiles, | ||
IsAtAll: isAtAll, | ||
}, | ||
}) | ||
} | ||
|
||
type dingResponse struct { | ||
Errcode int `json:"errcode"` | ||
Errmsg string `json:"errmsg"` | ||
} | ||
|
||
func (r Robot) send(ctx context.Context, msg interface{}) error { | ||
jm, err := json.Marshal(msg) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed to marshal message") | ||
} | ||
|
||
webURL := r.webHook | ||
if len(r.secret) != 0 { | ||
webURL += genSignedURL(r.secret) | ||
} | ||
|
||
req, errRequest := http.NewRequestWithContext(ctx, http.MethodPost, webURL, bytes.NewReader(jm)) | ||
if errRequest != nil { | ||
return errors.WithMessage(errRequest, "failed to create request") | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
resp, errDo := http.DefaultClient.Do(req) | ||
if errDo != nil { | ||
return errors.WithMessage(errDo, "failed to do http request") | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, errReadBody := io.ReadAll(resp.Body) | ||
if errReadBody != nil { | ||
return errors.WithMessage(errReadBody, "failed to read http response body") | ||
} | ||
|
||
var dr dingResponse | ||
err = json.Unmarshal(body, &dr) | ||
if err != nil { | ||
return err | ||
} | ||
if dr.Errcode != 0 { | ||
return fmt.Errorf("dingrobot send failed: %v", dr.Errmsg) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func genSignedURL(secret string) string { | ||
timeStr := fmt.Sprintf("%d", time.Now().UnixNano()/1e6) | ||
sign := fmt.Sprintf("%s\n%s", timeStr, secret) | ||
signData := computeHmacSha256(sign, secret) | ||
encodeURL := url.QueryEscape(signData) | ||
return fmt.Sprintf("×tamp=%s&sign=%s", timeStr, encodeURL) | ||
} | ||
|
||
func computeHmacSha256(message string, secret string) string { | ||
key := []byte(secret) | ||
h := hmac.New(sha256.New, key) | ||
h.Write([]byte(message)) | ||
return base64.StdEncoding.EncodeToString(h.Sum(nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dingtalk | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
robot *Robot | ||
) | ||
|
||
// Please set the following enviroments before running tests: | ||
// `TEST_DINGTALK_WEBHOOK`: DingTalk webhook; | ||
// `TEST_DINGTALK_SECRET`: DingTalk secret. | ||
|
||
func TestMain(m *testing.M) { | ||
webhook := os.Getenv("TEST_DINGTALK_WEBHOOK") | ||
secrect := os.Getenv("TEST_DINGTALK_SECRET") | ||
|
||
if len(webhook) > 0 && len(secrect) > 0 { | ||
robot = NewRobot(webhook, secrect) | ||
} | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestSendMarkdown(t *testing.T) { | ||
if robot == nil { | ||
t.SkipNow() | ||
return | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
|
||
// Please manually check if message sent to dingtalk group chat | ||
err := robot.SendMarkdown(ctx, "test", "# Hello, test!", nil, false) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dingtalk | ||
|
||
const ( | ||
msgTypeMarkdown = "markdown" | ||
) | ||
|
||
type atParams struct { | ||
AtMobiles []string `json:"atMobiles,omitempty"` | ||
IsAtAll bool `json:"isAtAll,omitempty"` | ||
} | ||
|
||
type markdownMessage struct { | ||
MsgType string `json:"msgtype"` | ||
Markdown markdownParams `json:"markdown"` | ||
At atParams `json:"at"` | ||
} | ||
|
||
type markdownParams struct { | ||
Title string `json:"title"` | ||
Text string `json:"text"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters