package speechNlu import ( "encoding/json" "errors" "fmt" "regexp" "speech-nlu-parse/global" "speech-nlu-parse/model" "speech-nlu-parse/pkg/logger" "speech-nlu-parse/service/connect" "strconv" "strings" ) const ( speech_nlu_parse = "speech_nlu_parse" ) //type DomainParams struct { // SpeechWsResp *SpeechWsResp // Query string // RequestId string // Mac string // Domain string // Mid string // MidType string // SessionId string // HomeId string // AppKey string //} // 屏蔽处理 func shieldedDomain() []byte { // 暂不支持此功能 resultTextStr := shielded_reply return replyWithChat(resultTextStr, "chat.chat") } func baseParse(params *model.SpeechDomainParams) (*model.ResponseBody, error) { if !params.CheckDm() || !params.CheckWidget() { return nil, errors.New("SpeechNlpResp check error.") } var result model.ResponseBody result.Header.Semantic.Code = 0 //result.Header.Semantic.Domain = "joke" //result.Header.Semantic.Intent = "tell" result.Header.Semantic.Msg = speech_nlu_parse result.Header.Semantic.SessionComplete = params.SpeechWsResp.Dm.ShouldEndSession //params.SpeechWsResp.Dm.ShouldEndSession //result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent result.ResponseText = params.SpeechWsResp.Dm.Nlg result.AsrRecongize = params.SpeechWsResp.Dm.Input //result.ListItems = make([]map[string]interface{}, 0) if !params.SpeechWsResp.Dm.ShouldEndSession { fmt.Println("保存:", params.SpeechWsResp.SessionId) UpdateSession(params.Mac, params.SpeechWsResp.SessionId) } return &result, nil } // 待更新带屏设备部分 func weatherDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("weatherDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "weather" res.Header.Semantic.Intent = "general_search" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent //todo:带屏设备的待改 return Marshal(params, res) } // 假期 speech 返回是日历 func calendarDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("calendarDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "holiday" res.Header.Semantic.Intent = "search_dateDiff" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent if params.SpeechWsResp.Dm.IntentName == "查询历史事件" { res.Header.Semantic.Domain = "chat" res.Header.Semantic.Intent = "chat" res.Header.Semantic.SkillId = "history.search_today" if !params.CheckWidResult() { if res.ResponseText != "" { return replyWithChat(res.ResponseText, "doudi") } } else { sliceData, isSlice := params.SpeechWsResp.Dm.Widget.Extra.Result.([]interface{}) if !isSlice { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("params.SpeechWsResp.Dm.Widget.Extra.Result不是数组类型") if res.ResponseText != "" { return replyWithChat(res.ResponseText, "doudi") } return replyWithChat(error_reply, "doudi") } var l int var resStrb string if len(sliceData) < global.GetLimitedSetting(params.Mid).HistoryNum { l = len(sliceData) } else { // 使用限制的条数 l = global.GetLimitedSetting(params.Mid).HistoryNum } if len(sliceData) > l { sliceData = sliceData[:l] } for _, item := range sliceData { itemBytes, _ := json.Marshal(item) var con model.Result if err := json.Unmarshal(itemBytes, &con); err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Errorf("json.Unmarshal元素解析失败: %v", err) return replyWithChat(error_reply, "doudi") } resStrb += fmt.Sprintf("%s", con.Title) } res.ResponseText = resStrb } } return Marshal(params, res) } func chatDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("chatDomain baseParse error.") return replyWithChat(error_reply, "doudi") } //res.Header.Semantic.SessionComplete = true res.Header.Semantic.Domain = "chat" res.Header.Semantic.Intent = "chat" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent return Marshal(params, res) } // 目前无返回 func newsDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("newsDomain baseParse error.") return replyWithChat(error_reply, "doudi") } return Marshal(params, res) } func ancientpoemDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("ancientpoemDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "ancient_poem" res.Header.Semantic.Intent = "search_tangshi" if params.SpeechWsResp.Dm.IntentName == "查询诗词释义" { res.Header.Semantic.Intent = "search_ancientpoem_meaning" res.ListItems = append(res.ListItems, map[string]interface{}{ "content": params.SpeechWsResp.Dm.Widget.Extra.ContentTranslation, "title": params.SpeechWsResp.Dm.Widget.Extra.Title, "type": params.SpeechWsResp.Dm.Widget.Type, //原来是 TEXT,看看是否能播放 "url": "", "mediaId": "", }) res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent return Marshal(params, res) } res.ListItems = make([]map[string]interface{}, 0) sliceData, isSlice := params.SpeechWsResp.Dm.Widget.Content.([]interface{}) if !isSlice { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("params.SpeechWsResp.Dm.Widget.Content不是数组类型") return replyWithChat(error_reply, "doudi") } else { for _, item := range sliceData { itemBytes, _ := json.Marshal(item) var con model.Content if err := json.Unmarshal(itemBytes, &con); err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Errorf("json.Unmarshal元素解析失败: %v", err) return replyWithChat(error_reply, "doudi") } res.ListItems = append(res.ListItems, map[string]interface{}{ "url": con.LinkUrl, "title": con.Title, "content": con.Text, "author": con.Author, "dynasty": con.Extra.Dynasty, "mediaId": "", "type": params.SpeechWsResp.Dm.Widget.Type, //原来是 AUDIO,看看是否能播放 }) } } //if len(params.SpeechWsResp.Dm.Widget.Content) > 0 { // for i := 0; i < len(params.SpeechWsResp.Dm.Widget.Content); i++ { // res.ListItems = append(res.ListItems, map[string]interface{}{ // "url": params.SpeechWsResp.Dm.Widget.Content[i].LinkUrl, // "title": params.SpeechWsResp.Dm.Widget.Content[i].Title, // "content": params.SpeechWsResp.Dm.Widget.Content[i].Text, // "author": params.SpeechWsResp.Dm.Widget.Content[i].Author, // "dynasty": params.SpeechWsResp.Dm.Widget.Content[i].Extra.Dynasty, // "mediaId": "", // "type": params.SpeechWsResp.Dm.Widget.Type, //原来是 AUDIO,看看是否能播放 // //思必驰的没有回复话术 // //"content": params.Dm.Widget.Content[i].TextContent, // }) // } //} res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent return Marshal(params, res) } func jokeDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("jokeDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "joke" res.Header.Semantic.Intent = "tell" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent res.ListItems = make([]map[string]interface{}, 0) sliceData, isSlice := params.SpeechWsResp.Dm.Widget.Content.([]interface{}) if !isSlice { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("params.SpeechWsResp.Dm.Widget.Content不是数组类型") return replyWithChat(error_reply, "doudi") } for _, item := range sliceData { itemBytes, _ := json.Marshal(item) var con model.Content if err := json.Unmarshal(itemBytes, &con); err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Errorf("json.Unmarshal元素解析失败: %v", err) return replyWithChat(error_reply, "doudi") } res.ListItems = append(res.ListItems, map[string]interface{}{ "url": con.LinkUrl, "title": con.Title, //思必驰的没有回复话术 //"content": params.Dm.Widget.Content[i].TextContent, }) } return Marshal(params, res) } func astroDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("astroDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "astro" res.Header.Semantic.Intent = "search_info" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent return Marshal(params, res) } func translateDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("translateDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "translate" res.Header.Semantic.Intent = "translate" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent return Marshal(params, res) } func alarmDomain(params *model.SpeechDomainParams) []byte { var result model.ResponseBody result.Header.Semantic.Code = 0 result.Header.Semantic.Msg = speech_nlu_parse result.AsrRecongize = params.SpeechWsResp.Dm.Input result.Header.Semantic.Domain = "alarm" result.Header.Semantic.SessionComplete = params.SpeechWsResp.Dm.ShouldEndSession //params.SpeechWsResp.Dm.ShouldEndSession result.ResponseText = params.SpeechWsResp.Dm.Nlg if !params.SpeechWsResp.Dm.ShouldEndSession { fmt.Println("保存:", params.SpeechWsResp.SessionId) UpdateSession(params.Mac, params.SpeechWsResp.SessionId) return Marshal(params, &result) } fmt.Println("1111111111111111111111111") fmt.Println(params.SpeechWsResp.SessionId) if !params.CheckDm() || !params.CheckCommandParam() { return replyWithChat(error_reply, "doudi") } api := params.SpeechWsResp.Dm.Command.Api extra := params.SpeechWsResp.Dm.Command.Param.Extra object := params.SpeechWsResp.Dm.Command.Param.Object if object == "闹钟" { result.Header.Semantic.Domain = "alarm" } else { result.Header.Semantic.Domain = "reminder_v2" } if api == "ai.dui.dskdm.reminder.query" { result.Header.Semantic.Intent = "check" result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent } else if api == "ai.dui.dskdm.reminder.insert" { result.Header.Semantic.Intent = "new" result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent resAlarm, err := connect.AlarmGrpc(params, api, extra, object) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Errorf("connect.AlarmGrpc error:%v", err) return replyWithChat(error_reply, "doudi") } if resAlarm.GetStatus().GetCode() == 0 { return Marshal(params, &result) } else { resultTextStr := "网络不稳定,请稍后再试" return replyWithChat(resultTextStr, "chat.chat") } } else if api == "ai.dui.dskdm.reminder.remove" { result.Header.Semantic.Intent = "delete" result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent resAlarm, err := connect.AlarmGrpc(params, api, extra, object) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Errorf("connect.AlarmGrpc error:%v", err) return replyWithChat(error_reply, "doudi") } if resAlarm.GetStatus().GetCode() == 0 { return Marshal(params, &result) } else { resultTextStr := "网络不稳定,请稍后再试" return replyWithChat(resultTextStr, "chat.chat") } } // //var result model.ResponseBody //result.Header.Semantic.Code = 0 //result.Header.Semantic.Msg = speech_nlu_parse //result.Header.Semantic.SessionComplete = params.SpeechWsResp.Dm.ShouldEndSession ////result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent //result.AsrRecongize = params.SpeechWsResp.Dm.Input // //var alarmDBData model.AlarmRemindInfo //alarmDBData.Mac = params.Mac //alarmDBData.Note = "" //alarmDBData.URL = "" //alarmDBData.Speech_Type = onlyTTS //alarmDBData.Createtime = time.Now().String()[:19] ////items := "" //contentTTS := "" // //if params.SpeechWsResp.Dm.Param.Object == "闹钟" { // alarmDBData.E_type = alarm // result.Header.Semantic.Domain = "alarm" //} else { // alarmDBData.E_type = remind // result.Header.Semantic.Domain = "reminder_v2" // if !params.CheckEvent() { // contentTTS = alarm_remind_common + params.SpeechWsResp.Dm.Param.Event // } else { // contentTTS = alarm_remind_raw // } // alarmDBData.Note = contentTTS //} //fmt.Println(contentTTS) // //if params.SpeechWsResp.Dm.Api == "DUI.Alerts.SetAlert" { // result.Header.Semantic.Intent = "new" // if !params.CheckDate() { // global.Logger.WithFields(logger.Fields{ // "requestId": params.RequestId, // "mac": params.Mac, // "mid": params.Mid, // "vender": params.MidType}).Error("params.CheckDate() error") // return replyWithChat(error_reply, "doudi") // } else { // //如果日期存在 // t, err := time.Parse("20060102", params.SpeechWsResp.Dm.Param.Date) // if err != nil { // global.Logger.WithFields(logger.Fields{ // "requestId": params.RequestId, // "mac": params.Mac, // "mid": params.Mid, // "vender": params.MidType}).Error(err.Error()) // return replyWithChat(error_reply, "doudi") // } // // // 将日期转换为目标格式 // formatted := t.Format("2006-01-02") // alarmDBData.E_date = formatted // //获得时间 // alarmDBData.E_time = params.SpeechWsResp.Dm.Param.Time // } // // if !params.CheckRepeat() { // alarmDBData.Repeat_type = 1 // } else if params.SpeechWsResp.Dm.Param.Repeat == "W1W2W3W4W5W6W7" { // alarmDBData.Repeat_type = 2 // } else if params.SpeechWsResp.Dm.Param.Repeat == "W1W2W3W4W5" { // alarmDBData.Repeat_type = 5 // } else if strings.Contains(strings.ToLower(params.SpeechWsResp.Dm.Param.Repeat), "M") { // alarmDBData.Repeat_type = 4 // } else if strings.Contains(strings.ToLower(params.SpeechWsResp.Dm.Param.Repeat), "W") { // alarmDBData.Repeat_type = 3 // //每周几待写 // } // dao.SaveAlarmRemindData(alarmDBData) // if params.SpeechWsResp.Dm.Nlg != "" { // result.ResponseText = params.SpeechWsResp.Dm.Nlg // } else { // result.ResponseText = "已经设置好了" // } // //} else if params.SpeechWsResp.Dm.Api == "DUI.Alerts.QueryAlert" { // result.Header.Semantic.Intent = "check" // dao.DeleteOverdueAlarmRemindData(mac) // //} else if params.SpeechWsResp.Dm.Api == "DUI.Alerts.DeleteAlert" { // result.Header.Semantic.Intent = "delete" //} // //result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent // //if !alarmDevFilter(params) { // return Marshal(params, &result) //} // //return Marshal(params, &result) return Marshal(params, &result) } func scienceDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("scienceDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "science" res.Header.Semantic.Intent = "calculator" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent return Marshal(params, res) } func chengyuDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("chengyuDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "chat" //成语原来腾讯的返回的就是chat res.Header.Semantic.Intent = "chat" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent return Marshal(params, res) } func baikeDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("baikeDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "baike" //成语原来腾讯的返回的就是chat res.Header.Semantic.Intent = "search_baike" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent return Marshal(params, res) } func fmDomain(params *model.SpeechDomainParams) []byte { res, err := baseParse(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("fmDomain baseParse error.") return replyWithChat(error_reply, "doudi") } res.Header.Semantic.Domain = "fm" res.Header.Semantic.Intent = "play_radio" res.Header.Semantic.SkillId = res.Header.Semantic.Domain + "." + res.Header.Semantic.Intent res.ListItems = make([]map[string]interface{}, 0) sliceData, isSlice := params.SpeechWsResp.Dm.Widget.Content.([]interface{}) if !isSlice { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Error("params.SpeechWsResp.Dm.Widget.Content不是数组类型") if res.ResponseText != "" { return replyWithChat(res.ResponseText, "doudi") } return replyWithChat(error_reply, "doudi") } for _, item := range sliceData { itemBytes, _ := json.Marshal(item) var con model.Content if err := json.Unmarshal(itemBytes, &con); err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Errorf("json.Unmarshal元素解析失败: %v", err) return replyWithChat(error_reply, "doudi") } res.ListItems = append(res.ListItems, map[string]interface{}{ "url": con.LinkUrl, "title": con.Title, }) } //if len(params.SpeechWsResp.Dm.Widget.Content) > 0 { // for i := 0; i < len(params.SpeechWsResp.Dm.Widget.Content); i++ { // res.ListItems = append(res.ListItems, map[string]interface{}{ // "url": params.SpeechWsResp.Dm.Widget.Content[i].LinkUrl, // "title": params.SpeechWsResp.Dm.Widget.Content[i].Title, // //思必驰的没有回复话术 // //"content": params.Dm.Widget.Content[i].TextContent, // }) // } //} return Marshal(params, res) } func globalCtrlDomain(params *model.SpeechDomainParams) []byte { query := params.SpeechWsResp.Dm.Input if !params.CheckCommandParam() { return replyWithChat(error_reply, "doudi") } else { if params.SpeechWsResp.Dm.Command.Param.Volume != "" && params.SpeechWsResp.Dm.Command.Api == "DUI.MediaController.SetVolume" { if strings.Contains(params.SpeechWsResp.Dm.Command.Param.Volume, "-") || strings.Contains(params.SpeechWsResp.Dm.Command.Param.Volume, "+") { pattern := `([+-]?)(\d+)` // 分组1:符号(+/-),分组2:数字 re := regexp.MustCompile(pattern) // 查找所有匹配项 matches := re.FindAllStringSubmatch(params.SpeechWsResp.Dm.Command.Param.Volume, -1) var sign, numStr, action string for _, match := range matches { sign = match[1] // 符号部分(可能为空) numStr = match[2] // 数字部分 fmt.Printf("符号: [%s], 数字: %s\n", sign, numStr) } semanticParams := make(map[string]interface{}) if numStr != "" { semanticParams["degree"] = model.ParamsStr{Origin: numStr, Norm: numStr, Code: 0} } if sign == "-" { action = "control_reduceVol" } else { action = "control_riseVol" } return transformGreeProtocolReply(query, "UniversalControl", action, &semanticParams) } else if params.SpeechWsResp.Dm.Command.Param.Volume == "max" { return transformGreeProtocolReply(query, "UniversalControl", "control_riseVolMax", nil) } else if params.SpeechWsResp.Dm.Command.Param.Volume == "min" { return transformGreeProtocolReply(query, "UniversalControl", "control_reduceVolMin", nil) } else { semanticParams := make(map[string]interface{}) // 百分数 if strings.Contains(params.SpeechWsResp.Dm.Command.Param.Volume, "%") { parts := strings.SplitN(params.SpeechWsResp.Dm.Command.Param.Volume, "%", 2) semanticParams["degree"] = model.ParamsStr{Origin: parts[0], Norm: parts[0], Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_setVol", &semanticParams) } // 分数 if strings.Contains(params.SpeechWsResp.Dm.Command.Param.Volume, "/") { num, err := fractionToPercent(params.SpeechWsResp.Dm.Command.Param.Volume) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Errorf("fractionToPercent error: %v", err) return replyWithChat(error_reply, "doudi") } semanticParams["degree"] = model.ParamsStr{Origin: strconv.Itoa(num), Norm: strconv.Itoa(num), Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_setVol", &semanticParams) } semanticParams["degree"] = model.ParamsStr{Origin: params.SpeechWsResp.Dm.Command.Param.Volume, Norm: params.SpeechWsResp.Dm.Command.Param.Volume, Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_setVol", &semanticParams) } } else if params.SpeechWsResp.Dm.Command.Api == "DUI.System.Sounds.OpenMode" && params.SpeechWsResp.Dm.Command.Param.Mode == "静音模式" { return transformGreeProtocolReply(query, "SystemControl", "control_volumeOff", nil) } else if params.SpeechWsResp.Dm.Command.Api == "DUI.System.Sounds.CloseMode" && params.SpeechWsResp.Dm.Command.Param.Mode == "静音模式" { return transformGreeProtocolReply(query, "SystemControl", "control_volumeOn", nil) } } return replyWithChat(error_reply, "doudi") } func playCtrlDomain(params *model.SpeechDomainParams) []byte { query := params.SpeechWsResp.Dm.Input if !params.CheckDmParam() { return replyWithChat(error_reply, "doudi") } else { if params.SpeechWsResp.Dm.Param.Operate != "" { switch params.SpeechWsResp.Dm.Param.Operate { case "暂停": return transformGreeProtocolReply(query, "PlayControl", "control_pause", nil) case "继续": return transformGreeProtocolReply(query, "PlayControl", "control_resume", nil) case "停止": return transformGreeProtocolReply(query, "PlayControl", "control_stop", nil) } } else if params.SpeechWsResp.Dm.Param.Number != "" { switch params.SpeechWsResp.Dm.Param.Number { case "+1": return transformGreeProtocolReply(query, "UniversalControl", "control_next", nil) case "-1": return transformGreeProtocolReply(query, "UniversalControl", "control_previous", nil) } } } return replyWithChat(error_reply, "doudi") } func musicDomain(params *model.SpeechDomainParams) []byte { list, err := connect.MusicGrpc(params) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Errorf("connect.MusicGrpc error:%v", err) return replyWithChat(error_reply, "doudi") } var result model.ResponseBody result.Header.Semantic.Code = 0 result.Header.Semantic.Domain = "music" result.Header.Semantic.Intent = "play" result.Header.Semantic.Msg = speech_nlu_parse result.Header.Semantic.SessionComplete = params.SpeechWsResp.Dm.ShouldEndSession result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent //result.ResponseText = "以下音乐由网易云提供:" //params.SpeechWsResp.Dm.Nlg result.AsrRecongize = params.SpeechWsResp.Dm.Input result.ListItems = make([]map[string]interface{}, 0) if len(list.Data.ListItems) > 0 { for i := 0; i < len(list.Data.ListItems); i++ { result.ListItems = append(result.ListItems, map[string]interface{}{ "mediaId": list.Data.ListItems[i].MediaId, "singer": list.Data.ListItems[i].Singer, "song": list.Data.ListItems[i].Song, }) } result.ResponseText = "以下音乐由网易云提供:" //params.SpeechWsResp.Dm.Nlg } else { return otherDomain(params) } return Marshal(params, &result) } func financeDomain(params *model.SpeechDomainParams) []byte { if !params.CheckDm() { return replyWithChat(error_reply, "doudi") } var result model.ResponseBody result.Header.Semantic.Code = 0 result.Header.Semantic.Domain = "finance" result.Header.Semantic.Intent = "exchangecalculate" result.Header.Semantic.Msg = speech_nlu_parse result.Header.Semantic.SessionComplete = params.SpeechWsResp.Dm.ShouldEndSession //params.SpeechWsResp.Dm.ShouldEndSession result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent result.ResponseText = params.SpeechWsResp.Dm.Nlg result.AsrRecongize = params.SpeechWsResp.Dm.Input return Marshal(params, &result) } func convertDomain(params *model.SpeechDomainParams) []byte { if !params.CheckDm() { return replyWithChat(error_reply, "doudi") } var result model.ResponseBody result.Header.Semantic.Code = 0 result.Header.Semantic.Domain = "science" result.Header.Semantic.Intent = "unit_convert" result.Header.Semantic.Msg = speech_nlu_parse result.Header.Semantic.SessionComplete = params.SpeechWsResp.Dm.ShouldEndSession //params.SpeechWsResp.Dm.ShouldEndSession result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent result.ResponseText = params.SpeechWsResp.Dm.Nlg result.AsrRecongize = params.SpeechWsResp.Dm.Input return Marshal(params, &result) } func holidayDomain(params *model.SpeechDomainParams) []byte { if !params.CheckDm() { return replyWithChat(error_reply, "doudi") } var result model.ResponseBody result.Header.Semantic.Code = 0 result.Header.Semantic.Domain = "holiday" result.Header.Semantic.Intent = "search_holiday" result.Header.Semantic.Msg = speech_nlu_parse result.Header.Semantic.SessionComplete = params.SpeechWsResp.Dm.ShouldEndSession //params.SpeechWsResp.Dm.ShouldEndSession result.Header.Semantic.SkillId = result.Header.Semantic.Domain + "." + result.Header.Semantic.Intent result.ResponseText = params.SpeechWsResp.Dm.Nlg result.AsrRecongize = params.SpeechWsResp.Dm.Input return Marshal(params, &result) } func helpDomain(params *model.SpeechDomainParams) []byte { resultTextStr := "我可以控制空调开机、播放音乐、查询时间天气、调节温度,设置模式,例如您可以对我说,空调开机。" return replyWithChat(resultTextStr, "chat.chat") } func Marshal(params *model.SpeechDomainParams, res *model.ResponseBody) []byte { if res.Header.Semantic.Domain == "" { res.Header.Semantic.Domain = "chat" } if res.Header.Semantic.Intent == "" { res.Header.Semantic.Domain = "chat" } resByte, err := json.Marshal(res) if err != nil { global.Logger.WithFields(logger.Fields{ "requestId": params.RequestId, "mac": params.Mac, "mid": params.Mid, "vender": params.MidType, }).Errorf("Marshal error. %v", err) return replyWithChat(error_reply, "doudi") } return resByte } func otherDomain(params *model.SpeechDomainParams) []byte { resultText := reply_when_tencent_empty return replyWithChat(resultText, "chat.chat") } func fractionToPercent(fraction string) (int, error) { // 1. 按 "/" 分割字符串 parts := strings.Split(fraction, "/") if len(parts) != 2 { return 0, fmt.Errorf("invalid fraction format: %s", fraction) } // 2. 转换分子和分母为整数 numerator, err := strconv.Atoi(parts[0]) if err != nil { return 0, fmt.Errorf("invalid numerator: %s", parts[0]) } denominator, err := strconv.Atoi(parts[1]) if err != nil { return 0, fmt.Errorf("invalid denominator: %s", parts[1]) } // 3. 检查分母是否为0 if denominator == 0 { return 0, fmt.Errorf("denominator cannot be zero") } // 4. 计算百分比并四舍五入取整 percent := float64(numerator) / float64(denominator) * 100 return int(percent + 0.5), nil // 四舍五入 }