package controller import ( "fmt" "github.com/gin-gonic/gin" "kefu/lib" "kefu/models" "kefu/tools" "kefu/types" "kefu/ws" "log" "strings" "time" ) // 钉钉机器人消息 func PostDingRobotMessage(c *gin.Context) { var jsonData map[string]interface{} if err := c.ShouldBindJSON(&jsonData); err != nil { // 发生错误,返回错误响应 c.JSON(200, gin.H{"error": err.Error()}) return } log.Println("ding robot", jsonData) entId := c.Param("entId") senderStaffId := jsonData["senderStaffId"].(string) nickName := jsonData["senderNick"].(string) conversationType := jsonData["conversationType"].(string) conversationId := jsonData["conversationId"].(string) avatar := "/static/images/dingding.png" content := jsonData["text"].(map[string]interface{})["content"].(string) if content == "" { return } kefuInfo := models.FindUserByUid(entId) if kefuInfo.ID == 0 { c.JSON(200, gin.H{ "code": types.ApiCode.ACCOUNT_NO_EXIST, "msg": types.ApiCode.GetMessage(types.ApiCode.ACCOUNT_NO_EXIST), }) return } content, _ = ReplaceBlackWords(content) content = strings.TrimSpace(content) //查找访客插入访客表 visitorId := fmt.Sprintf("ding|%s|%s", entId, senderStaffId) vistorInfo := models.FindVisitorByVistorId(visitorId) if vistorInfo.ID == 0 { visitorName := nickName avator := avatar if visitorName == "" { visitorName = "钉钉用户" } if avator == "" { avator = "/static/images/dingding.png" } vistorInfo = models.Visitor{ Model: models.Model{ CreatedAt: time.Now(), UpdatedAt: time.Now(), }, Name: visitorName, Avator: avator, ToId: kefuInfo.Name, VisitorId: visitorId, Status: 1, Refer: "来自钉钉", EntId: entId, VisitNum: 0, City: "钉钉用户", ClientIp: c.ClientIP(), } vistorInfo.AddVisitor() } else { go models.UpdateVisitorStatus(visitorId, 3) } go ws.VisitorOnline(kefuInfo.Name, vistorInfo) go ws.VisitorSendToKefu(kefuInfo.Name, vistorInfo, content) result := "" dingConfig := models.GetEntConfigsMap(entId, "DingAppKey", "DingAppSecret", "DingRobotCode", "QdrantAIStatus") if dingConfig["DingAppKey"] == "" || dingConfig["DingAppSecret"] == "" || dingConfig["DingRobotCode"] == "" { c.JSON(200, gin.H{"error": "钉钉服务未配置!"}) return } //调用自动回复 result = GetLearnReplyContent(entId, content) if result == "" && dingConfig["QdrantAIStatus"] == "true" { //调用GPT3.5 result = ws.Gpt3Knowledge(entId, visitorId, kefuInfo, content) } result = tools.TrimHtml(result) if result == "" { c.JSON(200, gin.H{"error": "回复内容为空!"}) return } models.CreateMessage(kefuInfo.Name, visitorId, result, "kefu", entId, "read") go ws.KefuMessage(visitorId, result, kefuInfo) //调用钉钉 ding, err := lib.NewDing(dingConfig["DingAppKey"], dingConfig["DingAppSecret"]) if err != nil { log.Println(err) } else { //userid uids := []string{ senderStaffId, } //robotCode robotCode := dingConfig["DingRobotCode"] if conversationType == "2" { go ding.SendGroup(conversationId, robotCode, fmt.Sprintf("@%s %s", nickName, result)) } else { go ding.BatchSend(robotCode, uids, result) } } c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": gin.H{ "content": result, }, }) }