AI Chat Agent

Updated at:

This topic describes how to integrate the AI Chat Agent on Android.

Introduction

You can use the real-time messaging feature of DingRTC to receive service status, transcription messages, and control commands from the AI Chat Agent. This feature relies on meetings, and you must join a meeting before you can use it.

Concepts

Session: A real-time messaging session uniquely identified by a session ID. Sessions support join, leave, and close operations. To send and receive real-time messages from the agent, you must first join the corresponding session.

Agent ID: The user ID of the agent.

Transcription message: The transcribed information from the conversation with the agent.

Natural conversation/Push-to-talk mode: The conversation mode of an agent created through an OpenAPI.

Integration method

  1. Integrate basic real-time messaging before you join a meeting. For more information, see Android integration method.

  2. You can also obtain the DingRtmAgentClient from the DingRtmClient:

    DingRtmAgentClient agentClient = rtmClient.getAgentClient();
  3. Set a callback listener for the DingRtmAgentClient instance:

    agentClient.setListener(new DingRtmAgentEventListener() {
        @Override
        public void onAgentUserReady(String sessionId, String agentId) {
        }
    
        @Override
        public void onAgentStateChanged(String sessionId, String agentId, DingRtmAgentClient.AgentState state) {
        }
    
        @Override
        public void onTranscriptionMessage(String sessionId, String agentId, DingRtmAgentClient.TranscriptionData data) {
        }
    });
  4. After you join the meeting, call the StartAgent - Start an agent OpenAPI to have the agent join the meeting and the session.

  5. Other members in the meeting receive an onSessionCreate event notification. The application must then join the same session:

    rtmClient.joinSession(sessionId); // If the session exists, call joinSession to join.
  6. You receive an onAgentUserReady event notification when you join a session that contains an agent or when an agent joins the session you are in. The application layer must maintain the list of agents for the session:

    @Override
    public void onAgentUserReady(String sessionId, String agentId) {
        // The application layer must save the agentId to call agent-related methods.
    }
  7. Use the onAgentStateChanged event to monitor the agent's status changes:

    @Override
    public void onAgentStateChanged(String sessionId, String agentId, DingRtmAgentClient.AgentState state) {
        // Use state to get the current agent status: Listening, Thinking, or Responding.
    }
  8. Send signals to control the agent's behavior, such as prompting the agent to provide a greeting:

    agentClient.sendGreetingReady(sessionId, agentId);

    To manually interrupt the agent:

    agentClient.sendInterrupt(sessionId, agentId);

    In push-to-talk mode, send control signals. Note: Send DingAgentPushToTalkCmdStart before you send DingAgentPushToTalkCmdSend.

    // Makes the agent start accepting user speech input.
    agentClient.sendPushToTalk(sessionId, agentId, AgentPushToTalkCmd.Start);
    ...
    // Makes the agent start processing the user's current speech input data.
    agentClient.sendPushToTalk(sessionId, agentId, AgentPushToTalkCmd.Send);
    ...
    // Cancels the user's current speech input.
    agentClient.sendPushToTalk(sessionId, agentId, AgentPushToTalkCmd.Cancel);
  9. If the local client no longer needs the agent's real-time messaging feature, leave the session:

    rtmClient.leaveSession(sessionId);

Notes

  1. The creation of an agent and its joining of an RTM session are controlled by the server-side StartAgent - Start an agent OpenAPI.

  2. Do not close an RTM session that contains an agent. The session is managed by the server-side agent service OpenAPI.

  3. If a remote user leaves the channel without calling leaveSession, the onSessionRemoteUserLeave event might not be triggered. In this case, when the onRemoteUserOffLineNotify event is received, the application layer must remove that user from the member lists of all sessions.

  4. Set the callback listener for the DingRtmAgentClient instance before joining the meeting to avoid missing callback messages.