【问题标题】:IBM Watson Conversation & IBM Cloud Functions : User Input For ParametersIBM Watson Conversation 和 IBM Cloud Functions:参数的用户输入
【发布时间】:2018-12-23 17:58:53
【问题描述】:

我已经在 IBM Cloud Functions 中创建了一个函数,但是如何实现来自用户输入的参数?

我想做的是

  • 例如:当用户输入“我需要产品”/“立即购买产品”/显示产品时。 product 输入作为参数实现到我的 Cloud Function 中,它显示所有使用 product 作为关键字的产品。
  • 响应文本将从云函数返回输出(这是一个 JSON 数组)中获取信息
    • (res.body.items[?].name)

来自 IBM 的示例布局:

{
    "context": {
      "variable_name" : "variable_value"
    },
    "actions": [
      {
        "name":"getProducts",
        "type":"client | server",
        "parameters": {
          "<parameter_name>":"<parameter_value>"
        },
        "result_variable": "<result_variable_name>",
        "credentials": "<reference_to_credentials>"
      }
    ],
    "output": {
      "text": "response text"
    }
  }

【问题讨论】:

    标签: ibm-cloud watson-conversation ibm-cloud-functions


    【解决方案1】:

    有一个 full tutorial I wrote available in the IBM Cloud docs,其中包含 IBM Cloud Functions 和一个后端数据库。代码在 GitHub 上的这个存储库中提供:https://github.com/IBM-Cloud/slack-chatbot-database-watson/

    这是来自workspace file 的相关部分,它显示了如何将参数传递给函数:

    {
          "type": "response_condition",
          "title": null,
          "output": {
            "text": {
              "values": []
            }
          },
          "actions": [
            {
              "name": "_/slackdemo/fetchEventByShortname",
              "type": "server",
              "parameters": {
                "eventname": [
                  "<? $eventName.substring(1,$eventName.length()-1) ?>"
                ]
              },
              "credentials": "$private.icfcreds",
              "result_variable": "events"
            }
          ],
          "context": {
            "private": {}
          },
    

    稍后,结果会呈现出来,例如,以这种方式:

    "output": {
            "text": {
              "values": [
                "ok. Here is what I got:\n ```<? $events['result'] ?>```",
                "Data:\n ``` <? $events['data'] ?> ```"
              ],
              "selection_policy": "sequential"
            },
            "deleted": "<? context.remove('eventDateBegin') ?><? context.remove('eventDateEnd') ?> <? context.remove('queryPredicate') ?>"
          },
    

    当然,可以通过迭代结果来完成一些更高级的格式化。 Some tricks are here。该代码还展示了如何使用子节点来处理结果并清除上下文变量。

    要获取参数,在您的情况下是产品名称或类型,您需要访问input string 并找到“产品”之后的部分。另一种方法是使用专为此类情况设计的 beta 功能“contextual entity”。

    【讨论】:

    • 现在,当产品名称传入参数时,我的 Cloud Function 已经提供了我需要的产品。有没有一种简单的方法可以在使用 Intent 后立即获取用户输入?例如:("I need product" / "Buy product") 意图是“I need”或“buy”,Watson Conversation 获取的字符串是就在那个意图之后?
    • 我在答案中添加了更多内容。
    猜你喜欢
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多