要使用用于Dialogflow v2的Python客户端发送文本响应,您可以按照以下步骤进行操作:
pip install dialogflow
import dialogflow_v2 as dialogflow
from google.api_core.exceptions import InvalidArgument
def detect_intent_texts(project_id, session_id, texts, language_code):
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session))
for text in texts:
text_input = dialogflow.TextInput(text=text, language_code=language_code)
query_input = dialogflow.QueryInput(text=text_input)
try:
response = session_client.detect_intent(
request={'session': session, 'query_input': query_input}
)
except InvalidArgument:
raise
print('Query text: {}'.format(response.query_result.query_text))
print('Detected intent: {} (confidence: {})\n'.format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence))
print('Fulfillment text: {}\n'.format(
response.query_result.fulfillment_text))
project_id = 'YOUR_PROJECT_ID'
session_id = 'YOUR_SESSION_ID'
texts = ['Hello', 'How are you?']
language_code = 'en-US'
detect_intent_texts(project_id, session_id, texts, language_code)
请注意,您需要将YOUR_PROJECT_ID
替换为您的Dialogflow代理的项目ID,YOUR_SESSION_ID
替换为您的会话ID。
领取专属 10元无门槛券
手把手带您无忧上云