Add a person to a group

Updated at:

Use the Content Moderation Go SDK to add a person to one or more person groups.

Prerequisites

Before you begin, ensure that you have:

  • Go dependencies installed. For installation steps and the required Go version, see Installation

  • The AI Guardrails API endpoint configured. For endpoint details, see Endpoint

Note: Use the Go version specified in the Installation topic. Using an unsupported Go version causes operation calls to fail.

Add a person to person groups

The following example calls AddGroups to assign a person to one or more person groups. Replace <your-person-id> and <your-group-id> with your actual values before running.

package main

import (
    "encoding/json"
    "fmt"
    "os"
    "strconv"

    "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
)

func main() {
    // Reuse the client across calls to improve performance
    // and avoid repeated connection overhead.
    // Credentials are read from environment variables—never hardcode them.
    client, err := green.NewClientWithAccessKey(
        "cn-shanghai",
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),     // AccessKey ID of your RAM user
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), // AccessKey secret of your RAM user
    )
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    // personId: ID of the person to add (required)
    // groupIds: IDs of the target person groups (required, supports multiple groups)
    content, _ := json.Marshal(map[string]interface{}{
        "personId": "<your-person-id>",
        "groupIds": []string{"<your-group-id>"},
    })

    request := green.CreateAddGroupsRequest()
    request.SetContent(content)

    response, err := client.AddGroups(request)
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    if response.GetHttpStatus() != 200 {
        fmt.Println("Request failed. HTTP status code: " + strconv.Itoa(response.GetHttpStatus()))
        return
    }

    fmt.Println(response.GetHttpContentString())
}

Parameters

ParameterTypeRequiredDescription
personIdstringYesID of the custom person to add
groupIdsstring arrayYesIDs of the person groups to add the person to. A person can belong to multiple groups.

For the complete parameter reference, see API operation for adding a person to a group.