Skip to content

Commit

Permalink
add dingtalk lark and wecom chatbots
Browse files Browse the repository at this point in the history
  • Loading branch information
jicheng1014 committed Dec 20, 2022
1 parent aa75b2b commit becddab
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ icon.png
qr.png
go_fir_cli
go_fir_cli.exe
test.go
*.apk
*.ipa
.vscode/*
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"betaqr.com/go_fir_cli/api"
"betaqr.com/go_fir_cli/constants"
"betaqr.com/go_fir_cli/notifiers"
"github.com/skip2/go-qrcode"
"gopkg.in/urfave/cli.v1"
)
Expand Down Expand Up @@ -68,6 +69,33 @@ func initLogin() cli.Command {
}
}

func testWebhook() cli.Command {
return cli.Command{
Name: "test",
Usage: "测试 webhook",
Flags: []cli.Flag{
cli.StringFlag{
Name: "token, t",
},
cli.StringFlag{
Name: "secret, s",
},
},
Action: func(c *cli.Context) error {
token := c.String("token")
secret := c.String("secret")

notifier := &notifiers.DingTalkNotifier{
Key: token,
SecretToken: secret,
}
notifier.Notify("测试消息")

return nil
},
}
}

func uploadFile() cli.Command {
return cli.Command{
Name: "upload",
Expand Down
58 changes: 58 additions & 0 deletions notifiers/dingtalk.go
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&timestamp=%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
}
71 changes: 71 additions & 0 deletions notifiers/lark.go
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
}
47 changes: 47 additions & 0 deletions notifiers/wecom.go
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()))
}

}

0 comments on commit becddab

Please sign in to comment.