-
Notifications
You must be signed in to change notification settings - Fork 85
/
api.go
157 lines (149 loc) · 4.55 KB
/
api.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
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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/larksuite/oapi-sdk-go/api"
"github.com/larksuite/oapi-sdk-go/api/core/request"
"github.com/larksuite/oapi-sdk-go/api/core/response"
"github.com/larksuite/oapi-sdk-go/core"
"github.com/larksuite/oapi-sdk-go/core/test"
"github.com/larksuite/oapi-sdk-go/core/tools"
"io/ioutil"
"os"
)
var conf = test.GetInternalConf("online")
func main() {
//testSendMessage()
testSendCardMessage()
//testUploadFile()
//testDownloadFile()
}
// send card message
func testSendCardMessage() {
coreCtx := core.WrapContext(context.Background())
cardContent := "{\"config\":{\"wide_screen_mode\":true},\"i18n_elements\":{\"zh_cn\":[{\"tag\":\"div\",\"text\":{\"tag\":\"lark_md\",\"content\":\"[飞书](https://www.feishu.cn)整合即时沟通、日历、音视频会议、云文档、云盘、工作台等功能于一体,成就组织和个人,更高效、更愉悦。\"}},{\"tag\":\"action\",\"actions\":[{\"tag\":\"button\",\"text\":{\"tag\":\"plain_text\",\"content\":\"主按钮\"},\"type\":\"primary\",\"value\":{\"key\":\"primary\"}},{\"tag\":\"button\",\"text\":{\"tag\":\"plain_text\",\"content\":\"次按钮\"},\"type\":\"default\",\"value\":{\"key\":\"default\"}}]}]}}"
card := map[string]interface{}{}
err := json.Unmarshal([]byte(cardContent), &card)
if err != nil {
panic(err)
}
body := map[string]interface{}{
"open_id": "[open_id]",
"msg_type": "interactive",
"card": card,
}
ret := make(map[string]interface{})
req := request.NewRequestWithNative("message/v4/send", "POST",
request.AccessTokenTypeTenant, body, &ret,
//应用市场应用 request.SetTenantKey("TenantKey"),
)
err = api.Send(coreCtx, conf, req)
fmt.Println(coreCtx.GetRequestID())
fmt.Println(coreCtx.GetHTTPStatusCode())
if err != nil {
fmt.Println(tools.Prettify(err))
e := err.(*response.Error)
fmt.Println(e.Code)
fmt.Println(e.Msg)
return
}
fmt.Println(tools.Prettify(ret))
}
// send message
func testSendMessage() {
coreCtx := core.WrapContext(context.Background())
body := map[string]interface{}{
"open_id": "[open_id]",
"msg_type": "text",
"content": map[string]interface{}{
"text": "test",
},
}
ret := make(map[string]interface{})
req := request.NewRequestWithNative("message/v4/send", "POST",
request.AccessTokenTypeTenant, body, &ret,
//应用市场应用 request.SetTenantKey("TenantKey"),
)
err := api.Send(coreCtx, conf, req)
fmt.Println(coreCtx.GetRequestID())
fmt.Println(coreCtx.GetHTTPStatusCode())
if err != nil {
fmt.Println(tools.Prettify(err))
e := err.(*response.Error)
fmt.Println(e.Code)
fmt.Println(e.Msg)
return
}
fmt.Println(tools.Prettify(ret))
}
type UploadImage struct {
ImageKey string `json:"image_key"`
}
// upload image
func testUploadFile() {
coreCtx := core.WrapContext(context.Background())
// coreCtx.Set(constants.HTTPHeaderKeyRequestID, "2020122212081301001702714534518-xxxxx")
var formData = request.NewFormData()
formData.AddParam("image_type", "message")
bs, err := ioutil.ReadFile("test.png")
if err != nil {
fmt.Println(err)
return
}
formData.AddFile("image", request.NewFile().SetContent(bs))
/*
// stream upload, file implement io.Reader
file, err := os.Open("test.png")
if err != nil {
fmt.Println(err)
return
}
formData.AddFile("image", request.NewFile().SetContentStream(file))
*/
ret := &UploadImage{}
err = api.Send(coreCtx, conf, request.NewRequestWithNative("image/v4/put", "POST",
request.AccessTokenTypeTenant, formData, ret))
fmt.Println(coreCtx.GetRequestID())
fmt.Println(coreCtx.GetHTTPStatusCode())
if err != nil {
fmt.Println(tools.Prettify(err))
e := err.(*response.Error)
fmt.Println(e.Code)
fmt.Println(e.Msg)
return
}
fmt.Println(tools.Prettify(ret))
}
// download image
func testDownloadFile() {
coreCtx := core.WrapContext(context.Background())
ret := &bytes.Buffer{}
/*
// stream download: ret implement io.Writer
ret, err := os.Create("[file path]")
if err != nil {
fmt.Println(err)
return
}
*/
req := request.NewRequestWithNative("image/v4/get", "GET",
request.AccessTokenTypeTenant, nil, ret,
request.SetQueryParams(map[string]interface{}{"image_key": "[image key]"}), request.SetResponseStream())
err := api.Send(coreCtx, conf, req)
fmt.Println(coreCtx.GetRequestID())
fmt.Println(coreCtx.GetHTTPStatusCode())
if err != nil {
fmt.Println(tools.Prettify(err))
e := err.(*response.Error)
fmt.Println(e.Code)
fmt.Println(e.Msg)
return
}
err = ioutil.WriteFile("test_download.png", ret.Bytes(), os.ModePerm)
if err != nil {
fmt.Println(err)
return
}
}