Skip to content

Commit b618c5d

Browse files
author
Mingqiang Zhao
authored
Merge pull request #18 from larksuite/new_service
context GetHeader add ai service add im service
2 parents 6ac53bd + a898b8f commit b618c5d

File tree

35 files changed

+3871
-927
lines changed

35 files changed

+3871
-927
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ modules.txt
1010
*_test.go
1111
*.json
1212
*.sh
13+
test.go

README.zh.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,17 @@ readCloser, err := tools.DownloadFileToStream(ctx context.Context, url string)
518518

519519
---
520520

521-
|业务域|版本|路径|代码示例|
522-
|---|---|---|----|
523-
|[用户身份验证](https://open.feishu.cn/document/ukTMukTMukTM/uETOwYjLxkDM24SM5AjN)|v1|[service/authen](service/authen)|[sample/api/authen.go](sample/api/authen.go)|
524-
|[图片](https://open.feishu.cn/document/ukTMukTMukTM/uEDO04SM4QjLxgDN)|v4|[service/image](service/image)|[sample/api/image.go](sample/api/image.go)|
525-
|[通讯录](https://open.feishu.cn/document/ukTMukTMukTM/uETNz4SM1MjLxUzM/v3/introduction)|v3|[service/contact](service/contact)|[sample/api/contact.go](sample/api/contact.go)|
526-
|[日历](https://open.feishu.cn/document/ukTMukTMukTM/uETM3YjLxEzN24SMxcjN)|v4|[service/calendar](service/calendar)|[sample/api/calendar.go](sample/api/calendar.go)|
527-
|[视频会议](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/videoconference/guide)|v1|[service/vc](service/vc)|[sample/api/vc.go](sample/api/vc.go)|
528-
|[云空间文件](https://open.feishu.cn/document/ukTMukTMukTM/uUjM5YjL1ITO24SNykjN)|v1|[service/drive](service/drive)|[sample/api/drive.go](sample/api/drive.go)|
521+
|业务域|版本|路径|API示例|Event示例|
522+
|---|---|---|----|---|
523+
|[用户身份验证](https://open.feishu.cn/document/ukTMukTMukTM/uETOwYjLxkDM24SM5AjN)|v1|[service/authen](service/authen)|[sample/api/authen.go](sample/api/authen.go)||
524+
|[通讯录](https://open.feishu.cn/document/ukTMukTMukTM/uETNz4SM1MjLxUzM/v3/introduction)|v3|[service/contact](service/contact)|[sample/api/contact.go](sample/api/contact.go)|[sample/event/contact.go](sample/event/contact.go)|
525+
|[日历](https://open.feishu.cn/document/ukTMukTMukTM/uETM3YjLxEzN24SMxcjN)|v4|[service/calendar](service/calendar)|[sample/api/calendar.go](sample/api/calendar.go)||
526+
|[视频会议](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/videoconference/guide)|v1|[service/vc](service/vc)|[sample/api/vc.go](sample/api/vc.go)||
527+
|[云空间文件](https://open.feishu.cn/document/ukTMukTMukTM/uUjM5YjL1ITO24SNykjN)|v1|[service/drive](service/drive)|[sample/api/drive.go](sample/api/drive.go)||
528+
|[消息&群组](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/create)|v1|[service/im](service/im)|[sample/api/im.go](sample/api/im.go)|[sample/event/im.go](sample/event/im.go)|
529+
|[AI能力-光学字符识别(OCR)](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/ai/optical_char_recognition-v1/image/basic_recognize)|v1|[service/optical_char_recognition](service/optical_char_recognition)|[sample/api/optical_char_recognition.go](sample/api/optical_char_recognition.go)||
530+
|[AI能力-语音识别(ASR)](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/ai/speech_to_text-v1/speech/stream_recognize)|v1|[service/speech_to_text](service/speech_to_text)|[sample/api/speech_to_text.go](sample/api/speech_to_text.go)||
531+
|[AI能力-机器翻译(MT)](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/ai/translation-v1/text/translate)|v1|[service/translation](service/translation)|[sample/api/translation.go](sample/api/translation.go)||
529532

530533
## License
531534

api/core/handlers/handlers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/larksuite/oapi-sdk-go/core"
1414
"github.com/larksuite/oapi-sdk-go/core/config"
1515
coreconst "github.com/larksuite/oapi-sdk-go/core/constants"
16+
"github.com/larksuite/oapi-sdk-go/core/model"
1617
"io"
1718
"io/ioutil"
1819
"mime/multipart"
@@ -89,7 +90,7 @@ func (hs *Handlers) send(ctx *core.Context, req *request.Request) {
8990
req.Err = err
9091
return
9192
}
92-
ctx.SetRequestID(resp.Header.Get(coreconst.HTTPHeaderKeyLogID), resp.Header.Get(coreconst.HTTPHeaderKeyRequestID))
93+
ctx.Set(coreconst.HTTPHeader, model.NewOapiHeader(resp.Header))
9394
ctx.Set(coreconst.HTTPKeyStatusCode, resp.StatusCode)
9495
req.HTTPResponse = resp
9596
defer hs.retry(ctx, req)

card/handlers/handlers.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,14 @@ type Handlers struct {
3535

3636
func initFunc(ctx *core.Context, httpCard *model.HTTPCard) {
3737
request := httpCard.Request
38+
ctx.Set(constants.HTTPHeader, request.Header)
3839
header := &model.Header{
3940
Timestamp: request.Header.GetFirstValue(model.LarkRequestTimestamp),
4041
Nonce: request.Header.GetFirstValue(model.LarkRequestRequestNonce),
4142
Signature: request.Header.GetFirstValue(model.LarkSignature),
4243
RefreshToken: request.Header.GetFirstValue(model.LarkRefreshToken),
4344
}
4445
httpCard.Header = header
45-
ctx.Set(model.LarkRequestTimestamp, header.Timestamp)
46-
ctx.Set(model.LarkRequestRequestNonce, header.Nonce)
47-
ctx.Set(model.LarkSignature, header.Signature)
48-
ctx.Set(model.LarkRefreshToken, header.RefreshToken)
49-
ctx.SetRequestID(request.Header.GetFirstValue(constants.HTTPHeaderKeyLogID), request.Header.GetFirstValue(constants.HTTPHeaderKeyRequestID))
5046
config.ByCtx(ctx).GetLogger().Debug(ctx, fmt.Sprintf("[init] card: %s", request.Body))
5147
httpCard.Input = []byte(request.Body)
5248
}

core/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const ContentTypeJson = "application/json"
55
const DefaultContentType = ContentTypeJson + "; charset=utf-8"
66

77
const (
8+
HTTPHeader = "HTTP-Header"
89
HTTPHeaderKeyRequestID = "X-Request-Id"
910
HTTPHeaderKeyLogID = "X-Log-Id"
1011
HTTPKeyStatusCode = "http_status_code"

core/context.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"context"
55
"github.com/larksuite/oapi-sdk-go/core/constants"
6+
"github.com/larksuite/oapi-sdk-go/core/model"
67
"sync"
78
"time"
89
)
@@ -52,11 +53,20 @@ func (c *Context) Get(key string) (value interface{}, exists bool) {
5253
return
5354
}
5455

56+
func (c *Context) GetHeader() *model.OapiHeader {
57+
if header, ok := c.Get(constants.HTTPHeader); ok {
58+
return header.(*model.OapiHeader)
59+
}
60+
return model.NewOapiHeader(map[string][]string{})
61+
}
62+
5563
func (c *Context) GetRequestID() string {
56-
if requestID, ok := c.Get(constants.HTTPHeaderKeyRequestID); ok {
57-
return requestID.(string)
64+
header := c.GetHeader()
65+
logID := header.GetFirstValue(constants.HTTPHeaderKeyLogID)
66+
if logID != "" {
67+
return logID
5868
}
59-
return ""
69+
return header.GetFirstValue(constants.HTTPHeaderKeyRequestID)
6070
}
6171

6272
func (c *Context) GetHTTPStatusCode() int {

event/core/handlers/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Handle(ctx *core.Context, httpEvent *model.HTTPEvent) {
4242

4343
func unmarshalFunc(ctx *core.Context, httpEvent *model.HTTPEvent) {
4444
request := httpEvent.Request
45-
ctx.SetRequestID(request.Header.GetFirstValue(constants.HTTPHeaderKeyLogID), request.Header.GetFirstValue(constants.HTTPHeaderKeyRequestID))
45+
ctx.Set(constants.HTTPHeader, request.Header)
4646
conf := config.ByCtx(ctx)
4747
conf.GetLogger().Debug(ctx, fmt.Sprintf("[unmarshal] event: %s", request.Body))
4848
body := []byte(request.Body)

sample/api/contact.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ import (
1717
var contactService = contact.NewService(configs.TestConfig(constants.DomainFeiShu))
1818

1919
func main() {
20-
//testUserServiceList()
21-
testDepartmentServiceList()
20+
testUserServiceList()
21+
//testDepartmentServiceList()
2222
}
2323
func testUserServiceList() {
2424
coreCtx := core.WrapContext(context.Background())
2525
reqCall := contactService.Users.List(coreCtx)
2626
reqCall.SetDepartmentIdType("open_id")
2727
reqCall.SetPageSize(20)
2828
reqCall.SetDepartmentIdType("open_department_id")
29-
reqCall.SetDepartmentId("od_XXXXXXXXX")
29+
reqCall.SetDepartmentId("0")
3030
reqCall.SetUserIdType("open_id")
3131
result, err := reqCall.Do()
3232
fmt.Printf("request_id:%s", coreCtx.GetRequestID())

sample/api/im.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"github.com/larksuite/oapi-sdk-go/api/core/request"
8+
"github.com/larksuite/oapi-sdk-go/api/core/response"
9+
"github.com/larksuite/oapi-sdk-go/core"
10+
"github.com/larksuite/oapi-sdk-go/core/constants"
11+
"github.com/larksuite/oapi-sdk-go/core/tools"
12+
"github.com/larksuite/oapi-sdk-go/sample/configs"
13+
im "github.com/larksuite/oapi-sdk-go/service/im/v1"
14+
"io/ioutil"
15+
"os"
16+
)
17+
18+
// for redis store and logrus
19+
// configs.TestConfigWithLogrusAndRedisStore(constants.DomainFeiShu)
20+
// configs.TestConfig("https://open.feishu.cn")
21+
var imService = im.NewService(configs.TestConfig(constants.DomainFeiShu))
22+
23+
func main() {
24+
//testMessageCreate()
25+
//testFileCreate()
26+
testFileRead()
27+
}
28+
29+
func testMessageCreate() {
30+
coreCtx := core.WrapContext(context.Background())
31+
reqCall := imService.Messages.Create(coreCtx, &im.MessageCreateReqBody{
32+
ReceiveId: "ou_a11d2bcc7d852afbcaf37e5b3ad01f7e",
33+
Content: "{\"text\":\"<at user_id=\\\"ou_a11d2bcc7d852afbcaf37e5b3ad01f7e\\\">Tom</at> test content\"}",
34+
MsgType: "text",
35+
})
36+
reqCall.SetReceiveIdType("open_id")
37+
message, err := reqCall.Do()
38+
fmt.Println(coreCtx.GetRequestID())
39+
fmt.Println(coreCtx.GetHTTPStatusCode())
40+
if err != nil {
41+
fmt.Println(tools.Prettify(err))
42+
e := err.(*response.Error)
43+
fmt.Println(e.Code)
44+
fmt.Println(e.Msg)
45+
return
46+
}
47+
fmt.Println(tools.Prettify(message))
48+
}
49+
50+
func testFileRead() {
51+
coreCtx := core.WrapContext(context.Background())
52+
reqCall := imService.Files.Get(coreCtx)
53+
buf := &bytes.Buffer{}
54+
reqCall.SetResponseStream(buf)
55+
reqCall.SetFileKey("file_ec24f8ad-89ea-4bb5-a7e4-c5db35d2925g")
56+
_, err := reqCall.Do()
57+
fmt.Println(coreCtx.GetRequestID())
58+
fmt.Println(coreCtx.GetHTTPStatusCode())
59+
fmt.Println(coreCtx.GetHeader())
60+
if err != nil {
61+
fmt.Println(tools.Prettify(err))
62+
e := err.(*response.Error)
63+
fmt.Println(e.Code)
64+
fmt.Println(e.Msg)
65+
return
66+
}
67+
err = ioutil.WriteFile("test_download.pdf", buf.Bytes(), os.ModePerm)
68+
if err != nil {
69+
fmt.Println(err)
70+
return
71+
}
72+
}
73+
74+
func testFileCreate() {
75+
coreCtx := core.WrapContext(context.Background())
76+
reqCall := imService.Files.Create(coreCtx)
77+
f, err := os.Open("test.pdf")
78+
if err != nil {
79+
fmt.Println(err)
80+
return
81+
}
82+
file := request.NewFile().SetContentStream(f)
83+
// request.NewFile().SetContent([]byte)
84+
reqCall.SetFile(file)
85+
reqCall.SetFileName("test-测试.pdf")
86+
reqCall.SetFileType("pdf")
87+
message, err := reqCall.Do()
88+
fmt.Println(coreCtx.GetRequestID())
89+
fmt.Println(coreCtx.GetHTTPStatusCode())
90+
if err != nil {
91+
fmt.Println(tools.Prettify(err))
92+
e := err.(*response.Error)
93+
fmt.Println(e.Code)
94+
fmt.Println(e.Msg)
95+
return
96+
}
97+
fmt.Println(tools.Prettify(message))
98+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/larksuite/oapi-sdk-go/api/core/response"
7+
"github.com/larksuite/oapi-sdk-go/core"
8+
"github.com/larksuite/oapi-sdk-go/core/constants"
9+
"github.com/larksuite/oapi-sdk-go/core/tools"
10+
"github.com/larksuite/oapi-sdk-go/sample/configs"
11+
optical_char_recognition "github.com/larksuite/oapi-sdk-go/service/optical_char_recognition/v1"
12+
)
13+
14+
// for redis store and logrus
15+
// configs.TestConfigWithLogrusAndRedisStore(constants.DomainFeiShu)
16+
// configs.TestConfig("https://open.feishu.cn")
17+
var opticalCharRecognitionService = optical_char_recognition.NewService(configs.TestConfig(constants.DomainFeiShu))
18+
19+
func main() {
20+
testImageBasicRecognize()
21+
}
22+
23+
func testImageBasicRecognize() {
24+
coreCtx := core.WrapContext(context.Background())
25+
reqCall := opticalCharRecognitionService.Images.BasicRecognize(coreCtx, &optical_char_recognition.ImageBasicRecognizeReqBody{
26+
Image: "base64 image",
27+
})
28+
result, err := reqCall.Do()
29+
fmt.Printf("request_id:%s\n", coreCtx.GetRequestID())
30+
fmt.Printf("http status code:%d", coreCtx.GetHTTPStatusCode())
31+
if err != nil {
32+
e := err.(*response.Error)
33+
fmt.Printf(tools.Prettify(e))
34+
return
35+
}
36+
fmt.Printf("reault:%s", tools.Prettify(result))
37+
}

sample/api/speech_to_text.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/larksuite/oapi-sdk-go/api/core/response"
7+
"github.com/larksuite/oapi-sdk-go/core"
8+
"github.com/larksuite/oapi-sdk-go/core/constants"
9+
"github.com/larksuite/oapi-sdk-go/core/tools"
10+
"github.com/larksuite/oapi-sdk-go/sample/configs"
11+
speech_to_text "github.com/larksuite/oapi-sdk-go/service/speech_to_text/v1"
12+
)
13+
14+
// for redis store and logrus
15+
// configs.TestConfigWithLogrusAndRedisStore(constants.DomainFeiShu)
16+
// configs.TestConfig("https://open.feishu.cn")
17+
var speechToTextService = speech_to_text.NewService(configs.TestConfig(constants.DomainFeiShu))
18+
19+
func main() {
20+
testSpeechFileRecognize()
21+
}
22+
23+
func testSpeechFileRecognize() {
24+
coreCtx := core.WrapContext(context.Background())
25+
reqCall := speechToTextService.Speechs.FileRecognize(coreCtx, &speech_to_text.SpeechFileRecognizeReqBody{
26+
Speech: &speech_to_text.Speech{
27+
Speech: "base64 后的音频内容",
28+
},
29+
Config: &speech_to_text.FileConfig{
30+
FileId: "qwe12dd34567890w",
31+
Format: "pcm",
32+
EngineType: "16k_auto",
33+
},
34+
})
35+
result, err := reqCall.Do()
36+
fmt.Printf("header:%s\n", coreCtx.GetHeader())
37+
fmt.Printf("request_id:%s\n", coreCtx.GetRequestID())
38+
fmt.Printf("http status code:%d", coreCtx.GetHTTPStatusCode())
39+
if err != nil {
40+
e := err.(*response.Error)
41+
fmt.Printf(tools.Prettify(e))
42+
return
43+
}
44+
fmt.Printf("reault:%s", tools.Prettify(result))
45+
}

sample/api/translation.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/larksuite/oapi-sdk-go/api/core/response"
7+
"github.com/larksuite/oapi-sdk-go/core"
8+
"github.com/larksuite/oapi-sdk-go/core/constants"
9+
"github.com/larksuite/oapi-sdk-go/core/tools"
10+
"github.com/larksuite/oapi-sdk-go/sample/configs"
11+
translation "github.com/larksuite/oapi-sdk-go/service/translation/v1"
12+
)
13+
14+
// for redis store and logrus
15+
// configs.TestConfigWithLogrusAndRedisStore(constants.DomainFeiShu)
16+
// configs.TestConfig("https://open.feishu.cn")
17+
var translationService = translation.NewService(configs.TestConfig(constants.DomainFeiShu))
18+
19+
func main() {
20+
testTextDetect()
21+
}
22+
23+
func testTextDetect() {
24+
coreCtx := core.WrapContext(context.Background())
25+
reqCall := translationService.Texts.Translate(coreCtx, &translation.TextTranslateReqBody{
26+
SourceLanguage: "zh",
27+
Text: "测试",
28+
TargetLanguage: "en",
29+
Glossary: []*translation.Term{
30+
{
31+
From: "测",
32+
To: "test",
33+
},
34+
},
35+
})
36+
result, err := reqCall.Do()
37+
fmt.Printf("request_id:%s\n", coreCtx.GetRequestID())
38+
fmt.Printf("http status code:%d", coreCtx.GetHTTPStatusCode())
39+
if err != nil {
40+
e := err.(*response.Error)
41+
fmt.Printf(tools.Prettify(e))
42+
return
43+
}
44+
fmt.Printf("reault:%s", tools.Prettify(result))
45+
}

sample/event/contact.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/larksuite/oapi-sdk-go/core/constants"
6-
"github.com/larksuite/oapi-sdk-go/sample/configs"
7-
"path"
8-
95
"github.com/gin-gonic/gin"
106
"github.com/larksuite/oapi-sdk-go/core"
7+
"github.com/larksuite/oapi-sdk-go/core/constants"
118
"github.com/larksuite/oapi-sdk-go/core/tools"
129
eventginserver "github.com/larksuite/oapi-sdk-go/event/http/gin"
10+
"github.com/larksuite/oapi-sdk-go/sample/configs"
1311
contact "github.com/larksuite/oapi-sdk-go/service/contact/v3"
1412
)
1513

@@ -32,14 +30,14 @@ func main() {
3230
return nil
3331
})
3432

35-
contact.SetDepartmentDeletedEventHandler(conf, func(ctx *core.Context, event *contact.DepartmentDeletedEvent) error {
33+
contact.SetUserUpdatedEventHandler(conf, func(ctx *core.Context, event *contact.UserUpdatedEvent) error {
3634
fmt.Println(ctx.GetRequestID())
3735
fmt.Println(tools.Prettify(event))
3836
return nil
3937
})
4038

4139
g := gin.Default()
42-
eventginserver.Register(path.Join("/", conf.GetAppSettings().AppID, "webhook/event"), conf, g)
40+
eventginserver.Register("/webhook/event", conf, g)
4341
err := g.Run(":8089")
4442
if err != nil {
4543
fmt.Println(err)

0 commit comments

Comments
 (0)