在这种情况下,来自 IBM 官方文档的 API 参考,展示了一个如何在 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。