如何将来自外部 api 的响应传递给 watson 对话中的对话?

How to pass response from external api to dialog in watson conversation?

我正在使用 watson 对话构建一个天气机器人 api。

每当用户发送 'what is the weather of '。我得到了带有意图和实体的回复。现在我打电话给天气 api 并得到回应。如何将此天气响应传回 watson 对话框以显示?

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

我正在使用 python api。

在这种情况下,API参考来自 IBM 官方文档,展示了一个如何在 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,而要像 Watson 一样发送消息,您可以使用 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