From 5c3b66aad64a63f1db842e75b7c33b5e80d387fb Mon Sep 17 00:00:00 2001 From: luhaoling <2198702716@qq.com> Date: Mon, 15 Jan 2024 12:08:22 +0800 Subject: [PATCH] feat: add webhook example --- deployments/templates/openim.yaml | 4 +- internal/api/msg.go | 64 +++++++++++++++++++++++++++++++ internal/api/route.go | 2 + 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/deployments/templates/openim.yaml b/deployments/templates/openim.yaml index 4c84373a5..7b2431bf5 100644 --- a/deployments/templates/openim.yaml +++ b/deployments/templates/openim.yaml @@ -315,9 +315,9 @@ iosPush: # Timeout in seconds # Whether to continue execution if callback fails callback: - url: "" + url: "http://127.0.0.1:10002/msg" beforeSendSingleMsg: - enable: ${CALLBACK_ENABLE} + enable: true timeout: ${CALLBACK_TIMEOUT} failedContinue: ${CALLBACK_FAILED_CONTINUE} beforeUpdateUserInfoEx: diff --git a/internal/api/msg.go b/internal/api/msg.go index 9348596ac..57a049536 100644 --- a/internal/api/msg.go +++ b/internal/api/msg.go @@ -27,6 +27,8 @@ import ( "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "github.com/mitchellh/mapstructure" + "github.com/openimsdk/open-im-server/v3/pkg/callbackstruct" + "time" "github.com/openimsdk/open-im-server/v3/pkg/authverify" "github.com/openimsdk/open-im-server/v3/pkg/common/config" @@ -377,3 +379,65 @@ func (m *MessageApi) SearchMsg(c *gin.Context) { func (m *MessageApi) GetServerTime(c *gin.Context) { a2r.Call(msg.MsgClient.GetServerTime, m.Client, c) } + +func (m *MessageApi) CallbackExample(c *gin.Context) { + // 1. 通过 url 获取具体信息 + // 2. 返回继续向下执行的命令 + // 3. 获取一个系统通知号 + // 4. 构造一个发消息的结构体 + // 5. 使用这个系统通知号发送消息 + + var req callbackstruct.CallbackBeforeSendSingleMsgReq + + if err := c.BindJSON(&req); err != nil { + log.ZError(c, "CallbackExample BindJSON failed", err) + apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) + return + } + log.ZInfo(c, "CallbackExample", "req", req) + + resp := &callbackstruct.CallbackBeforeSendSingleMsgResp{ + CommonCallbackResp: callbackstruct.CommonCallbackResp{ + ActionCode: 0, + ErrCode: 200, + ErrMsg: "success", + ErrDlt: "successful", + NextCode: 0, + }, + } + + apiresp.GinSuccess(c, resp) + + user, err := m.userRpcClient.GetUserInfo(c, config.Config.IMAdmin.UserID[0]) + if err != nil { + log.ZError(c, "GetUserInfo failed", err) + apiresp.GinError(c, errs.ErrDatabase.WithDetail(err.Error()).Wrap()) + return + } + + time := time.Now().Unix() + msgInfo := &sdkws.MsgData{ + SendID: user.UserID, + RecvID: req.RecvID, + ClientMsgID: req.ClientMsgID, + ServerMsgID: req.ServerMsgID, + SenderPlatformID: req.SenderPlatformID, + SenderNickname: user.Nickname, + SenderFaceURL: user.UserID, + SessionType: req.SessionType, + MsgFrom: req.MsgFrom, + ContentType: req.ContentType, + Content: []byte(req.Content), + Seq: int64(req.Seq), + SendTime: time, + CreateTime: time, + Status: req.Status, + } + rsp, err := m.Message.Client.SendMsg(c, &msg.SendMsgReq{MsgData: msgInfo}) + if err != nil { + log.ZError(c, "SendMsg failed", err) + apiresp.GinError(c, errs.ErrDatabase.WithDetail(err.Error()).Wrap()) + return + } + apiresp.GinSuccess(c, rsp) +} diff --git a/internal/api/route.go b/internal/api/route.go index 3f16d3e50..839bc0ff0 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -200,6 +200,8 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive msgGroup.POST("/batch_send_msg", m.BatchSendMsg) msgGroup.POST("/check_msg_is_send_success", m.CheckMsgIsSendSuccess) msgGroup.POST("/get_server_time", m.GetServerTime) + + msgGroup.POST("/callbackBeforeSendSingleMsgCommand", m.CallbackExample) } // Conversation conversationGroup := r.Group("/conversation", ParseToken)