Connecting to AI Bot

Feature Overview

After successfully integrating the TRTC SDK, joining a room, and starting a voice conversation with the AI Bot, you need to run the connect command to connect to the corresponding AI Bot to interact with it further. Once connected, you can not only receive real-time status transitions such as "Listening", "Thinking", and "Speaking", but also receive subtitles, interrupt the Bot, and use visual understanding.

Connecting to the AI Bot

Running the connect Command
Call callExperimentalAPI with a connect command in json format to connect to the corresponding AI Bot:
{
"api": "aiConversation",
"params": {
"command": "connect",
"robotid": "ai_bot_001"
}
}
Here, robotid is the AgentConfig.UserId specified when you create an AI conversation task through the REST API.
Getting the Command Result
To check whether the connection succeeded, listen for the TRTCCloudListener.onExperimentalEvent callback, where eventName is onAIConversationEvent and params is:
{
"event": "onCommand",
"robotid": "ai_bot_001",
"command": "connect",
"errCode": 0,
"errMsg": "ok"
}
errCode
Meaning
0
Connection successful.
-2
Parameter error: missing robotid.
2001
Task does not exist or has ended.
2002
SDK version is too low.

Getting the AI Bot's Status

Once connected, you can get the current status of the AI Bot via the TRTCCloudListener.onExperimentalEvent callback, where eventName is onAIConversationEvent and params is:
{
"event": "agentState",
"robotid": "ai_bot_001",
"data": {
"type": 10102,
"robotid": "ai_bot_001",
"sender": "tts_bot_001",
"payload": {
"taskid": "task-id",
"state": {
"code": 2,
"desc": "thinking"
},
"timestamp": 1751196060800
}
}
}
Field
Type
Description
event
String
Always agentState.
robotid
String
UserId of the currently connected AI bot.
data.type
Number
Message type; bot status is 10102.
data.robotid
String
UserId of the AI conversation bot (AgentConfig.UserId).
data.sender
String
Sender's UserId. For status messages, this is the corresponding TTS bot.
data.payload.taskid
String
AI conversation task ID.
data.payload.state.code
Number
Current status code. See table below.
data.payload.state.desc
String
Status description.
data.payload.timestamp
Number
UTC timestamp of the status change, in milliseconds.
Note:
data.payload (Type: Object, Description: Status payload) and data.payload.state (Type: Object, Description: Status object) are nested objects.
state.code
Meaning
0
Idle
1
Listening
2
Thinking
3
Speaking
4
Interrupted
5
Finished Speaking

Disconnecting from the AI Bot

After the conversation ends, call callExperimentalAPI with a disconnect command in json format to disconnect from the AI Bot:
{
"api": "aiConversation",
"params": {
"command": "disconnect"
}
}