-
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 dingtalk lark and wecom chatbots
- Loading branch information
1 parent
aa75b2b
commit becddab
Showing
5 changed files
with
205 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ icon.png | |
qr.png | ||
go_fir_cli | ||
go_fir_cli.exe | ||
test.go | ||
*.apk | ||
*.ipa | ||
.vscode/* |
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,58 @@ | ||
package notifiers | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
// https://open.dingtalk.com/document/group/custom-robot-access | ||
|
||
type DingTalkNotifier struct { | ||
Key string | ||
SecretToken string | ||
} | ||
|
||
func (d *DingTalkNotifier) Notify(message string) error { | ||
var jsonStr string | ||
|
||
url := fmt.Sprintf("https://oapi.dingtalk.com/robot/send?access_token=%s", d.Key) | ||
|
||
if d.SecretToken != "" { | ||
timestamp := time.Now().UnixMilli() | ||
signature, err := sign(d.SecretToken, timestamp) | ||
if err != nil { | ||
return err | ||
} | ||
url = fmt.Sprintf("%s×tamp=%d&sign=%s", url, timestamp, signature) | ||
} | ||
|
||
jsonStr = fmt.Sprintf(`{ | ||
"msgtype": "text", | ||
"text": { | ||
"content": "%s" | ||
} | ||
}`, message) | ||
|
||
resp, err := resty.New().R().SetBody(jsonStr).SetHeader("Content-Type", "application/json").Post(url) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode() >= 400 { | ||
return fmt.Errorf("请求失败 %s, %s", resp.Status(), string(resp.Body())) | ||
} | ||
|
||
} | ||
|
||
func sign(secret string, t int64) (string, error) { | ||
strToHash := fmt.Sprintf("%d\n%s", t, secret) | ||
hmac256 := hmac.New(sha256.New, []byte(secret)) | ||
hmac256.Write([]byte(strToHash)) | ||
data := hmac256.Sum(nil) | ||
return base64.StdEncoding.EncodeToString(data), 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,71 @@ | ||
package notifiers | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
type LarkNotifier struct { | ||
key string | ||
SecretToken string | ||
} | ||
|
||
// https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN | ||
|
||
func (l *LarkNotifier) Notify(message string) error { | ||
|
||
var jsonStr string | ||
|
||
if l.SecretToken != "" { | ||
timestamp := time.Now().Unix() | ||
signature, err := GenSign(l.SecretToken, timestamp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
jsonStr = fmt.Sprintf(`{ | ||
"timestamp": %v, | ||
"sign": "%s", | ||
"msg_type": "text", | ||
"content": { | ||
"text": "%s" | ||
} | ||
}`, timestamp, signature, message) | ||
|
||
} else { | ||
jsonStr = fmt.Sprintf(`{ | ||
"msg_type": "text", | ||
"content": { | ||
"text": "Update reminder" | ||
} | ||
} `) | ||
} | ||
|
||
url := fmt.Sprintf("https://open.feishu.cn/open-apis/bot/v2/hook/%s", l.key) | ||
resp, err := resty.New().R().SetBody(jsonStr).SetHeader("Content-Type", "application/json").Post(url) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode() >= 400 { | ||
return fmt.Errorf("请求失败 %s, %s", resp.Status(), string(resp.Body())) | ||
} | ||
} | ||
|
||
func GenSign(secret string, timestamp int64) (string, error) { | ||
//Encode timestamp + key with SHA256, and then with Base64 | ||
stringToSign := fmt.Sprintf("%v", timestamp) + "\n" + secret | ||
var data []byte | ||
h := hmac.New(sha256.New, []byte(stringToSign)) | ||
_, err := h.Write(data) | ||
if err != nil { | ||
return "", err | ||
} | ||
signature := base64.StdEncoding.EncodeToString(h.Sum(nil)) | ||
return signature, 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,47 @@ | ||
package notifiers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
type WeComNotifier struct { | ||
Key string | ||
} | ||
|
||
type Article struct { | ||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
Url string `json:"url"` | ||
PicUrl string `json:"picurl"` | ||
} | ||
|
||
func (w *WeComNotifier) Notify(message string) error { | ||
|
||
jsonStr := fmt.Sprintf(`{ | ||
"msgtype": "news", | ||
"news": { | ||
"articles": [ | ||
{ | ||
"title": "%s", | ||
"description": "%s", | ||
"url": "%s", | ||
"picurl": "%s" | ||
}] | ||
} | ||
}`) | ||
url := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s", w.Key) | ||
resp, err := resty.New().R().SetBody(jsonStr).SetHeader("Content-Type", "application/json").Post(url) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode() >= 400 { | ||
return fmt.Errorf("请求失败 %s, %s", resp.Status(), string(resp.Body())) | ||
} | ||
|
||
} |