Use agent callbacks to automatically trigger preset actions or responses in your application when specific events occur.
Overview
When an AI agent triggers certain runtime events, Alibaba Cloud sends a callback request to your server. You can then add business logic to process the request.
Configure agent callbacks
-
Log on to the Intelligent Media Service console. Select the agent that you want to configure and click Manage in the Actions column.
-
On the Callback Configuration tab, enable agent callbacks, select the callback types, and enter the Callback URL and an optional Authentication Token.
NoteThis token is sent in the
Authorizationfield of the request header. Your server should verify this token to ensure request security.Available callback types include: Agent status callbacks, Workflow status callbacks, Real-time chat history callbacks, Hang-up intent detection callbacks, Outbound call status callbacks, Inbound call status callbacks, Custom client message callbacks, and Instruction callbacks. If your callback URL supports both HTTP and HTTPS, HTTPS is strongly recommended for enhanced security.
-
Click OK to complete the callback configuration.
Callback payload fields
|
Parameter |
Type |
Required |
Description |
Example |
|
aiAgentId |
String |
Yes |
The agent ID. |
xxxxx |
|
instanceId |
String |
Yes |
The unique ID of the agent instance. |
39f8e0bc005e4f309379701645f4**** |
|
event |
String |
Yes |
The event type.
|
agent_start |
|
data |
Json |
No |
The data payload. The structure of this field depends on the event type. |
|
|
code |
String |
Yes |
1001 |
|
|
message |
String |
Yes |
The callback message. |
User has been kicked from the room |
|
timestamp |
String |
Yes |
The time the event occurred, formatted as an ISO 8601 string (UTC). |
2023-10-01T12:00:00Z |
|
userData |
String |
No |
User-defined information. |
|
|
extendData |
Json |
No |
Custom extension data. |
Callback examples
Outbound call status
When the event is outbound_call, this callback reports the status of an outbound call. The following table describes the fields in the extendData object for this event.
|
Parameter |
Type |
Description |
|
aiAgentId |
String |
The AI agent ID. |
|
channelId |
String |
The channel ID. |
|
instanceId |
String |
The unique ID of the AI agent instance. |
|
callerNumber |
String |
The phone number of the caller (the AI agent). |
|
calleeNumber |
String |
The phone number of the callee. |
|
failReason |
Int |
The failure reason. This field is returned only when the outbound call fails. |
|
status |
Int |
The current call status of the AI agent. Possible values are:
|
|
callStartTime |
String |
The time the call was connected. This field is returned only upon hangup. |
|
callEndTime |
String |
The time the call was hung up. This field is returned only upon hangup. |
|
hangupRole |
Int |
The party that hung up the call. This field is returned only upon hangup. Possible values are:
|
|
forwardInfo |
JSON |
Information about the call transfer. This field is returned only for call transfer-related callbacks. It contains the following subfields:
|
Outbound call failure
This callback indicates that an outbound call failed because the callee's number was invalid, the call was rejected, or the callee was unreachable.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10002,
"message":"Dial status failed",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"failReason": -6,
"status": 2
}
}
Outbound call connected
This callback is sent when the callee answers an outbound call.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10003,
"message":"Dial status connected",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 3
}
}
Callee hangup
This callback is sent when the callee hangs up after an outbound call connects.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10004,
"message":"Hangup",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 4,
"callStartTime": "2023-10-01T12:00:00.135045+00:00",
"callEndTime": "2023-10-01T12:01:00.135045+00:00",
"hangupRole": 1
}
}
Agent hangup
This callback is sent when the AI agent hangs up after an outbound call connects.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10004,
"message":"Hangup",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 4,
"callStartTime": "2023-10-01T12:00:00.135045+00:00",
"callEndTime": "2023-10-01T12:01:00.135045+00:00",
"hangupRole": 0
}
}
Successful call transfer
This callback is sent when a call transfer connects successfully.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10006,
"message":"Forward call connected",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 3,
"forwardInfo": {
"callerNumber": "XXX",
"calleeNumber": "XXX",
"callStartTime": "2023-10-01T12:00:59Z"
}
}
}
Call transfer failed
This callback is sent when a call transfer fails.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10005,
"message":"Forward call failed",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"failReason": 480,
"status": 2,
"forwardInfo": {
"callerNumber": "XXX",
"calleeNumber": "XXX"
}
}
}
Hangup after call transfer
This callback is sent when the party who received the transfer hangs up.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10004,
"message":"Hangup",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 4,
"callStartTime": "2023-10-01T12:00:00.135045+00:00",
"callEndTime": "2023-10-01T12:01:00.135045+00:00",
"hangupRole": 2,
"forwardInfo": {
"callerNumber": "XXX",
"calleeNumber": "XXX",
"callStartTime": "2023-10-01T12:00:59Z"
}
}
}
Inbound call status
When the event is inbound_call, this callback reports the status of an inbound call. The following table describes the fields in the extendData object for this event.
|
Parameter |
Type |
Description |
|
aiAgentId |
String |
This value is the same as the top-level |
|
channelId |
String |
The channel ID. |
|
instanceId |
String |
This value is the same as the top-level |
|
callerNumber |
String |
The phone number of the caller (the party making the inbound call). |
|
calleeNumber |
String |
The phone number of the callee (the AI agent). |
|
failReason |
Int |
The failure reason. This field is returned only when the inbound call fails. |
|
status |
Int |
The current call status of the AI agent. Possible values are:
|
|
callStartTime |
String |
The time the call was connected. This field is returned only upon hangup. |
|
callEndTime |
String |
The time the call was hung up. This field is returned only upon hangup. |
|
hangupRole |
Int |
The party that hung up the call. This field is returned only upon hangup. Possible values are:
|
|
forwardInfo |
JSON |
Information about the call transfer. This field is returned only for call transfer-related callbacks. It contains the following subfields:
|
Inbound call connected
This callback is sent when the AI agent successfully answers an inbound call.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"inbound_call",
"code":10003,
"message":"Dial status connected",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 3
}
}
Inbound call failed
This callback is sent when the AI agent fails to answer an inbound call.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"inbound_call",
"code":10002,
"message":"Dial status failed",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"failReason": -6,
"status": 2
}
}
Inbound call hangup
This callback is sent when the AI agent hangs up after an inbound call connects.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"inbound_call",
"code":10004,
"message":"Hangup",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 4,
"callStartTime": "2023-10-01T12:00:00.135045+00:00",
"callEndTime": "2023-10-01T12:01:00.135045+00:00",
"hangupRole": 0
}
}
Successful call transfer
This callback is sent when a call transfer from an inbound call connects successfully.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"inbound_call",
"code":10006,
"message":"Forward call connected",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 3,
"forwardInfo": {
"callerNumber": "XXX",
"calleeNumber": "XXX",
"callStartTime": "2023-10-01T12:00:59Z"
}
}
}
Call transfer failed
This callback is sent when a call transfer from an inbound call fails.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"inbound_call",
"code":10005,
"message":"Forward call failed",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"failReason": 480,
"status": 2,
"forwardInfo": {
"callerNumber": "XXX",
"calleeNumber": "XXX"
}
}
}
Hangup after call transfer
This callback is sent when the party who received the transfer hangs up after a transfer from an inbound call.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"inbound_call",
"code":10004,
"message":"Hangup",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 4,
"callStartTime": "2023-10-01T12:00:00.135045+00:00",
"callEndTime": "2023-10-01T12:01:00.135045+00:00",
"hangupRole": 2,
"forwardInfo": {
"callerNumber": "XXX",
"calleeNumber": "XXX",
"callStartTime": "2023-10-01T12:00:59Z"
}
}
}
Workflow status
For workflow status events, the extendData object contains the following fields:
|
Parameter |
Type |
Description |
|
channelId |
String |
The channel ID. |
|
sentenceId |
Int |
A unique ID for a conversational turn. Note
The AI agent's responses to a single user query share the same |
|
requestTimestamp |
String |
|
|
responseTimestamp |
String |
|
Instruction callback
When the event type is instruction, the callback indicates that a specific action instruction tag has been triggered. Supported instruction callbacks include:
Call transfer instruction callback
This callback is sent when the AI agent triggers a call transfer action tag.
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"instruction",
"code":11001,
"message":"Forward call triggered",
"timestamp":"2023-10-01T12:00:00Z",
"extendData":{
"triggerTime": "2023-10-01T12:00:00Z"
}
}
Server example
Python
from aiohttp import web
import json
from loguru import logger
async def handle_post(request):
"""
Handle POST requests and log the received data.
"""
# Get the Authorization header from the request.
authorization_header = request.headers.get('Authorization')
if authorization_header is None or not authorization_header.startswith('Bearer fixed-token'):
logger.error("Unauthorized request")
return web.Response(status=401, text='Unauthorized')
try:
# Parse the request body as JSON.
callback_data = await request.json()
logger.info("Parsed JSON data:")
logger.info(json.dumps(callback_data, indent=4))
return web.Response(text='Callback received successfully', status=200)
except json.JSONDecodeError:
# Return an error if JSON parsing fails.
return web.Response(text='Invalid JSON', status=400)
app = web.Application()
app.add_routes([web.post('/', handle_post)])
if __name__ == '__main__':
web.run_app(app, host='localhost', port=8081)
Callback event status codes
|
Status code |
Callback event |
Description |
|
1001 |
Agent starts |
Agent starts. |
|
1002 |
Agent stops |
Agent stops. |
|
1003 |
Session starts |
Session starts. |
|
4001 |
Concurrent agent routes exhausted |
The maximum number of concurrent agent routes has been reached. |
|
4002 |
Agent kicked from channel |
The system kicked the agent from the channel. |
|
4003 |
Invalid agent token |
The agent's token is invalid. |
|
4004 |
Agent stream subscription failed |
The agent failed to subscribe to the stream. |
|
4005 |
Third-party ASR failed |
The third-party ASR service failed. |
|
4006 |
Avatar service unavailable |
The avatar service is unavailable. |
|
8001 |
Intent recognized |
Intent recognized. |
|
8002 |
LLM data received |
LLM data received. |
|
8003 |
TTS data received |
TTS data received. |
|
10002 |
Dial status failed |
The call failed to connect. |
|
10003 |
Dial status connected |
The call connected successfully. |
|
10004 |
Hangup |
The call was hung up. |
|
10005 |
Forward call failed |
The call forwarding attempt failed. |
|
10006 |
Forward call connected |
The forwarded call connected successfully. |
|
11001 |
Forward call triggered |
The system triggered a call forward. |