Dialogflow CX Conversations
Manage conversations and sessions in Google Dialogflow CX via REST API for testing and interaction handling.
Prerequisites
- - Google Cloud project with Dialogflow CX API enabled
- Service account or OAuth credentials with Dialogflow API access
- INLINECODE0 CLI authenticated OR bearer token
Authentication
Option 1: gcloud CLI (recommended)
CODEBLOCK0
Option 2: Service Account
CODEBLOCK1
API Base URL
CODEBLOCK2
Regional endpoints available:
- -
https://{region}-dialogflow.googleapis.com (e.g., us-central1, europe-west1)
Common Operations
Detect Intent
CODEBLOCK3
Match Intent (no state change)
CODEBLOCK4
Create Test Case
CODEBLOCK5
Key Resources
| Resource | Description |
|---|
| Sessions | Conversation instances with state |
| Detect Intent |
Process user input and get responses |
|
Test Cases | Automated conversation testing |
Quick Reference
For detailed API reference:
Scripts
- -
scripts/conversations.py — CLI wrapper for conversation operations
Usage
CODEBLOCK6
Tips
- - Session IDs can be any unique string (e.g., UUID)
- Use detectIntent for full conversation flow, matchIntent for testing without state changes
- Test cases help validate conversation logic before deployment
Dialogflow CX 对话管理
通过REST API管理Google Dialogflow CX中的对话和会话,用于测试和交互处理。
前提条件
- - 已启用Dialogflow CX API的Google Cloud项目
- 具有Dialogflow API访问权限的服务账号或OAuth凭据
- 已认证的gcloud CLI或持有bearer令牌
身份认证
选项1:gcloud CLI(推荐)
bash
gcloud auth application-default login
TOKEN=$(gcloud auth print-access-token)
选项2:服务账号
bash
export GOOGLE
APPLICATIONCREDENTIALS=/path/to/service-account.json
TOKEN=$(gcloud auth application-default print-access-token)
API基础URL
https://dialogflow.googleapis.com/v3beta1
可用的区域端点:
- - https://{region}-dialogflow.googleapis.com(例如:us-central1、europe-west1)
常用操作
检测意图
bash
curl -X POST \
https://dialogflow.googleapis.com/v3beta1/projects/${PROJECT
ID}/locations/${LOCATION}/agents/${AGENTID}/sessions/${SESSION_ID}:detectIntent \
-H Authorization: Bearer ${TOKEN} \
-H Content-Type: application/json \
-d {
queryInput: {
text: {
text: Hello
},
languageCode: en
}
}
匹配意图(不改变状态)
bash
curl -X POST \
https://dialogflow.googleapis.com/v3beta1/projects/${PROJECT
ID}/locations/${LOCATION}/agents/${AGENTID}/sessions/${SESSION_ID}:matchIntent \
-H Authorization: Bearer ${TOKEN} \
-H Content-Type: application/json \
-d {
queryInput: {
text: {
text: Hello
},
languageCode: en
}
}
创建测试用例
bash
curl -X POST \
https://dialogflow.googleapis.com/v3beta1/projects/${PROJECT
ID}/locations/${LOCATION}/agents/${AGENTID}/testCases \
-H Authorization: Bearer ${TOKEN} \
-H Content-Type: application/json \
-d {
displayName: Greeting Test,
testCaseConversationTurns: [
{
userInput: {
input: {
text: {
text: Hi
}
}
},
virtualAgentOutput: {
textResponses: [
{
text: [Hello!]
}
]
}
}
]
}
关键资源
处理用户输入并获取响应 |
|
测试用例 | 自动化对话测试 |
快速参考
详细的API参考文档:
脚本
- - scripts/conversations.py — 对话操作的CLI封装
使用方法
bash
python scripts/conversations.py detect-intent --agent AGENT_NAME --text Hello
python scripts/conversations.py match-intent --agent AGENT_NAME --text Hello
提示
- - 会话ID可以是任意唯一字符串(例如UUID)
- 使用detectIntent进行完整对话流程,使用matchIntent进行不改变状态的测试
- 测试用例有助于在部署前验证对话逻辑