Java SDK call example

Updated at:

This topic describes how to use the software development kit (SDK) to call an OpenAPI operation to make an outbound call from an intelligent contact bot.

Step 1: View the OpenAPI document

Before you call an OpenAPI, we recommend that you read the documentation for the API, such as RobotCall, to learn about the parameters and permissions required to call the API. For more information, see API Catalog.

Step 2: Create a RAM user and grant permissions

Important

We recommend that you grant permissions to a RAM user and use the RAM user to make API calls and perform daily operations and maintenance (O&M). For more information about RAM users, see RAM User Overview.

Create a RAM user

Go to Create RAM User, enter the user information, set Access Mode to Access With Permanent AccessKey, and then click OK to create the RAM user. Save the AccessKey information.

Grant permissions to the RAM user

Go to the RAM User List, find the RAM user you created, and in the Actions column, click Add Permissions. In the Access Policy text search box, enter AliyunAiccsFullAccess, select the policy, and click Confirm Authorization.

Note
  • AliyunAiccsFullAccess: Grants full management permissions.

  • AliyunAiccsReadOnlyAccess: Grants read-only permissions.

To create a custom permission, see Authorization Information.

Step 3: Call the OpenAPI operation

SDKs can be easily integrated into your application and cover a wide range of operations. We recommend that you use SDKs to call API operations. This topic uses the Java SDK as an example. The usage of SDKs for other languages is similar. For more information, see Artificial Intelligence Cloud Call Service SDK.

Prerequisites

  • Ensure that your Java version is later than Java 8. For instructions on how to configure the Java environment, see Set up a Java development environment on Windows.

  • Install the SDK by configuring the Maven dependency.

    <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aiccs20191015</artifactId>
          <!-- Replace 'the-latest-version' with the latest version number: https://mvnrepository.com/artifact/com.aliyun/aiccs20191015 -->
          <version>the-latest-version</version>
        </dependency>
  • Configure environment variables to read your AccessKey. For more information, see Configure environment variables on Linux, macOS, and Windows.

    Note

    To prevent AccessKey pair leaks, we recommend that you configure environment variables to use the AccessKey pair instead of hard-coding it in your code.

    This topic uses the environment variable names ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET as examples in the following operations.

Code example

The following code example shows how to use the SDK to call the API operation to make an outbound call from an intelligent contact bot. You must fill in the parameters based on the comments in the code.

package com.aliyun.sample;

import com.aliyun.tea.*;

public class Sample {

    /**
     * description :
     * <p>Initializes a client using credentials.</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.aiccs20191015.Client createClient() throws Exception {
        Config config = new Config()
                // Configure the AccessKey ID. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Configure the AccessKey secret. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));

        // Configure the endpoint. For more information, see https://api.aliyun.com/product/aiccs
        config.endpoint = "aiccs.aliyuncs.com";

        return new Client(config);
    }

    public static void main(String[] args_) throws Exception {

        com.aliyun.aiccs20191015.Client client = Sample.createClient();
        com.aliyun.aiccs20191015.models.RobotCallRequest robotCallRequest = new com.aliyun.aiccs20191015.models.RobotCallRequest()
                // The caller ID number for the outbound call.
                .setCalledShowNumber("1320000****")
                // The called number.
                .setCalledNumber("1520000****")
                // The bot ID.
                .setRobotId(1000**********);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // Print the return value of the API operation when you run the code.
            client.robotCallWithOptions(robotCallRequest, runtime);
        } catch (TeaException error) {
            // The following code is for demonstration purposes only. Handle exceptions with caution. Do not ignore exceptions in your project.
            // The error message.
            System.out.println(error.getMessage());
            // The diagnostic address.
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // The following code is for demonstration purposes only. Handle exceptions with caution. Do not ignore exceptions in your project.
            // The error message.
            System.out.println(error.getMessage());
            // The diagnostic address.
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

You can also download the sample code and run it directly.

Download the sample code

  1. Go to RobotCall.

  2. In the left pane, on the Parameter Settings tab, enter the required parameters as shown in the following examples:

    • Set the CalledShowNumber parameter to an example value, such as 1390000****.

    • Set the CalledNumber parameter to an example value, such as 1320000****.

    • Set the RobotId parameter to an example value, such as 1000**********.

  3. On the SDK Example tab on the right, select v2.0 as the SDK version, select Java as the language, and click the Download Complete Project button to download the sample code package.

  4. After you decompress the package, use an integrated development environment (IDE) to load the project. After the dependencies are loaded, open the src/main/java/com/aliyun/sample/Sample.java file.

Run the project

After the project runs, you can view the output. The following content is returned in this example:

{
  "Message": "OK",
  "RequestId": "A4BBDCC0-4E32-5A61-9BDE-E34440D5B3B5",
  "Data": "1320000****^1390000****",
  "Code": "OK"
}