2017-04-19 82 views
1

我正在使用watson会话api构建天气机器人。如何将外部API的响应传递给watson对话中的对话框?

每当用户发送'什么是天气'。我收到意向和实体的回应。现在我打电话给天气api并获得回应。如何将这个天气响应传递回watson对话框来显示?

我认为我必须通过上下文对象发送响应,但是如何调用对话api来传递响应?

我正在使用python api。

回答

3

在这种情况下,来自IBM官方文档的API Referece显示了如何在Watson Conversation Service中发送消息的一个示例。

检查这个例子:

import json 
from watson_developer_cloud import ConversationV1 

conversation = ConversationV1(
    username='{username}', 
    password='{password}', 
    version='2017-04-21' 
) 

# Replace with the context obtained from the initial request 
context = {} 

workspace_id = '25dfa8a0-0263-471b-8980-317e68c30488' 

response = conversation.message(
    workspace_id=workspace_id, 
    message_input={'text': 'Turn on the lights'}, 
    context=context 
) 

print(json.dumps(response, indent=2)) 

在这种情况下,从用户发送信息,你可以使用message_input,并像沃森发送消息时,您可以使用output。 如果你的参数设置为response,例如,你可以使用:

#Get response from Watson Conversation 
responseFromWatson = conversation.message(
    workspace_id=WORKSPACE_ID, 
    message_input={'text': command}, 
    context=context 
) 

IBM Developers见一个正式的示例代码:

if intent == "schedule": 
      response = "Here are your upcoming events: " 
      attachments = calendarUsage(user, intent) 
     elif intent == "free_time": 
      response = calendarUsage(user, intent) 
     else: 
      response = responseFromWatson['output']['text'][0] //THIS SEND THE MESSAGE TO USER 

    slack_client.api_call("chat.postMessage", as_user=True, channel=channel, text=response, 
         attachments=attachments) 

使用此派:

response = responseFromWatson['output']['text'][0]; 
if intent == "timeWeather": 
     response = "The Weather today is: " +yourReturnWeather 

来自IBM Developer这个项目的教程here

该示例将与Slack集成,但您可以看到一个很好的示例,在project中完成了您想要的操作。

参见official documentation

相关问题