Skip to content

Commit

Permalink
增加了第三方通知
Browse files Browse the repository at this point in the history
  • Loading branch information
jicheng1014 committed Dec 21, 2022
1 parent becddab commit 978fdd7
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 40 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

一个基于 go 的 go-fir-cli 程序


## 说明


Expand All @@ -13,6 +14,12 @@ go-fir-cli 只实现了 fir-cli 的部分功能, 并无计划实现全部功能,

由于作者不善 golang, 所以大部分代码皆来自于 Copilot 和 chatGPT 生成, 作者仅在此基础上做了一些修改与调试, 以便于使用. 如果您在使用中发现任何问题, 欢迎提 issue 或者 pr.

## 更新说明

- 0.0.3 支持了 企业微信、飞书和钉钉的通知机器人
- 0.0.2 支持了生成二维码文件, 支持精确到版本的下载地址
- 0.0.1 支持了上传文件

## 安装

下载自己对应的系统的文件, 赋值可执行. 若想在全局使用,放到 path 里即可.
Expand Down Expand Up @@ -64,6 +71,8 @@ go-fir-cli 只实现了 fir-cli 的部分功能, 并无计划实现全部功能,
```




### 自行编译

下载好代码 安装好依赖即可运行 go build
Expand Down
2 changes: 1 addition & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package constants

const VERSION = "0.0.2"
const VERSION = "0.0.3"
const USER_AGENT = "go-fir-" + VERSION
78 changes: 76 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func initCli() {

app.Commands = []cli.Command{
initLogin(),

testWebhook(),
uploadFile(),
cli.Command{
Name: "version",
Expand All @@ -42,6 +42,14 @@ func initCli() {
return nil
},
},
cli.Command{
Name: "upgrade",
Usage: "如何升级 go_fir_cli",
Action: func(c *cli.Context) error {
fmt.Println("请访问 https://github.com/PGYER/go-fir-cli/releases 下载对应版本, 并替换原有的 go_fir_cli 文件")
return nil
},
},
}
app.Run(os.Args)
}
Expand Down Expand Up @@ -89,7 +97,10 @@ func testWebhook() cli.Command {
Key: token,
SecretToken: secret,
}
notifier.Notify("测试消息")
err := notifier.Notify("测试消息")
if err != nil {
fmt.Println(err)
}

return nil
},
Expand Down Expand Up @@ -117,6 +128,42 @@ func uploadFile() cli.Command {
Name: "qrcode, Q",
Usage: "输出二维码文件 qrcode.png, 用于下载, 默认为 false",
},

cli.StringFlag{
Name: "dingtalkToken, dt",
Usage: "dingtalk 的机器人的 token, 用于发送通知",
},
cli.StringFlag{
Name: "dingtalkSecret, ds",
Usage: "dingtalk 的机器人的 secret, 用于发送通知时的校验",
},
cli.StringFlag{
Name: "dingtalkCustomMsg, dcm",
Usage: "dingtalk 的机器人的自定义消息, 用于发送通知增加关键字",
},

cli.StringFlag{
Name: "larkToken, lt",
Usage: "飞书的机器人的 token (url hook 后面那段), 用于发送通知",
},
cli.StringFlag{
Name: "larkSecret, ls",
Usage: "飞书的机器人的 secret, 用于发送通知时的校验",
},
cli.StringFlag{
Name: "larkCustomMsg, lcm",
Usage: "飞书的机器人的自定义消息, 用于发送通知增加关键字",
},

cli.StringFlag{
Name: "wecomToken, wt",
Usage: "企业微信的机器人的 token, 用于发送通知",
},

cli.StringFlag{
Name: "wecomCustomMsg, wcm",
Usage: "企业微信的机器人的自定义消息, 用于发送通知增加关键字",
},
},
Action: func(c *cli.Context) error {

Expand Down Expand Up @@ -168,6 +215,33 @@ func uploadFile() cli.Command {
qrcode.WriteFile(url, qrcode.Medium, 256, "qr.png")
}

if c.String("dingtalkToken") != "" {
notifier := &notifiers.DingTalkNotifier{
Key: c.String("dingtalkToken"),
SecretToken: c.String("dingtalkSecret"),
}

json := notifier.BuildAppPubishedMessage(api.ApiAppInfo, c.String("dingtalkCustomMsg"), url)
notifier.Notify(json)
}

if c.String("larkToken") != "" {
notifier := &notifiers.LarkNotifier{
Key: c.String("larkToken"),
SecretToken: c.String("larkSecret"),
}

json := notifier.BuildAppPubishedMessage(api.ApiAppInfo, c.String("larkCustomMsg"), url)
notifier.Notify(json)
}

if c.String("wecomToken") != "" {
notifier := &notifiers.WeComNotifier{
Key: c.String("wecomToken"),
}
json := notifier.BuildAppPubishedMessage(api.ApiAppInfo, c.String("wecomCustomMsg"), url)
notifier.Notify(json)
}
return nil
},
}
Expand Down
25 changes: 16 additions & 9 deletions notifiers/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"crypto/sha256"
"encoding/base64"
"fmt"
"net/url"
"time"

"betaqr.com/go_fir_cli/api"
"github.com/go-resty/resty/v2"
)

Expand All @@ -17,8 +19,19 @@ type DingTalkNotifier struct {
SecretToken string
}

func (d *DingTalkNotifier) Notify(message string) error {
var jsonStr string
func (d *DingTalkNotifier) BuildAppPubishedMessage(apiAppInfo *api.ApiAppInfo, CustomMsg, DownloadUrl string) string {
jsonStr := fmt.Sprintf(`{
"msgtype": 'markdown',
"markdown": {
"title": "%s uploaded",
"text": "#### %s(%s)\n\n>uploaded at #{%s}\n\nurl: %s\n\n%s\n\n ![](https://api.appmeta.cn/welcome/qrcode?url=%s)"
}
}`, apiAppInfo.Name, apiAppInfo.Name, apiAppInfo.Type, time.Now(), DownloadUrl, CustomMsg, url.PathEscape(DownloadUrl))

return jsonStr
}

func (d *DingTalkNotifier) Notify(jsonStr string) error {

url := fmt.Sprintf("https://oapi.dingtalk.com/robot/send?access_token=%s", d.Key)

Expand All @@ -31,13 +44,6 @@ func (d *DingTalkNotifier) Notify(message string) error {
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 {
Expand All @@ -46,6 +52,7 @@ func (d *DingTalkNotifier) Notify(message string) error {
if resp.StatusCode() >= 400 {
return fmt.Errorf("请求失败 %s, %s", resp.Status(), string(resp.Body()))
}
return nil

}

Expand Down
57 changes: 43 additions & 14 deletions notifiers/lark.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,54 @@ import (
"fmt"
"time"

"betaqr.com/go_fir_cli/api"
"github.com/go-resty/resty/v2"
)

type LarkNotifier struct {
key string
Key string
SecretToken string
}

// https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN

func (l *LarkNotifier) Notify(message string) error {
func (l *LarkNotifier) BuildAppPubishedMessage(apiAppInfo *api.ApiAppInfo, CustomMsg, DownloadUrl string) string {
partialJSON := fmt.Sprintf(`
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "%s uploaded",
"content": [
[
{
"tag": "text",
"text": "%s(%s) uploaded at %s"
}
],
[
{
"tag": "a",
"text": "Download",
"href": "%s"
}
],
[
{
"tag": "text",
"text": "%s"
}
]
]
}
}
}`, apiAppInfo.Name, apiAppInfo.Name, apiAppInfo.Type, time.Now(), DownloadUrl, CustomMsg)

var jsonStr string
return partialJSON
}

func (l *LarkNotifier) Notify(partialJsonStr string) error {
var jsonStr string
if l.SecretToken != "" {
timestamp := time.Now().Unix()
signature, err := GenSign(l.SecretToken, timestamp)
Expand All @@ -31,22 +65,16 @@ func (l *LarkNotifier) Notify(message string) error {
jsonStr = fmt.Sprintf(`{
"timestamp": %v,
"sign": "%s",
"msg_type": "text",
"content": {
"text": "%s"
}
}`, timestamp, signature, message)
%s
}`, timestamp, signature, partialJsonStr)

} else {
jsonStr = fmt.Sprintf(`{
"msg_type": "text",
"content": {
"text": "Update reminder"
}
} `)
%s
} `, partialJsonStr)
}

url := fmt.Sprintf("https://open.feishu.cn/open-apis/bot/v2/hook/%s", l.key)
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 {
Expand All @@ -55,6 +83,7 @@ func (l *LarkNotifier) Notify(message string) error {
if resp.StatusCode() >= 400 {
return fmt.Errorf("请求失败 %s, %s", resp.Status(), string(resp.Body()))
}
return nil
}

func GenSign(secret string, timestamp int64) (string, error) {
Expand Down
26 changes: 12 additions & 14 deletions notifiers/wecom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,35 @@ package notifiers

import (
"fmt"
"net/url"
"time"

"betaqr.com/go_fir_cli/api"
"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 {

func (w *WeComNotifier) BuildAppPubishedMessage(apiAppInfo *api.ApiAppInfo, CustomMsg, DownloadUrl string) string {
jsonStr := fmt.Sprintf(`{
"msgtype": "news",
"news": {
"articles": [
{
"title": "%s",
"description": "%s",
"description": "%s (%s) uploaded at %s",
"url": "%s",
"picurl": "%s"
"picurl": "https://api.appmeta.cn/welcome/qrcode?url=%s"
}]
}
}`, apiAppInfo.Name, apiAppInfo.Name, apiAppInfo.Type, time.Now(), DownloadUrl, url.PathEscape(DownloadUrl))
return jsonStr
}

func (w *WeComNotifier) Notify(jsonStr string) error {

}`)
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)

Expand All @@ -43,5 +41,5 @@ func (w *WeComNotifier) Notify(message string) error {
if resp.StatusCode() >= 400 {
return fmt.Errorf("请求失败 %s, %s", resp.Status(), string(resp.Body()))
}

return nil
}

0 comments on commit 978fdd7

Please sign in to comment.