79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package saft_hat
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// TextToAudioReq 定义发送自定义语音的请求结构体
|
|
type TextToAudioReq struct {
|
|
g.Meta `path:"/device/text" method:"post" tags:"安全帽相关" summary:"发送自定义语言数据"`
|
|
Data string `json:"data" dc:"设备号"`
|
|
Text string `json:"text" dc:"发送语音的文字内容"`
|
|
Time int `json:"time" dc:"播放次数 最少一次 最多三次"`
|
|
}
|
|
|
|
type TextToAudioRes struct {
|
|
Code int `json:"code"` // 响应代码
|
|
Data string `json:"data"` // 设备数据
|
|
Msg string `json:"msg"` // 响应消息
|
|
}
|
|
|
|
// 发送自定义语音
|
|
func (h Hat) SendTextToAudio(ctx context.Context, req *TextToAudioReq) (res *TextToAudioRes, err error) {
|
|
res = new(TextToAudioRes)
|
|
timestamp := time.Now().Unix()
|
|
|
|
// 准备生成签名的参数
|
|
params := map[string]string{
|
|
"appid": appid,
|
|
"data": req.Data,
|
|
"secret": secret,
|
|
"text": req.Text,
|
|
"time": strconv.Itoa(req.Time),
|
|
"timestamp": strconv.FormatInt(timestamp, 10),
|
|
}
|
|
|
|
// 生成签名
|
|
sign := GenerateSignature(params)
|
|
|
|
// 构造请求体
|
|
reqBody := map[string]interface{}{
|
|
"data": req.Data,
|
|
"appid": appid,
|
|
"sign": sign,
|
|
"secret": secret,
|
|
"time": req.Time,
|
|
"text": req.Text,
|
|
"timestamp": strconv.FormatInt(timestamp, 10),
|
|
}
|
|
|
|
payloadBytes, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
// 发送POST请求
|
|
resp, err := http.Post("https://www.loctp.com/api/crm/v1/textToAudio", "application/json", bytes.NewBuffer(payloadBytes))
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 读取响应
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
fmt.Println("Response:", string(respBody))
|
|
return res, nil
|
|
}
|