From 324f577dc5dbe6e13a6b6d6e63f37563d409e17a Mon Sep 17 00:00:00 2001 From: luhaoling <2198702716@qq.com> Date: Thu, 4 Jan 2024 18:54:44 +0800 Subject: [PATCH] feat: add callback example --- go.work | 1 + test/callbackexample/control/group.go | 43 +++++++++++++++++++++++++++ test/callbackexample/go.mod | 32 ++++++++++++++++++++ test/callbackexample/main.go | 16 ++++++++++ test/callbackexample/model/group.go | 40 +++++++++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 test/callbackexample/control/group.go create mode 100644 test/callbackexample/go.mod create mode 100644 test/callbackexample/main.go create mode 100644 test/callbackexample/model/group.go diff --git a/go.work b/go.work index 63100b9f6..d5210f3bf 100644 --- a/go.work +++ b/go.work @@ -14,4 +14,5 @@ use ( ./tools/url2im ./tools/data-conversion ./tools/up35 + test/callbackexample ) diff --git a/test/callbackexample/control/group.go b/test/callbackexample/control/group.go new file mode 100644 index 000000000..a9eda5d16 --- /dev/null +++ b/test/callbackexample/control/group.go @@ -0,0 +1,43 @@ +package control + +import ( + "call-back-http/model" + "encoding/json" + "fmt" + "github.com/gin-gonic/gin" + "net/http" +) + +func CallbackBeforeSendSingleMsgCommand(c *gin.Context) { + var req model.CallbackBeforeSendSingleMsgReq + if err := c.BindJSON(&req); err != nil { + fmt.Printf("err:%v", err) + return + } + fmt.Printf("CallbackBeforeSendSingleMsgCommand received:%#v\n", req) + str := "callback return message" + byte, err := json.Marshal(str) + if err != nil { + c.JSON(http.StatusInternalServerError, &model.CallbackBeforeSendSingleMsgResp{ + CommonCallbackResp: model.CommonCallbackResp{ + ActionCode: 500, + ErrCode: 5001, + ErrMsg: "callback error", + ErrDlt: err.Error(), + NextCode: 2, + }, + }) + } + resp := &model.CallbackBeforeSendSingleMsgResp{ + CommonCallbackResp: model.CommonCallbackResp{ + ActionCode: 0, + ErrCode: 2000, + ErrMsg: "Success", + ErrDlt: "Successful", + NextCode: 2, + }, + Content: byte, + } + fmt.Printf("CallbackBeforeSendSingleMsgCommand return:%#v\n", resp) + c.JSON(http.StatusOK, resp) +} diff --git a/test/callbackexample/go.mod b/test/callbackexample/go.mod new file mode 100644 index 000000000..ffd17c0b3 --- /dev/null +++ b/test/callbackexample/go.mod @@ -0,0 +1,32 @@ +module call-back-http + +go 1.20 + +require github.com/gin-gonic/gin v1.9.1 + +require ( + github.com/bytedance/sonic v1.9.1 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.14.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/arch v0.3.0 // indirect + golang.org/x/crypto v0.9.0 // indirect + golang.org/x/net v0.10.0 // indirect + golang.org/x/sys v0.8.0 // indirect + golang.org/x/text v0.9.0 // indirect + google.golang.org/protobuf v1.30.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/test/callbackexample/main.go b/test/callbackexample/main.go new file mode 100644 index 000000000..73c62d15b --- /dev/null +++ b/test/callbackexample/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "call-back-http/control" + "github.com/gin-gonic/gin" +) + +func main() { + engine := gin.Default() + router := engine.Group("/callback") + router.POST("/callbackBeforeSendSingleMsgCommand", control.CallbackBeforeSendSingleMsgCommand) + + if err := engine.Run("0.0.0.0:18889"); err != nil { + panic(err) + } +} diff --git a/test/callbackexample/model/group.go b/test/callbackexample/model/group.go new file mode 100644 index 000000000..a9abaa88d --- /dev/null +++ b/test/callbackexample/model/group.go @@ -0,0 +1,40 @@ +package model + + +type CommonCallbackReq struct { + SendID string `json:"sendID"` + CallbackCommand string `json:"callbackCommand"` + ServerMsgID string `json:"serverMsgID"` + ClientMsgID string `json:"clientMsgID"` + OperationID string `json:"operationID"` + SenderPlatformID int32 `json:"senderPlatformID"` + SenderNickname string `json:"senderNickname"` + SessionType int32 `json:"sessionType"` + MsgFrom int32 `json:"msgFrom"` + ContentType int32 `json:"contentType"` + Status int32 `json:"status"` + CreateTime int64 `json:"createTime"` + Content string `json:"content"` + Seq uint32 `json:"seq"` + AtUserIDList []string `json:"atUserList"` + SenderFaceURL string `json:"faceURL"` + Ex string `json:"ex"` +} + +type CallbackBeforeSendSingleMsgReq struct { + CommonCallbackReq + RecvID string `json:"recvID"` +} + +type CallbackBeforeSendSingleMsgResp struct { + CommonCallbackResp + Content []byte `json:"content"` +} + +type CommonCallbackResp struct { + ActionCode int32 `json:"actionCode"` + ErrCode int32 ` json:"errCode"` + ErrMsg string ` json:"errMsg"` + ErrDlt string `json:"errDlt"` + NextCode int32 `json:"nextCode"` +}