This topic walks you through integrating an AI agent into an Android application for real-time messaging.
Source code
Download link
Go to GitHub open-source project to download the source code.
Directory structure
├── Android // The root directory.
│ ├── AUIBaseKits // The basic components of AUI Kits.
│ ├── AUIAICall // The UI components.
│ ├── README.md
│ ├── app // The demo.
│ ├── build.gradle
│ └── settings.gradleEnvironment requirements
Android Studio plug-in: V4.1.3 or later
Gradle: V7.0.2 or later
Java Development Kit (JDK) in Android Studio: JDK 11 or later
Prerequisite
The relevant interfaces have been developed, or the server source code has been deployed on your app server. For more information, see Deploy a project.
Run the demo
After downloading the source code, start Android Studio and open the Android directory.
Modify the package ID in the build.gradle file.
In the
AUIAICallAgentIdConfig.javafile, setChatBot_AGENT_IDto the AI agent ID generated by the console, andRegionto where the AI agent was created.private static String ChatBot_AGENT_ID = "<AI agent ID>"; private static String Region = "<Region where the agent was created>";Region name
Region ID
China (Hangzhou)
cn-hangzhou
China (Shanghai)
cn-shanghai
China (Beijing)
cn-beijing
China (Shenzhen)
cn-shenzhen
Singapore
ap-southeast-1
After completing the agent configuration, you can start the agent using one of the following methods:
When the app server is deployed: If you have deployed the source code provided by Alibaba Cloud on your app server, go to the
AppServiceConst.javafile and modify the server domain:// AppServiceConst.java String HOST = "Your app server domain";When the app server is not deployed: If you have not yet deployed the source code on your app server and need to run the demo for trial, go to the
AUIAIChatAuthTokenHelper.javafile, configure theEnableDevelopTokenparameter, and copy the application ID, AppKey, and AppSign of the Interactive Messaging (IM) application used by the agent from the console to generate the authentication token on the client side.ImportantThis method requires embedding your AppKey and other sensitive information into your application. It is for testing and development only. Never use this method in a production environment. Exposing your AppKey on the client side creates a serious security risk.
// AUIAIChatAuthTokenHelper.java public class AUIAIChatAuthTokenHelper { // Set the value to true to enable Develop mode. private static final boolean EnableDevelopToken = true; // AppId of IM application copied from the console. private static final String AIChatIMDevelopAppId = "AppId of IM application"; // AppKey of IM application copied from the console. private static final String AIChatIMDevelopAppKey = "AppKey of IM application"; // AppSign of IM application copied from the console. private static final String AIChatIMDevelopAppSign = "AppSign of IM application"; }
Run the app to message the AI agent.
Develop Conversational AI capabilities
Perform the following steps to integrate AUIAICall into your app:
Integrate the source code
Import AUIAICall
In Android Studio, choose File > New > Import Module, then import the downloaded folder.
Modify the following dependencies in the build.gradle file:
dependencies { implementation 'androidx.appcompat:appcompat:x.x.x' // Replace x.x.x with a version that is compatible with your project. implementation 'com.google.android.material:material:x.x.x' // Replace x.x.x with a version that is compatible with your project. androidTestImplementation 'androidx.test.espresso:espresso-core:x.x.x' // Replace x.x.x with a version that is compatible with your project. implementation 'com.aliyun.aio:AliVCSDK_ARTC:x.x.x' // Replace x.x.x with a version that is compatible with your project. implementation 'com.aliyun.auikits.android:ARTCAICallKit:x.x.x' implementation 'com.aliyun.sdk.android:AliVCInteractionMessage:x.x.x' }NoteThe minimum compatible version of the ARTC SDK is V7.1.0. Latest version: 7.8.1.
Latest version of AICallKit SDK: 2.9.0.
Latest version of AliVCInteractionMessage: 1.8.0.
Wait for the sync to complete.
Configure the source code
After you complete all the steps above, modify the server domain name in the AppServiceConst.java file.
// AppServiceConst.java
String HOST = "Domain name of your app server";If you have not deployed the app server, you can generate a token on the client side to test and run the demo. For more information, see Step 4 in "Run the demo" section.
Call API operations
After completing the above steps, call API operations in other modules of your app or on its homepage to start a conversation with the AI agent. You can also modify the source code as needed.
/** Make sure that you are authorized to use the microphone and camera. */
// The AI agent type.
ARTCAICallEngine.ARTCAICallAgentType aiCallAgentType =
ARTCAICallEngine.ARTCAICallAgentType.ChatBot;
// The AI agent ID, which cannot be empty.
String aiAgentId = "XXXXXX";
Context currentActivity = AUIAICallEntranceActivity.this;
Intent intent = new Intent(currentActivity, AUIAIChatInChatActivity.class);
// The ID of the user who joins the chat. We recommend using the user ID for login.
String userId = "123";
intent.putExtra(AUIAIConstStrKey.BUNDLE_KEY_LOGIN_USER_ID, userId);
// The AI agent type.
intent.putExtra(AUIAIConstStrKey.BUNDLE_KEY_AI_AGENT_TYPE, aiCallAgentType);
// The AI agent ID.
intent.putExtra(AUIAIConstStrKey.BUNDLE_KEY_AI_AGENT_ID, aiAgentId);
currentActivity.startActivity(intent);