Java SDK call example
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
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
Grant permissions to the RAM user
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.
NoteTo 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_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETas 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.
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"
}