-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRobotApi.go
97 lines (78 loc) · 1.73 KB
/
RobotApi.go
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
package feishuapi
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/sirupsen/logrus"
)
type RobotInfo struct {
Name string
OpenId string
}
func NewRobotInfo(data map[string]any) *RobotInfo {
return &RobotInfo{
Name: data["app_name"].(string),
OpenId: data["open_id"].(string),
}
}
func (c AppClient) RobotGetInfo() *RobotInfo {
u := "https://open.feishu.cn/open-apis/bot/v3/info"
header := make(map[string]string)
header["Authorization"] = "Bearer " + c._tenant_access_token
var req *http.Request
Url, err := url.Parse(u)
if err != nil {
logrus.WithFields(logrus.Fields{
"url": u,
"err": err,
}).Error("url parse error")
return nil
}
urlPath := Url.String()
req, err = http.NewRequest("GET", urlPath, nil)
if err != nil {
logrus.WithFields(logrus.Fields{
"url": u,
"err": err,
}).Error("Request create error")
return nil
}
for k, v := range header {
req.Header.Add(k, v)
}
cli := &http.Client{
Timeout: time.Second * 15,
}
resp, err := cli.Do(req)
if err != nil {
logrus.WithFields(logrus.Fields{
"url": u,
"err": err,
}).Error("client do error")
return nil
}
defer resp.Body.Close()
if !responseOk(resp) {
respBody, _ := ioutil.ReadAll(resp.Body)
logrus.WithFields(logrus.Fields{
"url": u,
"headers": header,
"error": "response incorrect",
"response": string(respBody),
}).Error("response status error")
return nil
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
logrus.WithFields(logrus.Fields{
"url": u,
"err": err,
}).Error("read response body error")
return nil
}
var result map[string]any
json.Unmarshal(respBody, &result)
return NewRobotInfo(result["bot"].(map[string]any))
}