feat(1.0): 新增用户发送消息
This commit is contained in:
parent
c8ebd6e7de
commit
1a857a6794
@ -1,7 +1,10 @@
|
|||||||
package message
|
package message
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"mini-chat/internal/services"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -63,6 +66,61 @@ func (h *handler) UserSendMessage() core.HandlerFunc {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time.AfterFunc(1*time.Second, func() {
|
||||||
|
if req.MsgType == 1 { // 自动回复逻辑
|
||||||
|
textMsg := new(services.TextMessage)
|
||||||
|
if err := json.Unmarshal([]byte(req.Content), &textMsg); err != nil {
|
||||||
|
h.logger.Error(fmt.Sprintf("AppID(%s),用户ID(%s),发送内容(%s) JSON解析失败: %s", req.AppID, req.FormUserID, req.Content, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
keyword, err := h.readDB.AppKeyword.
|
||||||
|
Where(h.readDB.AppKeyword.AppID.Eq(req.AppID)).
|
||||||
|
Where(h.readDB.AppKeyword.Keyword.Eq(textMsg.Message)).
|
||||||
|
Order(h.readDB.AppKeyword.ID.Asc()).
|
||||||
|
First()
|
||||||
|
if err != nil && err != gorm.ErrRecordNotFound {
|
||||||
|
h.logger.Error(fmt.Sprintf("AppID(%s),用户ID(%s),发送内容(%s) 获取意图关键字失败: %s", req.AppID, req.FormUserID, req.Content, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if keyword == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取群组关键字回复信息
|
||||||
|
reply, err := h.readDB.AppKeywordReply.
|
||||||
|
Where(h.readDB.AppKeywordReply.AppID.Eq(req.AppID)).
|
||||||
|
Where(h.readDB.AppKeywordReply.KeywordID.Eq(keyword.ID)).
|
||||||
|
Find()
|
||||||
|
if err != nil && err != gorm.ErrRecordNotFound {
|
||||||
|
h.logger.Error(fmt.Sprintf("AppID(%s),用户ID(%s),发送内容(%s) 获取群组关键字回复失败: %s", req.AppID, req.FormUserID, req.Content, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(reply) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range reply {
|
||||||
|
time.Sleep(time.Duration(v.IntervalSeconds) * time.Second)
|
||||||
|
|
||||||
|
replyData := new(model.AppMessageLog)
|
||||||
|
replyData.AppID = req.AppID
|
||||||
|
replyData.SenderID = "888888"
|
||||||
|
replyData.SenderName = "平台"
|
||||||
|
replyData.Content = v.Content
|
||||||
|
replyData.ReceiverID = req.FormUserID
|
||||||
|
replyData.MsgType = v.Type
|
||||||
|
replyData.SendTime = time.Now()
|
||||||
|
replyData.CreatedAt = time.Now()
|
||||||
|
if err := h.writeDB.AppMessageLog.Create(replyData); err != nil {
|
||||||
|
h.logger.Error(fmt.Sprintf("AppID(%s),用户ID(%s),发送内容(%s) 回复关键字回复失败: %s", req.AppID, req.FormUserID, req.Content, err.Error()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
res.Message = "操作成功"
|
res.Message = "操作成功"
|
||||||
ctx.Payload(res)
|
ctx.Payload(res)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user