我正在使用沃森对话api构建一个天气机器人。
每当用户发送'的天气是什么时候。我得到了意图和实体的回复。现在我打电话给天气api并得到回复。如何将此天气响应传递回要显示的watson对话框?
我认为我必须通过上下文对象发送响应,但是如何调用会话api来传递响应呢?
我正在使用python api。
答案 0 :(得分:3)
在这种情况下,来自IBM官方文档的API Referecence显示了如何在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中执行您想要的操作。