-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
tms.go
82 lines (70 loc) · 1.94 KB
/
tms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package qcloud
import (
"encoding/json"
"fmt"
"strconv"
)
func TMS(conf TmsConf) (bool, error) {
reqData := make(map[string]string)
reqData["Content"] = base64EncodeStr(conf.Content)
reqData["DataId"] = conf.DataID
reqData["User.UserId"] = conf.UserID
reqData["User.Nickname"] = conf.UserName
reqData["Device.IP"] = conf.DeviceIP
apiConf := TencentApiRequestConf{
SecretID: conf.SecretID,
SecretKey: conf.SecretKey,
Region: conf.Region,
Product: "tms",
Action: "TextModeration",
Version: "2020-12-29",
Method: "GET",
Data: reqData,
}
body, err := RequestApi(apiConf)
if err != nil {
return false, err
}
// fmt.Println(string(body))
var data TmsResponse
err = json.Unmarshal(body, &data)
if err != nil {
return false, err
}
if data.Response.Error != nil {
errMsg := data.Response.Error.Message
reqID := data.Response.RequestId
return false, fmt.Errorf("响应错误 err_msg=%s request_id= %s", strconv.Quote(errMsg), reqID)
}
suggestion := data.Response.Suggestion
if suggestion == "" {
return false, fmt.Errorf("suggestion value is empty")
}
return (suggestion == "Pass"), nil
}
type TmsConf struct {
SecretID string
SecretKey string
Region string
Content string
DataID string
UserID string
UserName string
DeviceIP string
}
type TmsResponse struct {
Response struct {
// 建议值,Block:建议屏蔽,Review:建议复审,Pass:建议通过
Suggestion string `json:"Suggestion"`
// 恶意标签,Normal:正常,Porn:色情,Abuse:谩骂,Ad:广告,Custom:自定义词库,以及令人反感、不安全或不适宜的内容类型
Label string `json:"Label"`
// 请求参数中的 DataId
DataId string `json:"DataId"`
// 唯一请求 ID,可用于定位 API 问题
RequestId string `json:"RequestId"`
Error *struct {
Code string `json:"Code"`
Message string `json:"Message"`
} `json:"Error,omitempty"`
} `json:"Response"`
}