|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | +) |
| 11 | + |
| 12 | +func runReplacementsAndReturnModifiedBody(body []byte, r *http.Request) []byte { |
| 13 | + typedBody := QueryResponse{} |
| 14 | + decoder := json.NewDecoder(bytes.NewReader(body)) |
| 15 | + decoder.DisallowUnknownFields() |
| 16 | + |
| 17 | + err := decoder.Decode(&typedBody) |
| 18 | + if err != nil { |
| 19 | + log.Println("Error when decoding JSON (", string(body), ") will return unhandled:", err) |
| 20 | + return body |
| 21 | + } else { |
| 22 | + if typedBody.QueryResult == nil || typedBody.QueryId == "" { |
| 23 | + log.Println("Unexpected query response, will return unhandled.") |
| 24 | + return body |
| 25 | + } |
| 26 | + |
| 27 | + if typedBody.QueryResult.Intent.Name == "chatgpt_speak" && conf.ChatGptSpeakServer != "" { |
| 28 | + speakResponse := makeChatGptSpeakRequest(typedBody.QueryResult.QueryText, typedBody.LanguageCode, typedBody.QueryResult.BehaviorParas.Txt, r) |
| 29 | + if speakResponse.Url != "" && speakResponse.Txt != "" { |
| 30 | + log.Println("Successfully replaced chatgpt_speak response for request.") |
| 31 | + typedBody.QueryResult.BehaviorParas.Url = speakResponse.Url |
| 32 | + typedBody.QueryResult.BehaviorParas.Txt = speakResponse.Txt |
| 33 | + } else { |
| 34 | + log.Println("Failed to get valid response from ChatGptSpeakServer, keeping original response.") |
| 35 | + } |
| 36 | + } |
| 37 | + modifiedBody, err := json.Marshal(typedBody) |
| 38 | + if err != nil { |
| 39 | + log.Println("Error when marshaling modified JSON, will return unhandled:", err) |
| 40 | + return body |
| 41 | + } |
| 42 | + return modifiedBody |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func makeEmoSpeechRequest(text string, languageCode string, r *http.Request) EmoSpeechResponse { |
| 47 | + request, _ := http.NewRequest("GET", "https://"+conf.Livingio_API_Server+"/emo/speech/tts?q="+url.QueryEscape(text)+"&l="+url.QueryEscape(languageCode), nil) |
| 48 | + |
| 49 | + val, exists := r.Header["Authorization"] |
| 50 | + if exists { |
| 51 | + request.Header.Add("Authorization", val[0]) |
| 52 | + } |
| 53 | + |
| 54 | + val, exists = r.Header["Secret"] |
| 55 | + if exists { |
| 56 | + request.Header.Add("Secret", val[0]) |
| 57 | + } |
| 58 | + |
| 59 | + request.Header.Del("User-Agent") |
| 60 | + |
| 61 | + httpclient := &http.Client{} |
| 62 | + response, err := httpclient.Do(request) |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + log.Fatalf("An Error Occured %v", err) |
| 66 | + } |
| 67 | + defer response.Body.Close() |
| 68 | + |
| 69 | + body, _ := io.ReadAll(response.Body) |
| 70 | + |
| 71 | + var emoSpeechResponse EmoSpeechResponse |
| 72 | + if err := json.Unmarshal([]byte(body), &emoSpeechResponse); err != nil { |
| 73 | + log.Printf("Error unmarshaling ChatGptSpeakServer response: %v\n", err) |
| 74 | + return EmoSpeechResponse{} |
| 75 | + } |
| 76 | + |
| 77 | + return emoSpeechResponse |
| 78 | +} |
| 79 | + |
| 80 | +func makeChatGptSpeakRequest(queryText string, languageCode string, fallbackResponse string, r *http.Request) BehaviorParas { |
| 81 | + type ChatGptSpeakRequest struct { |
| 82 | + QueryText string `json:"queryText"` |
| 83 | + LanguageCode string `json:"languageCode"` |
| 84 | + FallbackResponse string `json:"fallbackResponse,omitempty"` |
| 85 | + } |
| 86 | + type ChatGptSpeakResponse struct { |
| 87 | + ResponseText string `json:"responseText"` |
| 88 | + } |
| 89 | + |
| 90 | + chatGptRequestData := ChatGptSpeakRequest{ |
| 91 | + QueryText: queryText, |
| 92 | + LanguageCode: languageCode, |
| 93 | + FallbackResponse: fallbackResponse, |
| 94 | + } |
| 95 | + chatGptRequestBody, _ := json.Marshal(chatGptRequestData) |
| 96 | + chatGptRequest, _ := http.NewRequest("POST", conf.ChatGptSpeakServer+"/speak", bytes.NewBuffer(chatGptRequestBody)) |
| 97 | + chatGptRequest.Header.Add("Content-Type", "application/json") |
| 98 | + |
| 99 | + chatGptClient := &http.Client{} |
| 100 | + chatGptResponse, err := chatGptClient.Do(chatGptRequest) |
| 101 | + if err != nil { |
| 102 | + log.Fatalf("An Error Occured while calling ChatGptSpeakServer %v", err) |
| 103 | + } |
| 104 | + defer chatGptResponse.Body.Close() |
| 105 | + |
| 106 | + chatGptResponseBody, _ := io.ReadAll(chatGptResponse.Body) |
| 107 | + |
| 108 | + var chatGptTypedResponse ChatGptSpeakResponse |
| 109 | + if err := json.Unmarshal([]byte(chatGptResponseBody), &chatGptTypedResponse); err != nil { |
| 110 | + log.Printf("Error unmarshaling ChatGptSpeakServer response: %v\n", err) |
| 111 | + return BehaviorParas{} |
| 112 | + } |
| 113 | + |
| 114 | + if chatGptTypedResponse.ResponseText == "" { |
| 115 | + log.Println("ChatGptSpeakServer returned empty response text") |
| 116 | + return BehaviorParas{} |
| 117 | + } |
| 118 | + |
| 119 | + emoSpeechResponse := makeEmoSpeechRequest(chatGptTypedResponse.ResponseText, languageCode, r) |
| 120 | + if emoSpeechResponse.Code != 200 || emoSpeechResponse.Url == "" { |
| 121 | + log.Printf("Error in EmoSpeechResponse: Code %d, Errmessage: %s\n", emoSpeechResponse.Code, emoSpeechResponse.Errmessage) |
| 122 | + return BehaviorParas{} |
| 123 | + } |
| 124 | + behaviorParasResponse := BehaviorParas{ |
| 125 | + Txt: chatGptTypedResponse.ResponseText, |
| 126 | + Url: emoSpeechResponse.Url, |
| 127 | + } |
| 128 | + |
| 129 | + return behaviorParasResponse |
| 130 | +} |
0 commit comments