Use the AI Guardrails SDK for Go to submit text for synchronous spam moderation — including detection of pornographic and terrorist content — and to submit corrections when results don't match your expectations.
Prerequisites
Before you begin, make sure you have:
A RAM user with permission to call the Content Moderation API
The corresponding AccessKey ID and AccessKey secret stored as environment variables:
ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET
Go dependencies for the SDK installed (see Installation). Use the Go version specified in that guide; mismatched versions cause operation calls to fail.
Never hardcode credentials in your source code. Store them in environment variables and read them at runtime.
How it works
Text anti-spam runs synchronously — each request submits one or more text entries and returns a machine-assisted moderation result. Billing is based on the number of text entries submitted, not the number of requests. For pricing details, see Content Moderation 1.0 activation and billing.
Text anti-spam supports custom terms, such as competitor brand names. When moderated text contains a custom term, the suggestion field in the result is set to block. Add custom terms in the AI Guardrails console or by calling an API operation.
Submit a text moderation task
Use TextScanRequest to submit text entries for spam detection. Set the scenes parameter to antispam.
Supported regions: cn-shanghai (China, Shanghai), cn-beijing (China, Beijing), cn-shenzhen (China, Shenzhen), ap-southeast-1 (Singapore)
For the full list of request parameters, see /green/text/scan.
Key request parameters
| Parameter | Required | Description |
|---|---|---|
scenes | Required | Moderation scenario. Set to antispam. |
tasks[].content | Required | Text content to moderate. One task per text entry. |
Sample code
Reuse the client instance across calls to improve performance and avoid unnecessary reconnections.
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
)
func main() {
// Initialize the client using credentials from environment variables.
client, err := green.NewClientWithAccessKey(
"cn-shanghai",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
)
if err != nil {
fmt.Println(err.Error())
return
}
// Build the request: one task per text entry.
task := map[string]interface{}{"content": "<text-to-moderate>"}
body, _ := json.Marshal(map[string]interface{}{
"scenes": [...]string{"antispam"},
"tasks": [...]map[string]interface{}{task},
})
request := green.CreateTextScanRequest()
request.SetContent(body)
response, err := client.TextScan(request)
if err != nil {
fmt.Println(err.Error())
return
}
if response.GetHttpStatus() != 200 {
fmt.Println("Request failed. HTTP status: " + strconv.Itoa(response.GetHttpStatus()))
return
}
fmt.Println(response.GetHttpContentString())
}Replace <text-to-moderate> with the actual text content to scan.
Submit feedback on a moderation result
When a machine-assisted moderation result is incorrect, use TextFeedbackRequest to submit a correction. The server updates the result and adds the feedback to a text library. Subsequent submissions that match the same text pattern return the corrected result.
Supported regions: cn-shanghai (China, Shanghai), cn-beijing (China, Beijing), cn-shenzhen (China, Shenzhen), ap-southeast-1 (Singapore)
Key request parameters
| Parameter | Description |
|---|---|
taskId | ID of the text moderation task to correct. Returned in the TextScanRequest response. |
content | The original text content that was moderated. |
label | The expected moderation category (e.g., spam). |
Sample code
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/services/green"
)
func main() {
client, err := green.NewClientWithAccessKey(
"cn-shanghai",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
)
if err != nil {
fmt.Println(err.Error())
return
}
// label: the correct moderation category for the submitted text.
body, _ := json.Marshal(map[string]interface{}{
"taskId": "<task-id>",
"content": "<original-text>",
"label": "spam",
})
request := green.CreateTextFeedbackRequest()
request.SetContent(body)
response, err := client.TextFeedback(request)
if err != nil {
fmt.Println(err.Error())
return
}
if response.GetHttpStatus() != 200 {
fmt.Println("Request failed. HTTP status: " + strconv.Itoa(response.GetHttpStatus()))
return
}
fmt.Println(response.GetHttpContentString())
}Replace the placeholders with actual values:
| Placeholder | Description |
|---|---|
<task-id> | The taskId from the original TextScanRequest response |
<original-text> | The text content that was moderated |
What's next
Synchronous text moderation API reference: full parameter list and response schema for
TextScanRequestContent Moderation 1.0 activation and billing: pricing and quota details
AI Guardrails console: manage custom terms