|
@@ -6,12 +6,16 @@ import (
|
|
|
"io"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
+ "net/url"
|
|
|
"os"
|
|
|
+ "regexp"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/JamZYM/golagrange/api"
|
|
|
+ "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
|
|
|
+ "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
|
|
"github.com/shirou/gopsutil/v4/cpu"
|
|
|
"github.com/shirou/gopsutil/v4/disk"
|
|
|
"github.com/shirou/gopsutil/v4/mem"
|
|
@@ -141,3 +145,128 @@ func Func_serverStatus(connBot api.WsBot, user_id int) {
|
|
|
connBot.Send_private_msg(user_id, response_text)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+type TokenResult struct {
|
|
|
+ ErrMsg string
|
|
|
+ Token struct {
|
|
|
+ UserId string
|
|
|
+ Id string
|
|
|
+ ExpireTime int64
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func Func_TTS(connBot api.WsBot, id int, msg_text string, isGroup bool, file_id int) {
|
|
|
+ msg_text = strings.Replace(msg_text, "/tts ", "", 1)
|
|
|
+ currentTime := time.Now().Unix()
|
|
|
+ expireTime, _ := strconv.Atoi(os.Getenv("TOKENEXPIRE"))
|
|
|
+ if currentTime > int64(expireTime) {
|
|
|
+ _tts_getToken()
|
|
|
+ }
|
|
|
+ var appkey string = os.Getenv("APPKEY")
|
|
|
+ var token string = os.Getenv("TOKEN")
|
|
|
+ var text string = msg_text
|
|
|
+ var textUrlEncode = text
|
|
|
+ textUrlEncode = url.QueryEscape(textUrlEncode)
|
|
|
+ textUrlEncode = strings.Replace(textUrlEncode, "+", "%20", -1)
|
|
|
+ textUrlEncode = strings.Replace(textUrlEncode, "*", "%2A", -1)
|
|
|
+ textUrlEncode = strings.Replace(textUrlEncode, "%7E", "~", -1)
|
|
|
+ var audioSaveFile string = "temp/audio/" + strconv.Itoa(file_id) + ".wav"
|
|
|
+ absoluteDir, _ := os.Getwd()
|
|
|
+ var absoluteFile string = "file://" + absoluteDir + "/" + audioSaveFile
|
|
|
+ var format string = "wav"
|
|
|
+ var sampleRate int = 16000
|
|
|
+ _tts_processGETRequest(appkey, token, textUrlEncode, audioSaveFile, format, sampleRate)
|
|
|
+ if isGroup {
|
|
|
+ connBot.Send_group_msg(id, "[CQ:record,file="+absoluteFile+"]")
|
|
|
+ } else {
|
|
|
+ connBot.Send_private_msg(id, "[CQ:record,file="+absoluteFile+"]")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func _tts_processGETRequest(appkey string, token string, text string, audioSaveFile string, format string, sampleRate int) {
|
|
|
+ /**
|
|
|
+ * 设置HTTPS GET请求:
|
|
|
+ * 1.使用HTTPS协议
|
|
|
+ * 2.语音识别服务域名:nls-gateway-cn-shanghai.aliyuncs.com
|
|
|
+ * 3.语音识别接口请求路径:/stream/v1/tts
|
|
|
+ * 4.设置必须请求参数:appkey、token、text、format、sample_rate
|
|
|
+ * 5.设置可选请求参数:voice、volume、speech_rate、pitch_rate
|
|
|
+ */
|
|
|
+ var url string = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts"
|
|
|
+ url = url + "?appkey=" + appkey
|
|
|
+ url = url + "&token=" + token
|
|
|
+ url = url + "&text=" + text
|
|
|
+ url = url + "&format=" + format
|
|
|
+ url = url + "&sample_rate=" + strconv.Itoa(sampleRate)
|
|
|
+ // voice 发音人,可选,默认是xiaoyun。
|
|
|
+ // url = url + "&voice=" + "xiaoyun"
|
|
|
+ // volume 音量,范围是0~100,可选,默认50。
|
|
|
+ // url = url + "&volume=" + strconv.Itoa(50)
|
|
|
+ // speech_rate 语速,范围是-500~500,可选,默认是0。
|
|
|
+ // url = url + "&speech_rate=" + strconv.Itoa(0)
|
|
|
+ // pitch_rate 语调,范围是-500~500,可选,默认是0。
|
|
|
+ // url = url + "&pitch_rate=" + strconv.Itoa(0)
|
|
|
+ /**
|
|
|
+ * 发送HTTPS GET请求,处理服务端的响应。
|
|
|
+ */
|
|
|
+ response, err := http.Get(url)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("The GET request failed!")
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ defer response.Body.Close()
|
|
|
+ contentType := response.Header.Get("Content-Type")
|
|
|
+ body, _ := io.ReadAll(response.Body)
|
|
|
+ if contentType == "audio/mpeg" {
|
|
|
+ file, _ := os.Create(audioSaveFile)
|
|
|
+ defer file.Close()
|
|
|
+ file.Write([]byte(body))
|
|
|
+ // fmt.Println("The GET request succeed!")
|
|
|
+ } else {
|
|
|
+ // ContentType 为 null 或者为 "application/json"
|
|
|
+ statusCode := response.StatusCode
|
|
|
+ fmt.Println("The HTTP statusCode: " + strconv.Itoa(statusCode))
|
|
|
+ fmt.Println("The GET request failed: " + string(body))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func _tts_getToken() {
|
|
|
+ client, err := sdk.NewClientWithAccessKey("cn-shanghai", os.Getenv("AKID"), os.Getenv("AKKEY"))
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ request := requests.NewCommonRequest()
|
|
|
+ request.Method = "POST"
|
|
|
+ request.Domain = "nls-meta.cn-shanghai.aliyuncs.com"
|
|
|
+ request.ApiName = "CreateToken"
|
|
|
+ request.Version = "2019-02-28"
|
|
|
+ response, err := client.ProcessCommonRequest(request)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ var tr TokenResult
|
|
|
+ err = json.Unmarshal([]byte(response.GetHttpContentString()), &tr)
|
|
|
+ if err == nil {
|
|
|
+ fmt.Println(tr.Token.Id)
|
|
|
+ fmt.Println(tr.Token.ExpireTime)
|
|
|
+ } else {
|
|
|
+ fmt.Println(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ envContent, err := os.ReadFile(".env")
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error reading .env file")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ re1 := regexp.MustCompile(`TOKEN=".*"`)
|
|
|
+ newContent := re1.ReplaceAllString(string(envContent), fmt.Sprintf(`TOKEN="%s"`, tr.Token.Id))
|
|
|
+ re := regexp.MustCompile(`TOKENEXPIRE=".*"`)
|
|
|
+ newContent = re.ReplaceAllString(string(newContent), fmt.Sprintf(`TOKENEXPIRE="%d"`, tr.Token.ExpireTime))
|
|
|
+
|
|
|
+ err = os.WriteFile(".env", []byte(newContent), 0644)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error reading .env file")
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|