-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathfeishuapp.go
More file actions
224 lines (214 loc) · 6.22 KB
/
feishuapp.go
File metadata and controls
224 lines (214 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package controllers
import (
"PrometheusAlert/models"
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type FSAPPConf struct {
WideScreenMode bool `json:"wide_screen_mode"`
EnableForward bool `json:"enable_forward"`
}
type FSAPPTe struct {
Content string `json:"content"`
Tag string `json:"tag"`
}
type FSAPPElement struct {
Tag string `json:"tag"`
Text Te `json:"text"`
Content string `json:"content"`
FSAPPElements []FSAPPElement `json:"elements"`
}
type FSAPPTitles struct {
Content string `json:"content"`
Tag string `json:"tag"`
}
type FSAPPHeaders struct {
FSAPPTitle FSAPPTitles `json:"title"`
Template string `json:"template"`
}
type FSAPPCards struct {
FSAPPConfig FSAPPConf `json:"config"`
FSAPPElements []FSAPPElement `json:"elements"`
FSAPPHeader FSAPPHeaders `json:"header"`
}
type FSContentAPP struct {
MsgType string `json:"msg_type"`
ReceiveId string `json:"receive_id"` //用户传入的ID,可以是 open_id、user_id、union_id、email、chat_id
FSAPPContent string `json:"content"`
}
func GetAccessToken(logsign string) (string, error) {
// https://open.feishu.cn/open-apis/message/v4/batch_send/ 批量发送消息 tenant_access_token
// 先获取 tenant_access_token
u := TenantAccessMeg{
AppId: beego.AppConfig.String("FEISHU_APPID"),
AppSecret: beego.AppConfig.String("FEISHU_APPSECRET"),
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(u)
var tr *http.Transport
if proxyUrl := beego.AppConfig.String("proxy"); proxyUrl != "" {
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(proxyUrl)
}
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: proxy,
}
} else {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
client := &http.Client{Transport: tr}
//res, err := client.Post("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", "application/json; charset=utf-8", b)
res, err := http.NewRequest("POST", "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", b)
if err != nil {
logs.Error(logsign, "[feishuapp]", err.Error())
return "", err
}
res.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Do(res)
defer res.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
logs.Error(logsign, "[feishuapp]", err.Error())
return "", err
}
resp_json := TenantAccessResp{}
json.Unmarshal(result, &resp_json)
if resp_json.Msg != "ok" {
logs.Error(logsign, "[feishuapp]", resp_json.Msg)
return "", errors.New(resp_json.Msg)
}
logs.Info(logsign, "[feishuapp]", string(result))
return resp_json.TenantAccessToken, nil
}
func PostToFeiShuApp(title, text, receiveIds, logsign string) string {
open := beego.AppConfig.String("open-feishuapp")
if open != "1" {
logs.Info(logsign, "[feishuapp]", "飞书APP接口未配置未开启状态,请先配置open-feishuapp为1")
return "飞书APP接口未配置未开启状态,请先配置open-feishuapp为1"
}
var color string
if strings.Count(text, "resolved") > 0 && strings.Count(text, "firing") > 0 {
color = "orange"
} else if strings.Count(text, "resolved") > 0 {
color = "green"
} else {
color = "red"
}
token, err := GetAccessToken(logsign)
if err != nil {
logs.Error(logsign, "[feishuapp]", err.Error())
return err.Error()
}
SendContent := text
var result []byte
if receiveIds != "" {
ReceiveIds := strings.Split(receiveIds, ",")
fsAppContent :=
&FSAPPCards{
FSAPPConfig: FSAPPConf{
WideScreenMode: true,
EnableForward: true,
},
FSAPPHeader: FSAPPHeaders{
FSAPPTitle: FSAPPTitles{
Content: title,
Tag: "plain_text",
},
Template: color,
},
FSAPPElements: []FSAPPElement{
FSAPPElement{
Tag: "div",
Text: Te{
Content: SendContent,
Tag: "lark_md",
},
},
{
Tag: "hr",
},
{
Tag: "note",
FSAPPElements: []FSAPPElement{
{
Content: title,
Tag: "lark_md",
},
},
},
},
}
contentByte, _ := json.Marshal(fsAppContent)
fmt.Println("fsAppContent: " + string(contentByte))
for _, ReceiveId := range ReceiveIds {
u := FSContentAPP{
MsgType: "interactive",
ReceiveId: ReceiveId,
FSAPPContent: string(contentByte),
}
var ReceiveType string
if strings.Contains(ReceiveId, "ou_") {
ReceiveType = "open_id"
} else if strings.Contains(ReceiveId, "on_") {
ReceiveType = "union_id"
} else if strings.Contains(ReceiveId, "oc_") {
ReceiveType = "chat_id"
} else if strings.Contains(ReceiveId, "@") {
ReceiveType = "email"
} else {
ReceiveType = "user_id"
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(u)
logs.Info(logsign, "[feishuapp]", b)
var tr *http.Transport
if proxyUrl := beego.AppConfig.String("proxy"); proxyUrl != "" {
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(proxyUrl)
}
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: proxy,
}
} else {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
client := &http.Client{Transport: tr}
FSUrl := fmt.Sprintf("https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=%s", ReceiveType)
req, err := http.NewRequest("POST", FSUrl, b)
if err != nil {
logs.Error(logsign, "[feishuapp]", title+": "+err.Error())
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", "Bearer "+token)
resp, err := client.Do(req)
if err != nil {
logs.Error(logsign, "[feishuapp]", err.Error())
}
defer resp.Body.Close()
result, err = ioutil.ReadAll(resp.Body)
if err != nil {
logs.Error(logsign, "[feishuapp]", title+": "+err.Error())
}
models.AlertToCounter.WithLabelValues("feishuapp").Add(1)
ChartsJson.Feishu += 1
logs.Info(logsign, "[feishuapp]", title+": "+string(result))
//return string(result)
}
}
return string(result)
}