feat: sending messages supports returning fields modified by webhook (#3192)
* pb * fix: Modifying other fields while setting IsPrivateChat does not take effect * fix: quote message error revoke * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * upgrading pkg tools * fix * fix * optimize log output * feat: support GetLastMessage * feat: support GetLastMessage * feat: s3 switch * feat: s3 switch * fix: GetUsersOnline * feat: SendBusinessNotification supported configuration parameters * feat: SendBusinessNotification supported configuration parameters * feat: SendBusinessNotification supported configuration parameters * feat: seq conversion failed without exiting * fix: DeleteDoc crash * fix: fill send time * fix: fill send time * fix: crash caused by withdrawing messages from users who have left the group * fix: user msg timestamp * seq read config * seq read config * fix: the source message of the reference is withdrawn, and the referenced message is deleted * feat: optimize the default notification.yml * fix: shouldPushOffline * fix: the sorting is wrong after canceling the administrator in group settings * feat: Sending messages supports returning fields modified by webhook * feat: Sending messages supports returning fields modified by webhook * feat: Sending messages supports returning fields modified by webhook3.8.3-patch-bak
parent
0541d0bf06
commit
964ee7a8dd
@ -1 +0,0 @@
|
||||
package apistruct
|
@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
cbapi "github.com/openimsdk/open-im-server/v3/pkg/callbackstruct"
|
||||
"github.com/openimsdk/protocol/constant"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g := gin.Default()
|
||||
g.POST("/callbackExample/callbackBeforeMsgModifyCommand", toGin(handlerMsg))
|
||||
if err := g.Run(":10006"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func toGin[R any](fn func(c *gin.Context, req *R)) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Printf("HTTP %s %s %s\n", c.Request.Method, c.Request.URL, body)
|
||||
var req R
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
fn(c, &req)
|
||||
}
|
||||
}
|
||||
|
||||
func handlerMsg(c *gin.Context, req *cbapi.CallbackMsgModifyCommandReq) {
|
||||
var resp cbapi.CallbackMsgModifyCommandResp
|
||||
if req.ContentType != constant.Text {
|
||||
c.JSON(http.StatusOK, &resp)
|
||||
return
|
||||
}
|
||||
var textElem struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(req.Content), &textElem); err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
const word = "xxx"
|
||||
if strings.Contains(textElem.Content, word) {
|
||||
textElem.Content = strings.ReplaceAll(textElem.Content, word, strings.Repeat("*", len(word)))
|
||||
content, err := json.Marshal(&textElem)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
tmp := string(content)
|
||||
resp.Content = &tmp
|
||||
}
|
||||
c.JSON(http.StatusOK, &resp)
|
||||
}
|
@ -1,161 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/apistruct"
|
||||
cbapi "github.com/openimsdk/open-im-server/v3/pkg/callbackstruct"
|
||||
"github.com/openimsdk/protocol/auth"
|
||||
"github.com/openimsdk/protocol/constant"
|
||||
"github.com/openimsdk/protocol/msg"
|
||||
"github.com/openimsdk/tools/apiresp"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
getAdminToken = "/auth/get_admin_token"
|
||||
sendMsgApi = "/msg/send_msg"
|
||||
appendStreamMsg = "/msg/append_stream_msg"
|
||||
)
|
||||
|
||||
var (
|
||||
ApiAddr = "http://127.0.0.1:10002"
|
||||
Token string
|
||||
)
|
||||
|
||||
func ApiCall[R any](api string, req any) (*R, error) {
|
||||
data, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||
defer cancel()
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodPost, ApiAddr+api, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if Token != "" {
|
||||
request.Header.Set("token", Token)
|
||||
}
|
||||
request.Header.Set(constant.OperationID, uuid.New().String())
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
var resp R
|
||||
apiResponse := apiresp.ApiResponse{
|
||||
Data: &resp,
|
||||
}
|
||||
if err := json.NewDecoder(response.Body).Decode(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if apiResponse.ErrCode != 0 {
|
||||
return nil, errs.NewCodeError(apiResponse.ErrCode, apiResponse.ErrMsg)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
resp, err := ApiCall[auth.GetAdminTokenResp](getAdminToken, &auth.GetAdminTokenReq{
|
||||
Secret: "openIM123",
|
||||
UserID: "imAdmin",
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("get admin token failed", err)
|
||||
return
|
||||
}
|
||||
Token = resp.Token
|
||||
g := gin.Default()
|
||||
g.POST("/callbackExample/callbackAfterSendSingleMsgCommand", toGin(handlerUserMsg))
|
||||
if err := g.Run(":10006"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func toGin[R any](fn func(c *gin.Context, req *R) error) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Printf("HTTP %s %s %s\n", c.Request.Method, c.Request.URL, body)
|
||||
var req R
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if err := fn(c, &req); err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, "{}")
|
||||
}
|
||||
}
|
||||
|
||||
func handlerUserMsg(c *gin.Context, req *cbapi.CallbackAfterSendSingleMsgReq) error {
|
||||
if req.ContentType != constant.Text {
|
||||
return nil
|
||||
}
|
||||
if !strings.Contains(req.Content, "stream") {
|
||||
return nil
|
||||
}
|
||||
apiReq := apistruct.SendMsgReq{
|
||||
RecvID: req.SendID,
|
||||
SendMsg: apistruct.SendMsg{
|
||||
SendID: req.RecvID,
|
||||
SenderNickname: "xxx",
|
||||
SenderFaceURL: "",
|
||||
SenderPlatformID: constant.AdminPlatformID,
|
||||
ContentType: constant.Stream,
|
||||
SessionType: req.SessionType,
|
||||
SendTime: time.Now().UnixMilli(),
|
||||
Content: map[string]any{
|
||||
"type": "xxx",
|
||||
"content": "server test stream msg",
|
||||
},
|
||||
},
|
||||
}
|
||||
go func() {
|
||||
if err := doPushStreamMsg(&apiReq); err != nil {
|
||||
fmt.Println("doPushStreamMsg failed", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("doPushStreamMsg success")
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func doPushStreamMsg(sendReq *apistruct.SendMsgReq) error {
|
||||
resp, err := ApiCall[msg.SendMsgResp](sendMsgApi, sendReq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
const num = 5
|
||||
for i := 1; i <= num; i++ {
|
||||
_, err := ApiCall[msg.AppendStreamMsgResp](appendStreamMsg, &msg.AppendStreamMsgReq{
|
||||
ClientMsgID: resp.ClientMsgID,
|
||||
StartIndex: int64(i - 1),
|
||||
Packets: []string{
|
||||
fmt.Sprintf("stream_msg_packet_%03d", i),
|
||||
},
|
||||
End: i == num,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("append stream msg failed", "clientMsgID", resp.ClientMsgID, "index", fmt.Sprintf("%d/%d", i, num), "error", err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("append stream msg success", "clientMsgID", resp.ClientMsgID, "index", fmt.Sprintf("%d/%d", i, num))
|
||||
time.Sleep(time.Second * 10)
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in new issue