-
Notifications
You must be signed in to change notification settings - Fork 74
/
im.go
99 lines (92 loc) · 2.58 KB
/
im.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
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"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/tools"
"github.com/larksuite/oapi-sdk-go/sample/configs"
im "github.com/larksuite/oapi-sdk-go/service/im/v1"
)
// for redis store and logrus
// configs.TestConfigWithLogrusAndRedisStore(core.DomainFeiShu)
// configs.TestConfig("https://open.feishu.cn")
var imService = im.NewService(configs.TestConfig(core.DomainFeiShu))
func main() {
testMessageCreate()
//testFileCreate()
//testFileRead()
}
func testMessageCreate() {
coreCtx := core.WrapContext(context.Background())
reqCall := imService.Messages.Create(coreCtx, &im.MessageCreateReqBody{
// ReceiveId: "b1g6b445",
ReceiveId: "ou_c245b0a7dff2725cfa2fb104f8b48b9d",
Content: "{\"text\":\"<at user_id=\\\"ou_a11d2bcc7d852afbcaf37e5b3ad01f7e\\\">Tom</at> test content\"}",
MsgType: "text",
})
reqCall.SetReceiveIdType("open_id")
message, err := reqCall.Do()
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(message))
}
func testFileRead() {
coreCtx := core.WrapContext(context.Background())
reqCall := imService.Files.Get(coreCtx)
buf := &bytes.Buffer{}
reqCall.SetResponseStream(buf)
reqCall.SetFileKey("file_ec24f8ad-89ea-4bb5-a7e4-c5db35d2925g")
_, err := reqCall.Do()
fmt.Println(coreCtx.GetRequestID())
fmt.Println(coreCtx.GetHTTPStatusCode())
fmt.Println(coreCtx.GetHeader())
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.pdf", buf.Bytes(), os.ModePerm)
if err != nil {
fmt.Println(err)
return
}
}
func testFileCreate() {
coreCtx := core.WrapContext(context.Background())
reqCall := imService.Files.Create(coreCtx)
f, err := os.Open("test.pdf")
if err != nil {
fmt.Println(err)
return
}
file := request.NewFile().SetContentStream(f)
// request.NewFile().SetContent([]byte)
reqCall.SetFile(file)
reqCall.SetFileName("test-测试.pdf")
reqCall.SetFileType("pdf")
message, err := reqCall.Do()
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(message))
}