智能体回调允许您的应用在特定事件发生时自动触发预设的操作或响应。通过阅读本文,您可以了解如何使用智能体回调。
功能概述
当智能体在运行过程中触发某些事件时,阿里云将主动向您的服务器发送请求。在接收到回调请求后,您可以添加相应的业务逻辑处理。
配置智能体回调
登录智能融媒体控制台,选择您所需要配置的智能体,单击右侧操作列管理。
单击回调配置页签,开启智能体回调配置,勾选您需要配置回调的类型,并配置回调地址及鉴权Token(可选)。
单击确定,完成智能体回调配置。
回调消息体字段说明
名称 | 类型 | 必填 | 描述 | 示例值 |
aiAgentId | String | 是 | 智能体ID。 | xxxxx |
instanceId | String | 是 | 智能体实例的唯一ID。 | 39f8e0bc005e4f309379701645f4**** |
event | String | 是 | 事件类型。
| agent_start |
data | String | 否 | 实际发送数据,如聊天记录等。 | |
code | String | 是 | 服务状态码。 | 1001 |
message | String | 是 | 具体回调信息。 | User has been kicked from the room |
timestamp | String | 是 | 时间戳,表示回调事件的发生时间。 | 2023-10-01T12:00:00Z |
userData | String | 否 | 用户自定义信息。 | |
extendData | String | 否 | 定制化扩展信息。 |
消息体示例:
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"agent_start",
"code":1001,
"message":"User has been kicked from the room",
"timestamp":"2023-10-01T12:00:00Z"
}
事件回调示例
电话呼出状态回调
当event为“outbound_call”,表示回调类型为电话呼出回调;“extendData”内的字段释义如下:
名称 | 类型 | 描述 |
aiAgentId | String | 智能体ID。 |
channelId | String | 智能体实例的唯一ID。 |
instanceId | String | 同主消息体字段。 |
callerNumber | String | 主叫(智能体)电话号码。 |
calleeNumber | String | 被叫电话号码。 |
status | Int | 智能体当前通话状态:
|
callStartTime | String | 电话接通时间。 |
callEndTime | String | 电话挂断时间。 |
hangupRole | Int | 挂断方,0表示主叫,1表示被叫。 |
电话呼出失败
当使用智能体电话呼出功能时,若被叫方空号、拒接、无法接通时,智能体将发送此回调。回调数据结构如下:
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10002,
"message":"Dial status failed",
"timestamp":"2023-10-01T12:00:00Z",
"extenddData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"failReason": -6,
"status": 2
}
}
电话呼出成功
使用智能体电话呼出,被叫方接通通话时,智能体将发送此回调。回调数据结构如下:
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10003,
"message":"Dial status connected",
"timestamp":"2023-10-01T12:00:00Z",
"extenddData":{
"aiAgentId": "0d31c************b3c787",
"channelId": "XXX",
"instanceId": "39f8e0bc005e4f309379*********",
"callerNumber": "XXX",
"calleeNumber": "XXX",
"status": 3
}
}
用户挂断
使用智能体电话呼出,被叫方接通后挂断电话,智能体将发送此回调。回调数据结构:
{
"aiAgentId":"0d31c************b3c787",
"instanceId":"39f8e0bc005e4f309379*********",
"event":"outbound_call",
"code":10004,
"message":"Remote hangup",
"timestamp":"2023-10-01T12:00:00Z",
"extenddData":{
"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
}
}
工作流状态回调
当event为类型为工作流状态回调时,“extendData”内的字段释义如下:
名称 | 类型 | 描述 |
channelId | String | 房间ID信息。 |
sentenceId | Int | 问答ID信息。 说明 对于用户的同一次提问,智能体回答会使用相同的ID信息。 |
requestTimestamp | String |
|
responseTimestamp | String |
|
Server 示例
Python
from aiohttp import web
import json
from loguru import logger
async def handle_post(request):
"""
处理POST请求并记录接收到的数据。
"""
# 获取请求头中的Authorization
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:
# 从请求体中获取数据
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:
# 如果JSON解析失败,则返回错误响应
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)
回调事件状态码
状态码 | 回调事件 | 回调信息 |
1001 | 智能体启动 | AI Agent starts. |
1002 | 智能体终止 | AI Agent ends. |
1003 | 会话建立 | Session starts |
4001 | 数字人并发路数超额 | Concurrent routes exhausted |
4002 | 智能体被踢出房间 | User has been kicked from the room |
4003 | 智能体token有误 | Invalid token for the AI agent |
4004 | 智能体订阅音频流失败 | Failed to pull stream for the AI agent |
4005 | 第三方ASR失败 | error description |
4006 | 数字人服务不可用 | Avatar service is not available |
8001 | Intent recognized event | |
8002 | LLM data received event | |
8003 | TTS data received event | |
10002 | 电话呼出失败 | Dial status failed |
10003 | 电话呼出成功 | Dial status connected |
10004 | 用户挂断(电话呼出) | Remote hangup |