From 42205fc51474f3e2d6db79efc2f6194d20037c61 Mon Sep 17 00:00:00 2001 From: "xinning.wwm" Date: Wed, 9 Aug 2023 15:03:13 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?Demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/client.go | 6 ++++++ example/echo_service.go | 14 +++++++++++++ example/example.go | 16 ++++++++++++++ payload/utils.go | 3 ++- plugin/model.go | 40 +++++++++++++++++++++++++++++++++++ plugin/plugin_handler.go | 45 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 example/echo_service.go create mode 100644 plugin/model.go create mode 100644 plugin/plugin_handler.go diff --git a/client/client.go b/client/client.go index f57a529..37b4f1d 100644 --- a/client/client.go +++ b/client/client.go @@ -11,6 +11,7 @@ import ( "github.com/open-dingtalk/dingtalk-stream-sdk-go/handler" "github.com/open-dingtalk/dingtalk-stream-sdk-go/logger" "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/plugin" "github.com/open-dingtalk/dingtalk-stream-sdk-go/utils" "io" "net/http" @@ -380,6 +381,11 @@ func (cli *StreamClient) RegisterChatBotCallbackRouter(messageHandler chatbot.IC cli.RegisterRouter(utils.SubscriptionTypeKCallback, payload.BotMessageCallbackTopic, chatbot.NewDefaultChatBotFrameHandler(messageHandler).OnEventReceived) } +// AI插件的注册函数 +func (cli *StreamClient) RegisterPluginCallbackRouter(messageHandler plugin.IDingTalkPluginHandler) { + cli.RegisterRouter(utils.SubscriptionTypeKCallback, payload.BotPluginCallbackTopic, plugin.NewDefaultDingTalkPluginFrameHandler(messageHandler).OnEventReceived) +} + // 事件类型的注册函数 func (cli *StreamClient) RegisterEventRouter(topic string, frameHandler handler.IFrameHandler) { cli.RegisterRouter(utils.SubscriptionTypeKEvent, topic, frameHandler) diff --git a/example/echo_service.go b/example/echo_service.go new file mode 100644 index 0000000..74f657a --- /dev/null +++ b/example/echo_service.go @@ -0,0 +1,14 @@ +package main + +type EchoRequest struct { + Message string +} +type EchoResponse struct { + EchoMessage string +} + +func Echo(echoRequest *EchoRequest) *EchoResponse { + return &EchoResponse{ + EchoMessage: "You said: " + echoRequest.Message, + } +} diff --git a/example/example.go b/example/example.go index 437aa27..2b1f667 100644 --- a/example/example.go +++ b/example/example.go @@ -9,6 +9,7 @@ import ( "github.com/open-dingtalk/dingtalk-stream-sdk-go/event" "github.com/open-dingtalk/dingtalk-stream-sdk-go/logger" "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/plugin" ) /** @@ -27,6 +28,20 @@ func OnChatBotMessageReceived(ctx context.Context, data *chatbot.BotCallbackData return []byte(""), nil } +// 简单的插件处理实现 +func OnPluginRequestReceived(ctx context.Context, message *plugin.DingTalkPluginMessage) (interface{}, error) { + if message.AbilityKey == "echo" { + echoRequest := &EchoRequest{} + err := message.ParseData(echoRequest) + if err != nil { + return nil, err + } + echoResponse := Echo(echoRequest) + return echoResponse, nil + } + return nil, nil +} + // 事件处理 func OnEventReceived(ctx context.Context, df *payload.DataFrame) (frameResp *payload.DataFrameResponse, err error) { eventHeader := event.NewEventHeaderFromDataFrame(df) @@ -62,6 +77,7 @@ func main() { //注册callback类型的处理函数 cli.RegisterChatBotCallbackRouter(OnChatBotMessageReceived) + cli.RegisterPluginCallbackRouter(OnPluginRequestReceived) err := cli.Start(context.Background()) if err != nil { panic(err) diff --git a/payload/utils.go b/payload/utils.go index 8a48fc2..474ce21 100644 --- a/payload/utils.go +++ b/payload/utils.go @@ -23,7 +23,8 @@ const ( DataFrameResponseStatusCodeKInternalError = 500 DataFrameResponseStatusCodeKHandlerNotFound = 404 - BotMessageCallbackTopic = "/v1.0/im/bot/messages/get" //机器人消息统一回调topic + BotMessageCallbackTopic = "/v1.0/im/bot/messages/get" //机器人消息统一回调topic + BotPluginCallbackTopic = "/v1.0/agi/plugins/callback" //AI插件消息统一回调topic ) func GenerateMessageId(prefix string) string { diff --git a/plugin/model.go b/plugin/model.go new file mode 100644 index 0000000..9eeab80 --- /dev/null +++ b/plugin/model.go @@ -0,0 +1,40 @@ +package plugin + +import ( + "errors" + "fmt" + "reflect" +) + +type DingTalkPluginMessage struct { + PluginId string `json:"pluginId"` + PluginVersion string `json:"pluginVersion"` + AbilityKey string `json:"abilityKey"` + Data interface{} `json:"data"` + RequestId string `json:"requestId"` +} + +func (req *DingTalkPluginMessage) ParseData(model interface{}) error { + //TO DO 处理异常 + defer func() { + recover() + }() + m, ok := req.Data.(map[string]interface{}) + if !ok { + return errors.New(fmt.Sprintf("invalid data: %v", req.Data)) + } + stValue := reflect.ValueOf(model).Elem() + sType := stValue.Type() + for i := 0; i < sType.NumField(); i++ { + field := sType.Field(i) + if value, ok := m[field.Name]; ok { + stValue.Field(i).Set(reflect.ValueOf(value)) + } + } + return nil +} + +type DingTalkPluginResponse struct { + Result interface{} `json:"result"` + RequestId string `json:"requestId"` +} diff --git a/plugin/plugin_handler.go b/plugin/plugin_handler.go new file mode 100644 index 0000000..9382180 --- /dev/null +++ b/plugin/plugin_handler.go @@ -0,0 +1,45 @@ +package plugin + +import ( + "context" + "encoding/json" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" +) + +type CallbackResponse struct { + Response interface{} `json:"response"` +} + +type IDingTalkPluginHandler func(c context.Context, data *DingTalkPluginMessage) (interface{}, error) + +type DefaultDingTalkPluginFrameHandler struct { + defaultHandler IDingTalkPluginHandler +} + +func NewDefaultDingTalkPluginFrameHandler(defaultHandler IDingTalkPluginHandler) *DefaultDingTalkPluginFrameHandler { + return &DefaultDingTalkPluginFrameHandler{ + defaultHandler: defaultHandler, + } +} + +func (h *DefaultDingTalkPluginFrameHandler) OnEventReceived(ctx context.Context, df *payload.DataFrame) (*payload.DataFrameResponse, error) { + msgData := &DingTalkPluginMessage{} + err := json.Unmarshal([]byte(df.Data), msgData) + if err != nil { + return nil, err + } + + if h.defaultHandler == nil { + return payload.NewDataFrameResponse(payload.DataFrameResponseStatusCodeKHandlerNotFound), nil + } + + result, err := h.defaultHandler(ctx, msgData) + if err != nil { + return nil, err + } + dingTalkPluginResponse := &DingTalkPluginResponse{RequestId: msgData.RequestId, Result: result} + callbackResponse := &CallbackResponse{Response: dingTalkPluginResponse} + frameResp := payload.NewSuccessDataFrameResponse() + frameResp.SetJson(callbackResponse) + return frameResp, nil +} From 1fa75617956456a92ea843c6bde14871a3449c66 Mon Sep 17 00:00:00 2001 From: weiming wu Date: Thu, 10 Aug 2023 14:36:57 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/example.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/example/example.go b/example/example.go index 2b1f667..d06743d 100644 --- a/example/example.go +++ b/example/example.go @@ -29,8 +29,8 @@ func OnChatBotMessageReceived(ctx context.Context, data *chatbot.BotCallbackData } // 简单的插件处理实现 -func OnPluginRequestReceived(ctx context.Context, message *plugin.DingTalkPluginMessage) (interface{}, error) { - if message.AbilityKey == "echo" { +func OnPluginMessageReceived(ctx context.Context, message *plugin.DingTalkPluginMessage) (interface{}, error) { + if message.AbilityKey == "echo" { //可以根据message中的PluginId、PluginVersion、AbilityKey路由到具体一个能力 echoRequest := &EchoRequest{} err := message.ParseData(echoRequest) if err != nil { @@ -70,14 +70,15 @@ func main() { logger.SetLogger(logger.NewStdTestLogger()) - cli := client.NewStreamClient(client.WithAppCredential(client.NewAppCredentialConfig(clientId, clientSecret))) - + //cli := client.NewStreamClient(client.WithAppCredential(client.NewAppCredentialConfig(clientId, clientSecret))) + cli := client.NewStreamClient(client.WithAppCredential(client.NewAppCredentialConfig("dingfplahibzeqzfudlz", "If8TqLjRXpUuq8vPUdQ4042qRYTaCXaXldYZOJUq8xnCiwrhDYAz5YHxS7l2oPoC"))) //注册事件类型的处理函数 cli.RegisterAllEventRouter(OnEventReceived) //注册callback类型的处理函数 cli.RegisterChatBotCallbackRouter(OnChatBotMessageReceived) + //注册插件的处理函数 + cli.RegisterPluginCallbackRouter(OnPluginMessageReceived) - cli.RegisterPluginCallbackRouter(OnPluginRequestReceived) err := cli.Start(context.Background()) if err != nil { panic(err) From 0845d43d4682c52ac574f1c55f30f1606cd08a24 Mon Sep 17 00:00:00 2001 From: weiming wu Date: Thu, 10 Aug 2023 15:28:17 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/echo_service.go | 1 + example/example.go | 9 ++++++--- plugin/model.go | 8 +++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/example/echo_service.go b/example/echo_service.go index 74f657a..c22c499 100644 --- a/example/echo_service.go +++ b/example/echo_service.go @@ -1,5 +1,6 @@ package main +// 插件例子 type EchoRequest struct { Message string } diff --git a/example/example.go b/example/example.go index d06743d..accc50b 100644 --- a/example/example.go +++ b/example/example.go @@ -30,12 +30,15 @@ func OnChatBotMessageReceived(ctx context.Context, data *chatbot.BotCallbackData // 简单的插件处理实现 func OnPluginMessageReceived(ctx context.Context, message *plugin.DingTalkPluginMessage) (interface{}, error) { - if message.AbilityKey == "echo" { //可以根据message中的PluginId、PluginVersion、AbilityKey路由到具体一个能力 + //可以根据message中的PluginId、PluginVersion、AbilityKey路由到具体一个能力 + if message.AbilityKey == "echo" { echoRequest := &EchoRequest{} + //将数据转换成插件的请求参数 err := message.ParseData(echoRequest) if err != nil { return nil, err } + //执行插件 echoResponse := Echo(echoRequest) return echoResponse, nil } @@ -70,8 +73,8 @@ func main() { logger.SetLogger(logger.NewStdTestLogger()) - //cli := client.NewStreamClient(client.WithAppCredential(client.NewAppCredentialConfig(clientId, clientSecret))) - cli := client.NewStreamClient(client.WithAppCredential(client.NewAppCredentialConfig("dingfplahibzeqzfudlz", "If8TqLjRXpUuq8vPUdQ4042qRYTaCXaXldYZOJUq8xnCiwrhDYAz5YHxS7l2oPoC"))) + cli := client.NewStreamClient(client.WithAppCredential(client.NewAppCredentialConfig(clientId, clientSecret))) + //注册事件类型的处理函数 cli.RegisterAllEventRouter(OnEventReceived) //注册callback类型的处理函数 diff --git a/plugin/model.go b/plugin/model.go index 9eeab80..399489f 100644 --- a/plugin/model.go +++ b/plugin/model.go @@ -14,10 +14,12 @@ type DingTalkPluginMessage struct { RequestId string `json:"requestId"` } -func (req *DingTalkPluginMessage) ParseData(model interface{}) error { - //TO DO 处理异常 +// 用于将数据转换成插件的请求参数 +func (req *DingTalkPluginMessage) ParseData(model interface{}) (err error) { defer func() { - recover() + if e := recover(); e != nil { + err = errors.New(fmt.Sprintf("parse data error: %v", e)) + } }() m, ok := req.Data.(map[string]interface{}) if !ok { From b46abaf5162caba3660de647e1d52849ac7d6a01 Mon Sep 17 00:00:00 2001 From: weiming wu Date: Thu, 10 Aug 2023 15:33:37 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/client.go | 4 ++-- payload/utils.go | 4 ++-- plugin/plugin_handler.go | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/client/client.go b/client/client.go index 37b4f1d..52c7136 100644 --- a/client/client.go +++ b/client/client.go @@ -382,8 +382,8 @@ func (cli *StreamClient) RegisterChatBotCallbackRouter(messageHandler chatbot.IC } // AI插件的注册函数 -func (cli *StreamClient) RegisterPluginCallbackRouter(messageHandler plugin.IDingTalkPluginHandler) { - cli.RegisterRouter(utils.SubscriptionTypeKCallback, payload.BotPluginCallbackTopic, plugin.NewDefaultDingTalkPluginFrameHandler(messageHandler).OnEventReceived) +func (cli *StreamClient) RegisterPluginCallbackRouter(messageHandler plugin.IPluginMessageHandler) { + cli.RegisterRouter(utils.SubscriptionTypeKCallback, payload.PluginMessageCallbackTopic, plugin.NewDefaultPluginFrameHandler(messageHandler).OnEventReceived) } // 事件类型的注册函数 diff --git a/payload/utils.go b/payload/utils.go index 474ce21..0e5f5c9 100644 --- a/payload/utils.go +++ b/payload/utils.go @@ -23,8 +23,8 @@ const ( DataFrameResponseStatusCodeKInternalError = 500 DataFrameResponseStatusCodeKHandlerNotFound = 404 - BotMessageCallbackTopic = "/v1.0/im/bot/messages/get" //机器人消息统一回调topic - BotPluginCallbackTopic = "/v1.0/agi/plugins/callback" //AI插件消息统一回调topic + BotMessageCallbackTopic = "/v1.0/im/bot/messages/get" //机器人消息统一回调topic + PluginMessageCallbackTopic = "/v1.0/agi/plugins/callback" //AI插件消息统一回调topic ) func GenerateMessageId(prefix string) string { diff --git a/plugin/plugin_handler.go b/plugin/plugin_handler.go index 9382180..01bbd73 100644 --- a/plugin/plugin_handler.go +++ b/plugin/plugin_handler.go @@ -10,19 +10,19 @@ type CallbackResponse struct { Response interface{} `json:"response"` } -type IDingTalkPluginHandler func(c context.Context, data *DingTalkPluginMessage) (interface{}, error) +type IPluginMessageHandler func(c context.Context, data *DingTalkPluginMessage) (interface{}, error) -type DefaultDingTalkPluginFrameHandler struct { - defaultHandler IDingTalkPluginHandler +type DefaultPluginFrameHandler struct { + defaultHandler IPluginMessageHandler } -func NewDefaultDingTalkPluginFrameHandler(defaultHandler IDingTalkPluginHandler) *DefaultDingTalkPluginFrameHandler { - return &DefaultDingTalkPluginFrameHandler{ +func NewDefaultPluginFrameHandler(defaultHandler IPluginMessageHandler) *DefaultPluginFrameHandler { + return &DefaultPluginFrameHandler{ defaultHandler: defaultHandler, } } -func (h *DefaultDingTalkPluginFrameHandler) OnEventReceived(ctx context.Context, df *payload.DataFrame) (*payload.DataFrameResponse, error) { +func (h *DefaultPluginFrameHandler) OnEventReceived(ctx context.Context, df *payload.DataFrame) (*payload.DataFrameResponse, error) { msgData := &DingTalkPluginMessage{} err := json.Unmarshal([]byte(df.Data), msgData) if err != nil { From 8e2b1452b3148e48c91e8615e16d35a415d88ce2 Mon Sep 17 00:00:00 2001 From: weiming wu Date: Thu, 10 Aug 2023 15:38:27 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/example.go | 2 +- plugin/model.go | 6 +++--- plugin/plugin_handler.go | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/example/example.go b/example/example.go index accc50b..4a9dc41 100644 --- a/example/example.go +++ b/example/example.go @@ -29,7 +29,7 @@ func OnChatBotMessageReceived(ctx context.Context, data *chatbot.BotCallbackData } // 简单的插件处理实现 -func OnPluginMessageReceived(ctx context.Context, message *plugin.DingTalkPluginMessage) (interface{}, error) { +func OnPluginMessageReceived(ctx context.Context, message *plugin.PluginMessage) (interface{}, error) { //可以根据message中的PluginId、PluginVersion、AbilityKey路由到具体一个能力 if message.AbilityKey == "echo" { echoRequest := &EchoRequest{} diff --git a/plugin/model.go b/plugin/model.go index 399489f..d77ce01 100644 --- a/plugin/model.go +++ b/plugin/model.go @@ -6,7 +6,7 @@ import ( "reflect" ) -type DingTalkPluginMessage struct { +type PluginMessage struct { PluginId string `json:"pluginId"` PluginVersion string `json:"pluginVersion"` AbilityKey string `json:"abilityKey"` @@ -15,7 +15,7 @@ type DingTalkPluginMessage struct { } // 用于将数据转换成插件的请求参数 -func (req *DingTalkPluginMessage) ParseData(model interface{}) (err error) { +func (req *PluginMessage) ParseData(model interface{}) (err error) { defer func() { if e := recover(); e != nil { err = errors.New(fmt.Sprintf("parse data error: %v", e)) @@ -36,7 +36,7 @@ func (req *DingTalkPluginMessage) ParseData(model interface{}) (err error) { return nil } -type DingTalkPluginResponse struct { +type PluginResponse struct { Result interface{} `json:"result"` RequestId string `json:"requestId"` } diff --git a/plugin/plugin_handler.go b/plugin/plugin_handler.go index 01bbd73..339d4f9 100644 --- a/plugin/plugin_handler.go +++ b/plugin/plugin_handler.go @@ -10,7 +10,7 @@ type CallbackResponse struct { Response interface{} `json:"response"` } -type IPluginMessageHandler func(c context.Context, data *DingTalkPluginMessage) (interface{}, error) +type IPluginMessageHandler func(c context.Context, data *PluginMessage) (interface{}, error) type DefaultPluginFrameHandler struct { defaultHandler IPluginMessageHandler @@ -23,7 +23,7 @@ func NewDefaultPluginFrameHandler(defaultHandler IPluginMessageHandler) *Default } func (h *DefaultPluginFrameHandler) OnEventReceived(ctx context.Context, df *payload.DataFrame) (*payload.DataFrameResponse, error) { - msgData := &DingTalkPluginMessage{} + msgData := &PluginMessage{} err := json.Unmarshal([]byte(df.Data), msgData) if err != nil { return nil, err @@ -37,8 +37,8 @@ func (h *DefaultPluginFrameHandler) OnEventReceived(ctx context.Context, df *pay if err != nil { return nil, err } - dingTalkPluginResponse := &DingTalkPluginResponse{RequestId: msgData.RequestId, Result: result} - callbackResponse := &CallbackResponse{Response: dingTalkPluginResponse} + pluginResponse := &PluginResponse{RequestId: msgData.RequestId, Result: result} + callbackResponse := &CallbackResponse{Response: pluginResponse} frameResp := payload.NewSuccessDataFrameResponse() frameResp.SetJson(callbackResponse) return frameResp, nil From 851bc54daf5261324a38586130ebd8e600ea5677 Mon Sep 17 00:00:00 2001 From: weiming wu Date: Thu, 10 Aug 2023 15:44:09 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/model.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin/model.go b/plugin/model.go index d77ce01..c4f30ac 100644 --- a/plugin/model.go +++ b/plugin/model.go @@ -25,12 +25,12 @@ func (req *PluginMessage) ParseData(model interface{}) (err error) { if !ok { return errors.New(fmt.Sprintf("invalid data: %v", req.Data)) } - stValue := reflect.ValueOf(model).Elem() - sType := stValue.Type() - for i := 0; i < sType.NumField(); i++ { - field := sType.Field(i) + pValue := reflect.ValueOf(model).Elem() + pType := pValue.Type() + for i := 0; i < pType.NumField(); i++ { + field := pType.Field(i) if value, ok := m[field.Name]; ok { - stValue.Field(i).Set(reflect.ValueOf(value)) + pValue.Field(i).Set(reflect.ValueOf(value)) } } return nil From b37dd68939daee1d3fbffbad669c8f93e6e4fd39 Mon Sep 17 00:00:00 2001 From: weiming wu Date: Thu, 10 Aug 2023 16:36:37 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/example.go | 2 +- go.mod | 1 + go.sum | 2 ++ plugin/model.go | 27 ++++++++------------------- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/example/example.go b/example/example.go index 4a9dc41..0af836f 100644 --- a/example/example.go +++ b/example/example.go @@ -34,7 +34,7 @@ func OnPluginMessageReceived(ctx context.Context, message *plugin.PluginMessage) if message.AbilityKey == "echo" { echoRequest := &EchoRequest{} //将数据转换成插件的请求参数 - err := message.ParseData(echoRequest) + err := message.ParseRequest(echoRequest) if err != nil { return nil, err } diff --git a/go.mod b/go.mod index b70cd2a..c9eb776 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c3dad28..0d40687 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/plugin/model.go b/plugin/model.go index c4f30ac..a8970f6 100644 --- a/plugin/model.go +++ b/plugin/model.go @@ -1,9 +1,7 @@ package plugin import ( - "errors" - "fmt" - "reflect" + "encoding/json" ) type PluginMessage struct { @@ -15,23 +13,14 @@ type PluginMessage struct { } // 用于将数据转换成插件的请求参数 -func (req *PluginMessage) ParseData(model interface{}) (err error) { - defer func() { - if e := recover(); e != nil { - err = errors.New(fmt.Sprintf("parse data error: %v", e)) - } - }() - m, ok := req.Data.(map[string]interface{}) - if !ok { - return errors.New(fmt.Sprintf("invalid data: %v", req.Data)) +func (req *PluginMessage) ParseRequest(pluginRequest interface{}) error { + data, err := json.Marshal(req.Data) + if err != nil { + return err } - pValue := reflect.ValueOf(model).Elem() - pType := pValue.Type() - for i := 0; i < pType.NumField(); i++ { - field := pType.Field(i) - if value, ok := m[field.Name]; ok { - pValue.Field(i).Set(reflect.ValueOf(value)) - } + err = json.Unmarshal(data, pluginRequest) + if err != nil { + return err } return nil } From cad976572cceaf9d6a424e0fffcf01d8ed59b21c Mon Sep 17 00:00:00 2001 From: weiming wu Date: Thu, 10 Aug 2023 16:45:04 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 - 1 file changed, 1 deletion(-) diff --git a/go.mod b/go.mod index c9eb776..b70cd2a 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) From f189c22d23cad8c3eedb278c85e4eea8b03b4045 Mon Sep 17 00:00:00 2001 From: weiming wu Date: Thu, 10 Aug 2023 16:46:06 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0stream=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index 0d40687..c3dad28 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,6 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= -github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=