package tencentNlu import ( "encoding/json" "fmt" "regexp" "speech-nlu-parse/dao" "speech-nlu-parse/global" "speech-nlu-parse/model" "speech-nlu-parse/pkg/logger" "strconv" "strings" "time" "github.com/tidwall/gjson" ) // TODO: 增对 音频 图文 不同类型进行解析 // zww 2018-11-22 兜底 // func musicDomain(jsonData []byte, authInterface string) ([]byte, []TencentReport) { func musicDomain(params *model.DomainParams) []byte { // 获取参数 jsonData auth var jsonData []byte jsonData = params.JsonData // var auth string tokenSearchResponse := params.TokenSearchResponse query := params.Query Start: header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" responseText := replyMessage[getRandom(len(replyMessage))] if !header.Exists() || !payload.Exists() { items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } domain := gjson.GetBytes(jsonData, "header.semantic.domain").String() intent := gjson.GetBytes(jsonData, "header.semantic.intent").String() // slots := gjson.GetBytes(jsonData, "header.semantic.slots").Array() //resultTexts := gjson.GetBytes(jsonData, "payload.response_text") switch intent { case "play", "play_album", "play_favorite", "choose_song", "play_operation_list", "change_bacth", "play_in_times": resultUrl := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.audio.stream.url") resultSinger := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.selfData.singer") resultSong := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.selfData.song") resultSongId := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.selfData.songId") resultText := gjson.GetBytes(jsonData, "payload.response_text") resultLen := len(resultUrl.Array()) var reportDatas []model.TencentReport var reportData model.TencentReport resultMediaId := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.mediaId") if resultLen != 0 && resultLen == len(resultSinger.Array()) && resultLen == len(resultSong.Array()) { for i := 0; i < resultLen; i++ { if i == 0 { items = `{"url":` + resultUrl.Array()[i].Raw + `,"singer":` + resultSinger.Array()[i].Raw + `,"song":` + resultSong.Array()[i].Raw + `,"mediaId":` + resultMediaId.Array()[i].Raw + `,"songId":` + resultSongId.Array()[i].Raw + `}` } else { items += `,{"url":` + resultUrl.Array()[i].Raw + `,"singer":` + resultSinger.Array()[i].Raw + `,"song":` + resultSong.Array()[i].Raw + `,"mediaId":` + resultMediaId.Array()[i].Raw + `,"songId":` + resultSongId.Array()[i].Raw + `}` } reportData.UserId = "test" reportData.Domain = "music" reportData.Intent = gjson.GetBytes(jsonData, "header.semantic.intent").String() reportData.ResourceId = resultMediaId.Array()[i].String() reportData.DataSource = "..." if reportData.ResourceId != "" { reportDatas = append(reportDatas, reportData) } } } else { // 列表为空时也需要进行回复语提示 // chat回复需修改 if global.GetLimitedSetting(params.Mid).IsMusicRemid && tokenSearchResponse != nil { if tokenSearchResponse.Data.Status == 3 { // 过期提醒 return replyWithChat(expired_reply, domain+"."+intent) } if tokenSearchResponse.Data.Status != 2 { // 未授权提醒 return replyWithChat(not_using_reply, domain+"."+intent) } } return replyWithChat(resultText.String(), domain+"."+intent) // 无法进行验证, 建议domain和intent改成chat // return replyWithHeader(header.Raw, resultText.String()) } response := resultText.String() // 鉴权-qq音乐 // remind := "" if global.GetLimitedSetting(params.Mid).IsMusicRemid && tokenSearchResponse != nil { if tokenSearchResponse.Data.Status == 3 { // 过期提醒 response = expired_reply } else if tokenSearchResponse.Data.Status != 2 { // 未授权提醒 response = not_using_reply } } jsonStr := "" if resultLen > 0 { jsonStr = `{"header":` + header.Raw + `,"response_text":"` + response + `","listItems":[` + items + `]}` return []byte(jsonStr) } else { // chat回复需修改 return replyWithChat(resultText.String(), domain+"."+intent) // 无法进行验证, 建议domain和intent改成chat // return replyWithHeader(header.Raw, response) } case "prev", "previous": // previous 停止维护了 resultUrl := gjson.GetBytes(jsonData, "payload.data.json_template.0.strDestURL") resultStrData := gjson.GetBytes(jsonData, "payload.data.json_template.0.strData") resultData := []byte(resultStrData.String()) resultSinger := gjson.GetBytes(resultData, "person") resultSong := gjson.GetBytes(resultData, "contentName") resultText := gjson.GetBytes(jsonData, "payload.response_text") resultSongId := gjson.GetBytes(resultData, "strId") // 下一首,上一首均没有response_text if resultUrl.Exists() { // 列表可能出现空的问题 var singerStr, songStr, songIdStr string singerStr = `""` songStr = `""` songIdStr = `""` if resultSinger.Exists() { singerStr = resultSinger.Raw } if resultSong.Exists() { songStr = resultSong.Raw } if resultSongId.Exists() { songIdStr = resultSongId.Raw } items = `{"url":` + resultUrl.Raw + `,"singer":` + singerStr + `,"song":` + songStr + `,"songId":` + songIdStr + `}` jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `,"listItems":[` + items + `]}` return []byte(jsonStr) } else { return replyWithChat(resultText.String(), domain+"."+intent) } // return []byte(jsonStr), nil // 禅道 #64 case "pause", "pause_v2": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_PAUSE, nil) case "next": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_NEXT, nil) case "resume": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_RESUME, nil) case "replay": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_REPLAY, nil) case "stop": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_STOP, nil) case "exit": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_EXIT, nil) case "search_cur_song": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_QUERY_SONG, nil) case "ctrl_single_cycle", "ctrl_break_cycle", "ctrl_list_cycle", "ctrl_sequential", "ctrl_shuffle": // intent 为 pause 与 resume, response_text为空字符串 resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` // return []byte(jsonStr), nil return []byte(jsonStr) case "search_singer", "query_song_editions", "search_song", "search_lyrics", "search_song_property", "search_album": resultText := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` // return []byte(jsonStr), nil return []byte(jsonStr) case "search_prev_song": return replyWithChat(shielded_reply, domain+"."+intent) case "pay": // 采用兜底话术 jsonStr := `{"header":` + header.Raw + `,"response_text":"` + responseText + `"}` // return []byte(jsonStr), nil return []byte(jsonStr) // 2022年9月5日11:06:23 特殊处理, 更改"playmore", "dislike" 为 "play" case "playmore": var tempJson map[string]interface{} _ = json.Unmarshal(jsonData, &tempJson) tempJson["header"].(map[string]interface{})["semantic"].(map[string]interface{})["intent"] = "play" jsonData, _ = json.Marshal(tempJson) goto Start case "dislike": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_NEXT, nil) default: // 其他intent会被屏蔽掉,返回不支持的兜底话术 // 2021年7月24日15:42:41 优化兜底处理, 当带有资源列表的但intent字段为空字符串, intent赋值为"play" resultUrl := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.audio.stream.url") if len(resultUrl.Array()) > 0 && intent == "" { // 更改jsonData var tempJson map[string]interface{} _ = json.Unmarshal(jsonData, &tempJson) tempJson["header"].(map[string]interface{})["semantic"].(map[string]interface{})["intent"] = "play" jsonData, _ = json.Marshal(tempJson) goto Start } // 兜底 jsonStr := `{"header":` + header.Raw + `,"response_text":"` + responseText + `"}` // return []byte(jsonStr), nil return []byte(jsonStr) } } // 修复501问题 zww 2018-11-21 // 修复intent为空时的判定问题 zww 2018-11-22 func newsDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" responseText := "暂不支持该功能,我还在努力学习中" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } intent := gjson.GetBytes(jsonData, "header.semantic.intent").String() domain := gjson.GetBytes(jsonData, "header.semantic.domain").String() switch intent { case "search", "playmore", "": resultUrl := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.audio.stream.url") resultFrom := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.selfData.newsFrom") resultType := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.selfData.type") resultText := gjson.GetBytes(jsonData, "payload.response_text") resultTitle := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.title") resultLen := len(resultUrl.Array()) if resultLen != 0 && resultLen == len(resultFrom.Array()) && resultLen == len(resultType.Array()) { for i := 0; i < resultLen; i++ { /* reportData.ResourceId = resultMediaId.Array()[i].String() reportData.DataSource = resultSource.Array()[i].String() reportStatus := semantic.ReportTencentStatus(reportData) if nil == reportStatus { fmt.Println("report error!") } else { fmt.Println(string(reportStatus)) } */ if i == 0 { items = `{"url":` + resultUrl.Array()[i].Raw + `,"newsFrom":` + resultFrom.Array()[i].Raw + `,"newsType":` + resultType.Array()[i].Raw + `,"resultTitle":` + resultTitle.Array()[i].Raw + `}` } else { items += `,{"url":` + resultUrl.Array()[i].Raw + `,"newsFrom":` + resultFrom.Array()[i].Raw + `,"newsType":` + resultType.Array()[i].Raw + `,"resultTitle":` + resultTitle.Array()[i].Raw + `}` } } } else if resultLen == 0 { // 修改: 2021年7月16日15:25:15, 家技部要求以chat进行回复 // jsonStr := `{"header":` + header.Raw + `,"response_text":"` + resultText.Str + `"}` // return []byte(jsonStr) return replyWithChat(resultText.Str, domain) } else { //items = `{"code":501,"errorType":"result length of url/singer/song does not math!"}` items = `{"header":` + header.Raw + `,"response_text":"` + reply_when_tencent_empty + `"}` return []byte(items) } if resultText.Str == "" { responseText = "为你找到以下新闻:" jsonStr := `{"header":` + header.Raw + `,"response_text":"` + responseText + `","listItems":[` + items + `]}` return []byte(jsonStr) } jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `,"listItems":[` + items + `]}` return []byte(jsonStr) case "replay_all", "next", "previous", "pause", "stop", "resume", "replay_present": // next, previous pause resume stop 的response_text为空字符串 resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` return []byte(jsonStr) default: jsonStr := `{"header":` + header.Raw + `,"response_text":"` + responseText + `"}` return []byte(jsonStr) } } // tencent weather domain func weatherDomain(params *model.DomainParams) []byte { jsonData := params.JsonData var test string header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") //植入广告 //ad := "格力空调祝您生活愉快。" tips := "" //温馨提示 if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items := `{"code":500,"errorType":"result header/payload dose not exists!"}` return []byte(items) } //resultText := gjson.GetBytes(jsonData, "payload.response_text") //jsonStr := `{"header":` + header.String() + `,"response_text":"`+ resultText.String() + ad +`"}` resultComplete := gjson.GetBytes(jsonData, "header.semantic.session_complete") resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") weatherIndex := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.selfData.vecCityWeatherInfo.0."+ "stDobbyCurrentWeather.sDWeaIndex").String() if weatherIndex == "3" || weatherIndex == "4" || weatherIndex == "5" || weatherIndex == "6" { tips = weatherReplyMessage[getRandom(len(weatherReplyMessage))] } if resultComplete.String() == "true" { test = resultContent.String() + tips } else { test = resultContent.String() } test = resultContent.Raw jsonStr := "" /* //展厅I-铂II 视频拍摄 if mac == "f4911e56854f" { locationCity := "" slotsNames := gjson.GetBytes(jsonData, "header.semantic.slots.#.name").Array() slotsValues := gjson.GetBytes(jsonData, "header.semantic.slots.#.value").Array() for i := 0; i < len(slotsNames); i++ { if slotsNames[i].Str == "location" { locationCity = slotsValues[i].Str break; } } if locationCity == "珠海" || locationCity == "珠海市" { testForSmartHosuse := "珠海市今天多云,气温26度到33度,空气质量优" jsonStr = `{"header":` + header.Raw + `,"response_text":"`+ testForSmartHosuse +`"}` return []byte(jsonStr) } } */ // 增加带屏设备的输出 if !global.GetLimitedSetting(params.Mid).Memory { listItems := gjson.GetBytes(jsonData, "payload.data.json.listItems") jsonStr = `{"header":` + header.Raw + `,"response_text":` + test + `,"listItems":` + listItems.Raw + `}` return []byte(jsonStr) } else { jsonStr = `{"header":` + header.Raw + `,"response_text":` + test + `}` return []byte(jsonStr) } } // zww 2018-11-21 修复返回url为空的问题 func poemDomain(params *model.DomainParams) []byte { jsonData := params.JsonData query := params.Query header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } // domain := gjson.GetBytes(jsonData, "header.semantic.domain").String() intent := gjson.GetBytes(jsonData, "header.semantic.intent").String() resultType := gjson.GetBytes(jsonData, "payload.data.json.controlInfo.type") resultUrl := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.audio.stream.url") resultTitle := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.title") // 缺少mediaId resultMediaId := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.mediaId") resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.textContent") resultText := gjson.GetBytes(jsonData, "payload.response_text") author := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.selfData.author") dynasty := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.selfData.dynasty") title := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.selfData.title") //if resultText.String() == "" { // resultText = gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") //} resultLen := len(resultUrl.Array()) if resultLen == len(resultTitle.Array()) && resultLen == len(resultContent.Array()) && resultLen == len(author. Array()) { if resultType.String() == "AUDIO" { for i := 0; i < resultLen; i++ { if resultUrl.Array()[i].Str == "" { continue } authorI := "" dynastyI := "" titleI := "" if len(author.Array()) == resultLen && resultLen > 0 { authorI = author.Array()[i].Raw dynastyI = dynasty.Array()[i].Raw titleI = title.Array()[i].Raw } if i == 0 { items = `{"type":` + resultType.Raw + `,"url":` + resultUrl.Array()[i].Raw + `,"title":` + titleI + `,"content":` + resultContent.Array()[i].Raw + `,"author":` + authorI + `,"dynasty":` + dynastyI + `,"mediaId":` + resultMediaId.Array()[i].Raw + `}` } else { if items != "" { items += `,{"type":` + resultType.Raw + `,"url":` + resultUrl.Array()[i].Raw + `,"title":` + titleI + `,"content":` + resultContent.Array()[i].Raw + `,"author":` + authorI + `,"dynasty":` + dynastyI + `,"mediaId":` + resultMediaId.Array()[i].Raw + `}` } else { items = `{"type":` + resultType.Raw + `,"url":` + resultUrl.Array()[i].Raw + `,"title":` + titleI + `,"content":` + resultContent.Array()[i].Raw + `,"author":` + authorI + `,"dynasty":` + dynastyI + `,"mediaId":` + resultMediaId.Array()[i].Raw + `}` } } } } else { for i := 0; i < resultLen; i++ { if resultContent.Array()[i].Str == "" { continue } if i == 0 { items = `{"type":` + resultType.Raw + `,"url":` + resultUrl.Array()[i].Raw + `,"title":` + resultTitle.Array()[i].Raw + `,"content":` + resultContent.Array()[i].Raw + `,"mediaId":` + resultMediaId.Array()[i].Raw + `}` } else { if items != "" { items += `,{"type":` + resultType.Raw + `,"url":` + resultUrl.Array()[i].Raw + `,"title":` + resultTitle.Array()[i].Raw + `,"content":` + resultContent.Array()[i].Raw + `,"mediaId":` + resultMediaId.Array()[i].Raw + `}` } else { items = `{"type":` + resultType.Raw + `,"url":` + resultUrl.Array()[i].Raw + `,"title":` + resultTitle.Array()[i].Raw + `,"content":` + resultContent.Array()[i].Raw + `,"mediaId":` + resultMediaId.Array()[i].Raw + `}` } } } } } else { //items = `{"code":501,"errorType":"result length of url/title/content does not math!"}` items = `{"header":` + header.Raw + `,"response_text":"` + reply_when_tencent_empty + `"}` return []byte(items) } switch intent { // 特殊处理 2022年9月5日11:35:44 对next进行协议转换 case "next": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_NEXT, nil) case "search_ancientpoem_meaning", "search_ancientpoem_appreciation", "search_ancientpoem_chains": var jsonStr string if resultContent.Exists() && len(resultContent.Array()) > 0 { var resText = resultText.String() + resultContent.Array()[0].String() resText = strconv.Quote(resText) jsonStr = `{"header":` + header.Raw + `,"response_text":` + resText + `,"listItems":[` + items + `]}` } else { jsonStr = `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `,"listItems":[` + items + `]}` } fmt.Println("#######################") fmt.Println(jsonStr) return []byte(jsonStr) default: if items == "" { items = `{"type":"TEXT","url":"","title":"","content":"` + "这首诗我还不会,等学会了再来告诉你吧" + `"}` jsonStr := `{"header":` + header.Raw + `,"response_text":"这首诗我还不会,等学会了再来告诉你吧","listItems":[` + items + `]}` return []byte(jsonStr) } var responseText string if resultText.String() == "" && resultContent.Exists() && len(resultContent.Array()) > 0 { responseText = resultContent.Array()[0].Raw } else { responseText = resultText.Raw } jsonStr := `{"header":` + header.Raw + `,"response_text":` + responseText + `,"listItems":[` + items + `]}` return []byte(jsonStr) } } // tencent fm(story) domain ("\"Gjson 会自动删除转义字符) func fmDomain(params *model.DomainParams) []byte { jsonData := params.JsonData query := params.Query Start: header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" responseText := "暂不支持该功能,我还在努力学习中" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } domain := gjson.GetBytes(jsonData, "header.semantic.domain").String() intent := gjson.GetBytes(jsonData, "header.semantic.intent").String() switch intent { case "play_radio", "query_album_update", "play", "search_radio", "search_anchor", "query_cur_play", "change": resultUrl := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.audio.stream.url") resultTitle := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.title") resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.textContent") resultText := gjson.GetBytes(jsonData, "payload.response_text") resultLen := len(resultUrl.Array()) resultMediaId := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.mediaId") eShowType := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.selfData.eShowType") slotsNames := gjson.GetBytes(jsonData, "header.semantic.slots.#.name").Array() slotsValues := gjson.GetBytes(jsonData, "header.semantic.slots.#.value").Array() // ap和儿童空调均无此判断逻辑 for i := 0; i < len(slotsNames); i++ { if slotsNames[i].String() == "album" { // 屏蔽资源 for _, nonexistAlbum := range global.GetLimitedSetting(params.Mid).FmDomainAlbumList { if slotsValues[i].String() == nonexistAlbum { // chat 回复需修改 return replyWithChat("很抱歉,暂时还没有相关资源", "fm") // 已验证, 返回play intent 会继续播放音乐, 应该改成chat // return replyWithHeader(header.Raw, reply_when_recourse_limited) } } } } var responseText string = resultText.Raw var liveUrlFlag bool = false if resultLen != 0 && resultLen == len(resultTitle.Array()) && resultLen == len(resultContent.Array()) { items = "" for i := 0; i < resultLen; i++ { if eShowType.Array()[i].Int() == 1 { // eShowType为1时表示链接为直播类型, 过滤掉 liveUrlFlag = true continue } if len(items) == 0 { items = `{"url":` + resultUrl.Array()[i].Raw + `,"title":` + resultTitle.Array()[i].Raw + `,"content":` + resultContent.Array()[i].Raw + `,"mediaId":` + resultMediaId.Array()[i].Raw + `}` } else { items += `,{"url":` + resultUrl.Array()[i].Raw + `,"title":` + resultTitle.Array()[i].Raw + `,"content":` + resultContent.Array()[i].Raw + `,"mediaId":` + resultMediaId.Array()[i].Raw + `}` } } } else { //items = `{"code":501,"errorType":"result length of url/title/content does not math!"}` // 兜底的domain与儿童空调不一样, 儿童空调返回为闲聊 // 当资源列表为空时, 要以chat进行返回, 否则会出现本机与app返回不一致 // items = `{"header":` + header.Raw + `,"response_text":"` + reply_when_tencent_empty + `"}` //utils.LogErrorToFile(logfileError, tencent_response_error + " fm电台 ") // return []byte(items) response_raw := resultText.Raw response_text := response_raw[1 : len(response_raw)-1] return replyWithChat(response_text, domain+"."+intent) } // 更改直播类型链接的回复语 if liveUrlFlag && items == "" { responseText = "\"小格没有找到相关的资源哦\"" } jsonStr := `{"header":` + header.Raw + `,"response_text":` + responseText + `,"listItems":[` + items + `]}` return []byte(jsonStr) // case "next", "prev": // resultUrl := gjson.GetBytes(jsonData, "payload.data.json_template.0.strDestURL") // resultText := gjson.GetBytes(jsonData, "payload.response_text") // items = `{"url":"` + resultUrl.Str + `"}` // jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `,"listItems":[` + items + `]}` // return []byte(jsonStr) // 2022年9月5日11:51:58 协议转换 next, prev, case "next": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_NEXT, nil) case "prev": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_PREVIOUS, nil) // 禅道 #71 case "pause": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_PAUSE, nil) case "exit": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_EXIT, nil) case "stop": return transformGreeProtocolReply(query, global.DOMAIN_PLAYCONTROL, global.INTENT_CONTROL_STOP, nil) case "resume": resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` return []byte(jsonStr) // playmore case "playmore": // 列表为空 var tempJson map[string]interface{} _ = json.Unmarshal(jsonData, &tempJson) tempJson["header"].(map[string]interface{})["semantic"].(map[string]interface{})["intent"] = "play" jsonData, _ = json.Marshal(tempJson) goto Start default: jsonStr := `{"header":` + header.Raw + `,"response_text":"` + responseText + `"}` return []byte(jsonStr) } } func sportsDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } results := gjson.GetBytes(jsonData, "payload.data.json.listItems").Array() if len(results) == 0 { //items = `{"code":501,"errorType":"result header/payload dose not exists!"}` items = `{"header":` + header.Raw + `,"response_text":"` + reply_when_tencent_empty + `"}` return []byte(items) } resultText := results[0].Get("textContent") //resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` return []byte(jsonStr) } // Tencent joke domain func jokeDomain(params *model.DomainParams) []byte { jsonData := params.JsonData // 修复腾讯返回空资源的问题, testStr为错误示例 // testStr := `{"header":{"semantic":{"code":0,"domain":"joke","intent":"next","msg":"","session_complete":true,"skill_id":"990835372646862848"}},"payload":{"data":{"json":{"controlInfo":{"audioConsole":"true","backgroundImageValid":"true","orientation":"portrait","subTitleSpeak":"true","textSpeak":"true","titleSpeak":"true","type":"NONE","version":"1.0.0"},"listItems":[{"audio":{"stream":{"url":""}},"mediaId":"","title":""}]}},"response_text":"","ret":0}}` // jsonData = []byte(testStr) header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } domainStr := gjson.GetBytes(jsonData, "header.semantic.domain").String() intentStr := gjson.GetBytes(jsonData, "header.semantic.intent").String() typeStr := gjson.GetBytes(jsonData, "payload.data.json.controlInfo.type").String() // 只接受音频类数据 if typeStr != "AUDIO" { return replyWithChat(reply_when_tencent_empty, domainStr+"."+intentStr) } resultText := gjson.GetBytes(jsonData, "payload.response_text") listItems := gjson.GetBytes(jsonData, "payload.data.json.listItems") // 校验listItems 修复因listItems缺少对应的字段导致的错误 if len(listItems.Array()) > 0 { // 校验是否为空列表 // 校验字段是否存在 isUrlExist := gjson.GetBytes([]byte(listItems.Array()[0].Raw), "audio.stream.url").Exists() isTextContentExist := gjson.GetBytes([]byte(listItems.Array()[0].Raw), "textContent").Exists() // 缺少字段, 返回兜底 if !isUrlExist || !isTextContentExist { return replyWithChat(reply_when_tencent_empty, domainStr+"."+intentStr) } } else { // 空列表返回兜底 return replyWithChat(reply_when_tencent_empty, domainStr+"."+intentStr) } resultUrl := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.audio.stream.url") // ap 解析了textContent字段, content字段有换行符 resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.textContent") // 获取长度 resultUrlLength := len(resultUrl.Array()) resultContentLength := len(resultContent.Array()) // 重复请求获取列表策略 //lenth := len(resultUrl.Array()) //if lenth == 1 { // for j := 0; j < 3; j++ { // var dataReq semantic.SemanticReq // dataReq.Query = "下一个笑话" // if j == 0 { // items = `{"url":`+ resultUrl.Array()[0].Raw + `}` // } else { // tencentRespByteData, err := semantic.GetTencentNLUData(&dataReq) // if nil != err { // items += `,{"url":`+ "" + `}` // } else { // resultUrl := gjson.GetBytes(tencentRespByteData,"payload.data.json.listItems.0.audio.stream.url") // items += `,{"url":`+ resultUrl.Raw + `}` // } // } // } //} else { // for i, item := range resultUrl.Array() { // if i == 0 { // items = `{"url":`+ item.Raw + `}` // } else { // items += `,{"url":`+ item.Raw + `}` // } // } //} // 腾讯返回的 resultUrlLength 和 resultContentLength 不一致的情况, 导致数组越界, 缺少字段导致为空 if resultUrlLength != 0 && resultUrlLength == resultContentLength { for i, item := range resultUrl.Array() { var contentStr string contentStr = resultContent.Array()[i].Raw if i == 0 { items = `{"url":` + item.Raw + `,"content":` + contentStr + `}` } else { items += `,{"url":` + item.Raw + `,"content":` + contentStr + `}` } } } else { // 资源列表为空, 或者长度不一致的情况 responseText := resultText.String() if responseText == "" { responseText = reply_when_tencent_empty } return replyWithChat(responseText, domainStr+"."+intentStr) } //resultMediaId := gjson.GetBytes(jsonData,"payload.data.json.sMongoNewId") // resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `,"listItems":[` + items + `]}` return []byte(jsonStr) } // zww func astroDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } results := gjson.GetBytes(jsonData, "payload.data.json.listItems").Array() if len(results) == 0 { //items = `{"code":501,"errorType":"result header/payload dose not exists!"}` items = `{"header":` + header.Raw + `,"response_text":"` + reply_when_tencent_empty + `"}` return []byte(items) } resultText := results[0].Get("textContent") //resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` return []byte(jsonStr) } // Tencent holiday domain func holidayDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultContent.Raw + `}` return []byte(jsonStr) } // Tencent stock domain func stockDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } //resultText := gjson.GetBytes(jsonData, "payload.response_text") resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultContent.Raw + `}` return []byte(jsonStr) } // Tencent translate domain func translateDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") items := "" var response_text string response_text = resultContent.String() // TODO: 针对个别空调特殊处理 slots := gjson.GetBytes(jsonData, "header.semantic.slots").Array() for i := 0; i < len(slots); i++ { if key, ok := slots[i].Map()["name"]; ok && key.String() == "language" { if value, ok := slots[i].Map()["value"]; ok && !(value.String() == "英语" || value.String() == "中文") { response_text = "当前仅支持中英文翻译。" } } } if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } jsonStr := `{"header":` + header.Raw + `,"response_text":"` + response_text + `"}` return []byte(jsonStr) } func soundDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } resultType := gjson.GetBytes(jsonData, "payload.data.json.controlInfo.type") resultText := gjson.GetBytes(jsonData, "payload.response_text").Str resultUrl := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.audio.stream.url") resultTitle := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.title") //durationTime := gjson.GetBytes(jsonData, "payload.data.json.globalInfo.selfData.sDurationTime") for i := 0; i < len(resultUrl.Array()); i++ { if resultUrl.Array()[i].Str == "" { continue } if i == 0 { items = `{"type":` + resultType.Raw + `,"url":` + resultUrl.Array()[i].Raw + `,"title":` + resultTitle.Array()[i].Raw + `}` } else { items += `,{"type":` + resultType.Raw + `,"url":` + resultUrl.Array()[i].Raw + `,"title":` + resultTitle.Array()[i].Raw + `}` } } //if durationTime.Raw != "" { // jsonStr := `{"header":` + header.String() + `,"response_text":`+ resultText.Raw + `,"durationTime":`+ durationTime.Raw + `,"listItems":[` + items + `]}` // return []byte(jsonStr) //} if resultText == "" { listItems := gjson.GetBytes(jsonData, "payload.data.json.listItems").Array() if len(listItems) > 0 { resultText = listItems[0].Get("textContent").Str jsonStr := `{"header":` + header.String() + `,"response_text":"` + resultText + `","listItems":[` + items + `]` return []byte(jsonStr) } else { jsonStr := `{"header":` + header.String() + `,"response_text":"` + reply_when_tencent_empty + `"}` return []byte(jsonStr) } } else { jsonStr := `{"header":` + header.String() + `,"response_text":"` + resultText + `","listItems":[` + items + `]` return []byte(jsonStr) } } // Tencent almanac domain func almanacDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } resultText := gjson.GetBytes(jsonData, "payload.response_text") if resultText.String() == "" { resultText = gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") } if resultText.String() == "" { resultText = gjson.GetBytes(jsonData, "payload.data.json.globalInfo.selfData.noSupport") } jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` return []byte(jsonStr) } // Tencent finance domain func financeDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } resultText := gjson.GetBytes(jsonData, "payload.response_text") if resultText.String() == "" { resultText = gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") } jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` return []byte(jsonStr) } func cityinfoDomain(params *model.DomainParams) []byte { jsonData := params.JsonData domain := params.Domain header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } //resultText := gjson.GetBytes(jsonData, "payload.response_text") resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") // chat回复需修改 jsonStr := replyWithChat(strings.Replace(resultContent.String(), "\n", "", -1), domain) // 已验证, 无法进行正常播报, 建议domain和intent改为chat // jsonStr := replyWithHeader(header.Raw, strings.Replace(resultContent.String(), "\n", "", -1)) //jsonStr := `{"header":` + header.Raw + `,"response_text":`+ resultContent.Raw + `}` return jsonStr } // Tencent general_question_answering domain func generalQADomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") // chat回复需修改 return replyWithChat(resultContent.Str, "general_question_answering") // 已验证, 无法进行正常播报, 建议domain和intent改为chat // return replyWithHeader(header.Raw, resultContent.Str) //jsonStr := `{"header":` + header.Raw + `,"response_text":`+ resultContent.Raw + `}` //return []byte(jsonStr) } // Tencent common_qa domain func commonQADomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") //jsonStr := `{"header":` + header.Raw + `,"response_text":`+ resultContent.Raw + `}` // chat回复需修改 response_text := resultContent.String() response_text = strings.Replace(response_text, "\n", "", -1) return replyWithChat(response_text, "common_qa") // 无法进行验证, 建议domain和intent改成chat // return replyWithHeader(header.Raw, resultContent.Str) /* jsonStr := `{ "header": { "semantic": { "code": 0, "domain": "chat", "intent": "chat", "msg": "", "session_complete": true, "skill_id": "221364_common_qa" } }, "response_text": ` + resultContent.Raw + ` }` return []byte(jsonStr) */ } func baikeDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } var text string textContents := gjson.GetBytes(jsonData, "payload.data.json.listItems.#.textContent") resultContent := gjson.GetBytes(jsonData, "payload.data.json_template.0.strContentData") if len(textContents.Array()) > 0 && textContents.Array()[0].String() != "" { text = textContents.Array()[0].Raw } else { text = resultContent.Raw } //jsonStr := `{"header":` + header.Raw + `,"response_text":`+ test + `,"listItems":`+ resultListItems.Raw +`}` jsonStr := `{"header":` + header.Raw + `,"response_text":` + text + `}` return []byte(jsonStr) } // Tencent chenyu domain func chengyuDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } //resultText := gjson.GetBytes(jsonData, "payload.response_text") resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent") // chat回复需修改 jsonStr := replyWithChat(resultContent.String(), "chengyu") // 已验证, 无法进行正常播报, 建议domain和intent改为chat // jsonStr := replyWithHeader(header.Raw, resultContent.String()) //jsonStr := `{"header":` + header.Raw + `,"response_text":`+ resultContent.Raw + `}` return jsonStr } // Tencent science domain func scienceDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items := `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } intentString := gjson.GetBytes(jsonData, "header.semantic.intent").String() if intentString == "calculator" { resultText := "" calResult := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.selfData.sResult").String() if calResult == "" { resultText = gjson.GetBytes(jsonData, "payload.response_text").String() } else { resultText = "等于" + calResult } jsonStr := `{"header":` + header.Raw + `,"response_text":"` + resultText + `"}` return []byte(jsonStr) } else { resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` return []byte(jsonStr) } } // Tencent recipe domain func recipeDomain(params *model.DomainParams) []byte { // jsonData := params.JsonData // header := gjson.GetBytes(jsonData, "header") //resultText := gjson.GetBytes(jsonData, "payload.response_text") //resultText := "一道好菜很难通过三言两语描述清楚,建议您浏览相关菜谱网站。" // 区别返回, 带屏与非带屏需要做差异化 if global.GetLimitedSetting(params.Mid).Recipe { resultText := recipeReply[getRandom(len(recipeReply))] // chat回复需修改 return replyWithChat(resultText, "recipe") // 已验证, 无法进行正常播报, 建议domain和intent改为chat // return replyWithHeader(header.Raw, resultText) } else { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") resultText := gjson.GetBytes(jsonData, "payload.response_text").String() listItems := gjson.GetBytes(jsonData, "payload.data.json.listItems") jsonStr := `{"header":` + header.Raw + `,"response_text":"` + resultText + `", "listItems":` + listItems.Raw + `}` return []byte(jsonStr) } /* jsonStr := `{"header":` + header.Raw + `,"response_text":"`+ resultText + `"}` return []byte(jsonStr) */ } // Tencent history domain func historyDomain(params *model.DomainParams) []byte { jsonData := params.JsonData domainStr := gjson.GetBytes(jsonData, "header.semantic.domain").String() intentStr := gjson.GetBytes(jsonData, "header.semantic.intent").String() header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") //intent := gjson.GetBytes(jsonData, "header.semantic.intent") items := "" if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items = `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent").String() resStra := strings.Trim(strings.Replace(resultContent, "\n", ",", -1), ",") var resStrb string historyDays := strings.Split(resStra, ",") var l int if len(historyDays) < global.GetLimitedSetting(params.Mid).HistoryNum { l = len(historyDays) } else { // 使用限制的条数 l = global.GetLimitedSetting(params.Mid).HistoryNum } if l < 0 { // 不作限制 resStrb = resStra } else { for i := 0; i < l; i++ { // 进行条数限制, 无屏空调内存受限 resStrb += fmt.Sprintf("%s,", historyDays[i]) } } //strings.LastIndex(resStra, ",") // chat回复需修改 return replyWithChat(resStrb, domainStr+"."+intentStr) // 已验证, 无法进行正常播报, 建议domain和intent改为chat // return replyWithHeader(header.Raw, resStrb) } /* //增加敏感词过滤 zww 2018-11-21 func chatDomain(jsonData []byte) []byte { header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items := `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } code := gjson.GetBytes(jsonData, "header.semantic.code").String() if code == "0" { resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent").Str //resultContent = filter.WordReplace(resultContent) //替换聊天域中的腾讯关键字 jsonStr := `{"header":` + header.Raw + `,"response_text":"`+ resultContent + `"}` return []byte(jsonStr) } else { resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":`+ resultText.Raw + `}` return []byte(jsonStr) } } */ //增加敏感词过滤 zww 2018-11-21 //取消敏感词过滤 zww 2018-12-04 func chatDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items := `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } code := gjson.GetBytes(jsonData, "header.semantic.code").String() if code == "0" { resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent").Str //resultContent = filter.WordReplace(resultContent) //替换聊天域中的腾讯关键字 //取消 jsonStr := `{"header":` + header.Raw + `,"response_text":"` + resultContent + `"}` return []byte(jsonStr) } else if code == "-3" { resultContent := gjson.GetBytes(jsonData, "header.semantic.msg").Str jsonStr := `{"header":` + header.Raw + `,"response_text":"` + resultContent + `"}` return []byte(jsonStr) } else { resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":` + resultText.Raw + `}` return []byte(jsonStr) } } // 地理领域 zww 2018-12-04 func geographyDomain(params *model.DomainParams) []byte { jsonData := params.JsonData domainStr := gjson.GetBytes(jsonData, "header.semantic.domain").String() intentStr := gjson.GetBytes(jsonData, "header.semantic.intent").String() header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items := `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } // 处于逻辑与ap保持一致 responseTextStr := gjson.GetBytes(jsonData, "payload.response_text").Str textContentStr := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent").Str var resultTextStr string if responseTextStr != "" { resultTextStr = responseTextStr } else if textContentStr != "" { resultTextStr = textContentStr } else { resultTextStr = reply_when_geography_empty } // chat回复需修改 return replyWithChat(resultTextStr, domainStr+"."+intentStr) // 已验证, 无法进行正常播报, 建议domain和intent改为chat // return replyWithHeader(header.Raw, resultTextStr) /* jsonStr := `{ "header": { "semantic": { "code": 0, "domain": "chat", "intent": "chat", "msg": "", "session_complete": true, "skill_id": "221364_geography" } }, "response_text": ` + resultText.Raw + ` }` //jsonStr := `{"header":` + header.Raw + `,"response_text":`+ resultText.Raw + `}` return []byte(jsonStr) */ } // alarm domain zww 2018-11-30 func alarmDomain(params *model.DomainParams) []byte { jsonData := params.JsonData mac := params.Mac header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") responseText := gjson.GetBytes(jsonData, "payload.response_text") if !header.Exists() || !payload.Exists() { items := `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } //header = gjson.GetBytes([]byte(doudiJsonTencent), "header") //暂时以聊天域返回 intent := gjson.GetBytes(jsonData, "header.semantic.intent") domain := gjson.GetBytes(jsonData, "header.semantic.domain") // ret 标识是否成功 ret := gjson.GetBytes(jsonData, "payload.ret").Int() isSessionCompleted := gjson.GetBytes(jsonData, "header.semantic.session_complete").Bool() var alarmDBData model.AlarmRemindInfo alarmDBData.Mac = mac alarmDBData.Note = "" alarmDBData.URL = "" alarmDBData.Speech_Type = onlyTTS alarmDBData.Createtime = time.Now().String()[:19] items := "" //alarmSlotsName := gjson.GetBytes(jsonData, "header.semantic.slots.#.name") //alarmSlotsValue := gjson.GetBytes(jsonData, "header.semantic.slots.#.value") var startAlarmList []gjson.Result var repeatTypeList []gjson.Result var notes []gjson.Result var affectAlarm []gjson.Result var affectAlarmRepeaet []gjson.Result var affectAlarmNote []gjson.Result var idList []gjson.Result var affectIdList []gjson.Result if domain.Str == "alarm" { idList = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAlarmCell.#.lId").Array() startAlarmList = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAlarmCell.#.lStart").Array() repeatTypeList = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAlarmCell.#.eRepeatType").Array() notes = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAlarmCell.#.sNote").Array() affectIdList = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectAlarmCell.#.lId").Array() affectAlarm = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectAlarmCell.#.lStart").Array() affectAlarmRepeaet = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectAlarmCell.#.eRepeatType").Array() affectAlarmNote = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectAlarmCell.#.sNote").Array() //alarmDBData.E_type = remind alarmDBData.E_type = alarm } else if domain.Str == "reminder_v2" { idList = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vReminderCell.#.lId").Array() startAlarmList = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vReminderCell.#.lStart").Array() repeatTypeList = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vReminderCell.#.eRepeatType").Array() notes = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vReminderCell.#.sNote").Array() affectIdList = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectReminderCell.#.lId").Array() affectAlarm = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectReminderCell.#.lStart").Array() affectAlarmRepeaet = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectReminderCell.#.eRepeatType").Array() affectAlarmNote = gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectReminderCell.#.sNote").Array() alarmDBData.E_type = remind } else { jsonStr := `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } for i := 0; i < len(startAlarmList); i++ { repeaetType := int(repeatTypeList[i].Int()) contentTTS := "" if alarmDBData.E_type == remind { if notes[i].Str != "" { contentTTS = alarm_remind_common + notes[i].Str } else { contentTTS = alarm_remind_raw } } if i == 0 { items = `{"id":"` + strconv.FormatInt(idList[i].Int(), 10) + `","speechType":` + onlyTTS + `,"type":` + alarmDBData.E_type + `,"url":"` + "" + `","note":"` + notes[i].Str + `", "repeatType":` + strconv.Itoa(repeaetType) + `, "start":` + strconv.FormatInt(startAlarmList[i].Int(), 10) + `, "content":"` + contentTTS + `"}` } else { items += `,{"id":"` + strconv.FormatInt(idList[i].Int(), 10) + `","speechType":` + onlyTTS + `,"type":` + alarmDBData.E_type + `,"url":"` + "" + `", "note":"` + notes[i].Str + `", "repeatType":` + strconv.Itoa(repeaetType) + `, "start":` + strconv.FormatInt(startAlarmList[i].Int(), 10) + `, "content":"` + contentTTS + `"}` } //与同步接口同步更改 2019-07-10 DONE:2019-07-15 //需要做差异化 // if i == 15 { if i >= global.GetLimitedSetting(params.Mid).AlarmNum && global.GetLimitedSetting(params.Mid).AlarmNum >= 0 { break } } if intent.Str == "new" { jsonStr := "" if !isSessionCompleted { jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `, "listItems":[` + items + `]}` return []byte(jsonStr) } /* old if len(alarmSlotsName.Array()) == 0 { jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } else if len(alarmSlotsName.Array()) == 1 { //循环 if alarmSlotsName.Array()[0].Str == "date" { jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } else if alarmSlotsName.Array()[0].Str == "time" { //循环 eTime := strings.TrimSpace(alarmSlotsValue.Array()[0].Str) eTimeT, _ := time.ParseInLocation(datetimeLayout, time.Now().Format(dateLayout) + " "+ eTime, time.Local) fmt.Println(eTimeT) if time.Now().Before(eTimeT) { eTimeStamp = strconv.FormatInt(eTimeT.Unix(), 10) } else { var errt error eTimeT, errt = time.ParseInLocation(datetimeLayout, time.Now().Add(time.Hour * 24).Format(dateLayout) + " " + eTime, time.Local) if errt != nil { fmt.Println(errt.Error() + "time.Parse") } eTimeStamp = strconv.FormatInt(eTimeT.Unix(), 10) } alarmDBData.E_date = "1993-09-03" alarmDBData.E_time = eTime //return []byte(jsonStr) } else { jsonStr := `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } //return []byte(jsonStr) } else if len(alarmSlotsName.Array()) >= 2 { for i := 0; i < len(alarmSlotsName.Array()); i++ { if alarmSlotsName.Array()[i].Str == "date" { alarmDBData.E_date = alarmSlotsValue.Array()[i].Str } else if alarmSlotsName.Array()[i].Str == "time" { alarmDBData.E_time = alarmSlotsValue.Array()[i].Str } else if alarmSlotsName.Array()[i].Str == "note" { if len(affectAlarmRepeaet) == 1 { alarmDBData.Note = affectAlarmNote[0].Str } else { utils.LogErrorToFile(logfileError, "alarmDoamin.new:len(affectAlarmRepeaet)>1") //fmt.Println("alarmDoamin.new:len(affectAlarmRepeaet)>1") } //alarmDBData.Note = alarmSlotsValue.Array()[i].Str } } eTimeT, _ := time.ParseInLocation(datetimeLayout, alarmDBData.E_date + " " + alarmDBData.E_time, time.Local) eTimeStamp = strconv.FormatInt(eTimeT.Unix(), 10) fmt.Println(eTimeT) //alarmDBData.Repeat_type = //jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` //return []byte(jsonStr) } else { jsonStr := `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } */ // ret != 0 时, 标识插入不成功, 不需要遍历闹钟列表, 直接返回 if len(affectAlarm) == 0 || ret != 0 { jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } else { for i := 0; i < len(affectAlarm); i++ { fmt.Println("[debug] formatTime Location:", time.Unix(affectAlarm[i].Int(), 0).Location()) formatTime := time.Unix(affectAlarm[i].Int(), 0).Local() fmt.Println("[debug] formatTime:", formatTime) alarmDBData.Repeat_type = int(affectAlarmRepeaet[i].Int()) if strconv.Itoa(alarmDBData.Repeat_type) == once { alarmDBData.E_date = formatTime.String()[:10] } else { alarmDBData.E_date = formatTime.String()[:10] //alarmDBData.E_date = time.Now().String()[:10] } alarmDBData.Oid = strconv.FormatInt(affectIdList[i].Int(), 10) alarmDBData.Note = affectAlarmNote[i].Str if alarmDBData.Note != "" { alarmDBData.Content = alarm_remind_common + alarmDBData.Note } else { if domain.Str == "reminder_v2" { alarmDBData.Content = alarm_remind_raw } } alarmDBData.E_time = formatTime.String()[11:19] fmt.Println("[debug] alarmDBData.E_time:", alarmDBData.E_time) dao.SaveAlarmRemindData(alarmDBData) } } jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `, "listItems":[` + items + `]}` //saveAlarmRemindData(alarmDBData) return []byte(jsonStr) } else if intent.Str == "delete" { jsonStr := "" if !isSessionCompleted { jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `, "listItems":[` + items + `]}` //jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } else { //affectAlarm := gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectAlarmCell.#.lStart").Array() //affectAlarmRepeaet := gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAffectAlarmCell.#.eRepeatType").Array() /* if len(affectAlarm) == 0 { jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } else if len(alarmSlotsName.Array()) == 1 { //eTime := strings.TrimSpace(alarmSlotsValue.Array()[0].Str) for i := 0; i < len(affectAlarm); i++ { formatTime := time.Unix(affectAlarm[i].Int(), 0) alarmDBData.E_date = "1993-09-03" alarmDBData.E_time = formatTime.String()[11:19] alarmDBData.Repeat_type = int(affectAlarmRepeaet[i].Int()) deleteAlarmRemindData(alarmDBData) } } else if len(alarmSlotsName.Array()) >= 2 { for i := 0; i < len(affectAlarm); i++ { formatTime := time.Unix(affectAlarm[i].Int(), 0) alarmDBData.E_date = formatTime.String()[:10] alarmDBData.E_time = formatTime.String()[11:19] alarmDBData.Repeat_type = int(affectAlarmRepeaet[i].Int()) deleteAlarmRemindData(alarmDBData) } } */ if len(affectAlarm) == 0 { // 清空闹钟 dao.DeleteAllAlarmRemindData(mac) jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } else { for i := 0; i < len(affectAlarm); i++ { formatTime := time.Unix(affectAlarm[i].Int(), 0) alarmDBData.Repeat_type = int(affectAlarmRepeaet[i].Int()) if strconv.Itoa(alarmDBData.Repeat_type) == once { alarmDBData.E_date = formatTime.String()[:10] alarmDBData.E_date = formatTime.String()[:10] } else { } alarmDBData.Oid = strconv.FormatInt(affectIdList[i].Int(), 10) alarmDBData.E_time = formatTime.String()[11:19] dao.DeleteAlarmRemindData(alarmDBData) } } //startAlarmList := gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAlarmCell.#.lStart").Array() //repeatTypeList := gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAlarmCell.#.eRepeatType").Array() jsonStr = `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `, "listItems":[` + items + `]}` return []byte(jsonStr) } } else if intent.Str == "check" { jsonStr := "" //startAlarmList := gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAlarmCell.#.lStart").Array() //repeatTypeList := gjson.GetBytes(jsonData, "payload.data.json.stCalendarData.vAlarmCell.#.eRepeatType").Array() dao.DeleteOverdueAlarmRemindData(mac) responseTextStr := responseText.String() // responseTextStr = "你一共有29个闹钟:第1个是今天下午5点07分的闹钟,第2个是今天下午5点08分的闹钟,第3个是今天下午5点10分的闹钟,第4个是今天下午5点11分的闹钟,第5个是今天下午5点12分的闹钟,第6个是今天下午5点13分的闹钟,第7个是今天下午5点14分的闹钟,第8个是今天下午5点16分的闹钟,第9个是今天下午5点17分的闹钟,第10个是今天下午5点18分的闹钟,第11个是今天下午5点19分的闹钟,第12个是今天下午5点20分的闹钟,第13个是今天下午5点21分的闹钟,第14个是今天下午5点23分的闹钟,第15个是今天下午5点24分的闹钟,第16个是今天下午5点26分的闹钟,第17个是今天下午5点27分的闹钟,第18个是今天下午5点29分的闹钟,第19个是今天下午5点30分的闹钟,第20个是今天下午5点32分的闹钟,第21个是今天下午5点33分的闹钟,第22个是今天下午5点34分的闹钟,第23个是今天下午5点35分的闹钟,第24个是今天下午5点36分的闹钟,第25个是今天下午5点38分的闹钟,第26个是今天下午5点41分的闹钟,第27个是今天下午5点42分的闹钟,第28个是明天下午2点整的闹钟,第29个是明天下午3点整的闹钟。" textArr := strings.Split(responseTextStr, ",") idx := 0 splitLength := len([]byte(",")) // 经过循环idx 必然大于回复语的字节数 for i := 0; i < len(textArr); i++ { // 限制800字节 tempL := len([]byte(textArr[i])) if idx+tempL > 800 { break } idx = idx + splitLength idx = idx + tempL } textRaw := responseText.Raw if idx < len([]byte(responseTextStr)) { var tipsText string if domain.Str == "alarm" { tipsText = "。你设置的闹钟太多啦,我有点查不过来了。" } else { tipsText = "。你设置的提醒太多啦,我有点查不过来了。" } textByte := append([]byte(responseTextStr)[:idx-splitLength], []byte(tipsText)...) textRaw = strconv.Quote(string(textByte)) } if len(startAlarmList) == 0 { // 清空闹钟 dao.DeleteAllAlarmRemindData(mac) jsonStr = `{"header":` + header.Raw + `, "response_text":` + textRaw + `, "listItems":[]}` return []byte(jsonStr) } else { jsonStr = `{"header":` + header.Raw + `, "response_text":` + textRaw + `, "listItems":[` + items + `]}` return []byte(jsonStr) } } else { jsonStr := `{"header":` + header.Raw + `, "response_text":` + responseText.Raw + `}` return []byte(jsonStr) } } func customChatDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items := `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } code := gjson.GetBytes(jsonData, "header.semantic.code").String() if code == "0" { resultContent := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent").String() // chat回复需修改 return replyWithChat(resultContent, gjson.GetBytes(jsonData, "header.semantic.domain").Str) // 无法进行验证, 建议domain和intent改成chat // return replyWithHeader(header.Raw, resultContent) /* jsonStr := `{ "header": { "semantic": { "code": 0, "domain": "chat", "intent": "chat", "msg": "", "session_complete": true, "skill_id": "221364_` + gjson.GetBytes(jsonData, "header.semantic.domain").Str + `" } }, "response_text": "` + resultContent + `" }` return []byte(jsonStr) */ } else { jsonStr := `{"header":` + header.Raw + `,"response_text":"` + reply_when_tencent_empty + `"}` return []byte(jsonStr) } } // 不进行register func otherDomain(params *model.DomainParams) []byte { jsonData := params.JsonData // domain := gjson.GetBytes(jsonData, "header.semantic.domain").String() // header := gjson.GetBytes(jsonData, "header") resultText := reply_when_tencent_empty //jsonStr := `{"header":` + header.Raw + `,"response_text":"`+ resultText + `"}` //return []byte(jsonStr) domain := gjson.GetBytes(jsonData, "header.semantic.domain") // chat回复需修改 return replyWithChat(resultText, domain.Str) // 无法进行验证, 建议domain和intent改成chat // return replyWithHeader(header.Raw, resultText) /* jsonStr := `{ "header": { "semantic": { "code": 0, "domain": "`+chatDomainStr+`", "intent": "`+chatDomainStr+`", "msg": "", "session_complete": true, "skill_id": "221364_other_` + domain.Str + `" } }, "response_text": "` + resultText + `" }` return []byte(jsonStr) */ } // 腾讯协议转换 globalctrl func globalctrlDomain(params *model.DomainParams) []byte { jsonData := params.JsonData query := params.Query header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items := `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } intent := gjson.GetBytes(jsonData, "header.semantic.intent").String() // 判断意图进行协议转换 switch intent { case "pause": return transformGreeProtocolReply(query, "PlayControl", "control_pause", nil) case "resume": return transformGreeProtocolReply(query, "PlayControl", "control_resume", nil) case "stop": return transformGreeProtocolReply(query, "PlayControl", "control_stop", nil) case "turn_down": degree := gjson.GetBytes(jsonData, `header.semantic.slots.#[name="number"].value`) if degree.Exists() { semanticParams := make(map[string]interface{}) semanticParams["degree"] = model.ParamsStr{Origin: degree.String(), Norm: degree.String(), Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_reduceVol", &semanticParams) } return transformGreeProtocolReply(query, "UniversalControl", "control_reduceVol", nil) case "turn_up": degree := gjson.GetBytes(jsonData, `header.semantic.slots.#[name="number"].value`) if degree.Exists() { semanticParams := make(map[string]interface{}) semanticParams["degree"] = model.ParamsStr{Origin: degree.String(), Norm: degree.String(), Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_riseVol", &semanticParams) } return transformGreeProtocolReply(query, "UniversalControl", "control_riseVol", nil) case "turn_up_max": return transformGreeProtocolReply(query, "UniversalControl", "control_riseVolMax", nil) case "turn_down_min": return transformGreeProtocolReply(query, "UniversalControl", "control_reduceVolMin", nil) case "turn_volume_to": degree := gjson.GetBytes(jsonData, `header.semantic.slots.#[name="number"].value`) if degree.Exists() { semanticParams := make(map[string]interface{}) semanticParams["degree"] = model.ParamsStr{Origin: degree.String(), Norm: degree.String(), Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_setVol", &semanticParams) } volumeto := gjson.GetBytes(jsonData, `header.semantic.slots.#[name="volumeto"].value`) if volumeto.Exists() { // 音量必须调到百分之二十 {"name":"volumeto","value":"20/100"} // 处理分数 if match, _ := regexp.MatchString("[0-9]+/[0-9]+", volumeto.String()); match { numberList := strings.Split(volumeto.String(), "/") if len(numberList) < 2 { return replyWithChat(error_reply, "doudi") } if match, _ := regexp.MatchString("^[0]+$", numberList[1]); !match { // 非零 a, err := strconv.ParseFloat(numberList[0], 64) // 被除数 if err != nil { global.Logger.WithFields(logger.Fields{"data": map[string]interface{}{"jsonData": jsonData}}).Errorf("globalctrlDomain error. %s", err.Error()) return replyWithChat(error_reply, "doudi") } b, err := strconv.ParseFloat(numberList[1], 64) // 除数 if err != nil { global.Logger.WithFields(logger.Fields{"data": map[string]interface{}{"jsonData": jsonData}}).Errorf("globalctrlDomain error. %s", err.Error()) return replyWithChat(error_reply, "doudi") } semanticParams := make(map[string]interface{}) semanticParams["degree"] = model.ParamsStr{Origin: strconv.Itoa(int(a / b * 100)), Norm: strconv.Itoa(int(a / b * 100)), Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_setVol", &semanticParams) } else { // 除数为零 global.Logger.WithFields(logger.Fields{"data": map[string]interface{}{"jsonData": jsonData}}).Error("除数为零.") return replyWithChat(error_reply, "doudi") } } // 处理其他类型数字 semanticParams := make(map[string]interface{}) semanticParams["degree"] = model.ParamsStr{Origin: volumeto.String(), Norm: volumeto.String(), Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_setVol", &semanticParams) } semanticParams := make(map[string]interface{}) semanticParams["degree"] = model.ParamsStr{Origin: "50", Norm: "50", Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_setVol", &semanticParams) case "turn_mid": semanticParams := make(map[string]interface{}) semanticParams["degree"] = model.ParamsStr{Origin: "50", Norm: "50", Code: 0} return transformGreeProtocolReply(query, "UniversalControl", "control_setVol", &semanticParams) case "next": return transformGreeProtocolReply(query, "UniversalControl", "control_next", nil) case "previous": return transformGreeProtocolReply(query, "UniversalControl", "control_previous", nil) // case "yes": // return transformGreeProtocolReply(query, "SystemControl", "control_confirm", nil) // case "no": // return transformGreeProtocolReply(query, "SystemControl", "control_cancel", nil) // resultText := gjson.GetBytes(jsonData, "payload.response_text") // jsonStr := `{"header":` + header.Raw + `,"response_text":"` + resultText.Raw + `"}` // return []byte(jsonStr) case "mute": return transformGreeProtocolReply(query, "SystemControl", "control_volumeOff", nil) case "unmute": return transformGreeProtocolReply(query, "SystemControl", "control_volumeOn", nil) default: resultText := gjson.GetBytes(jsonData, "payload.response_text") jsonStr := `{"header":` + header.Raw + `,"response_text":"` + resultText.Raw + `"}` return []byte(jsonStr) } } // invention_qa_pairs func inventionQaPairsDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items := `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } // 处于逻辑与ap保持一致 responseTextStr := gjson.GetBytes(jsonData, "payload.response_text").Str textContentStr := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent").Str var resultTextStr string if responseTextStr != "" { resultTextStr = responseTextStr } else if textContentStr != "" { resultTextStr = textContentStr } else { resultTextStr = reply_when_geography_empty } return replyWithChat(resultTextStr, "invention_qa_pairs") } // 植物问答 plants_kbqa func plantsKbqaDomain(params *model.DomainParams) []byte { jsonData := params.JsonData header := gjson.GetBytes(jsonData, "header") payload := gjson.GetBytes(jsonData, "payload") if !header.Exists() || !payload.Exists() { //fmt.Println("json data header/payload dose not exists!") items := `{"code":501,"errorType":"result header/payload dose not exists!"}` return []byte(items) } responseTextStr := gjson.GetBytes(jsonData, "payload.response_text").Str textContentStr := gjson.GetBytes(jsonData, "payload.data.json.listItems.0.textContent").Str var resultTextStr string if responseTextStr != "" { resultTextStr = responseTextStr } else if textContentStr != "" { resultTextStr = textContentStr } else { resultTextStr = reply_when_geography_empty } return replyWithChat(resultTextStr, "plants_kbqa") } // 屏蔽处理 func shieldedDomain(params *model.DomainParams) []byte { jsonData := params.JsonData domainStr := gjson.GetBytes(jsonData, "header.semantic.domain").String() intentStr := gjson.GetBytes(jsonData, "header.semantic.intent").String() // 暂不支持此功能 resultTextStr := shielded_reply return replyWithChat(resultTextStr, domainStr+"."+intentStr) } // help func helpDomain(params *model.DomainParams) []byte { jsonData := params.JsonData domainStr := gjson.GetBytes(jsonData, "header.semantic.domain").String() intentStr := gjson.GetBytes(jsonData, "header.semantic.intent").String() // 暂不支持此功能 resultTextStr := "我可以控制空调开机、播放音乐、查询时间天气、调节温度,设置模式,例如您可以对我说,空调开机。" return replyWithChat(resultTextStr, domainStr+"."+intentStr) }