【问题标题】:How to pass response from external api to dialog in watson conversation?如何将来自外部 api 的响应传递给 watson 对话中的对话?
【发布时间】:2017-04-19 05:43:11
【问题描述】:

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

每当用户发送“天气如何”时。我收到了带有意图和实体的回复。现在我调用一个天气 api 并得到响应。如何将此天气响应传递回要显示的 watson 对话框?

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

我正在使用 python api。

【问题讨论】:

    标签: python ibm-watson watson watson-conversation


    【解决方案1】:

    在这种情况下,来自 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多