IBM Watson对话与交流IBM Cloud Functions:参数的用户输入

编程入门 行业动态 更新时间:2024-10-10 12:25:09
本文介绍了IBM Watson对话与交流IBM Cloud Functions:参数的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have already created a function in IBM Cloud Functions, but how would I implement the parameters from user input?

我想做的是

What I'm trying to do is

  • 例如:当用户键入我需要产品"/立即购买产品"/向我显示产品时.将 product 输入作为参数并将其实现到我的Cloud Function中,该函数显示所有使用 product 作为关键字的产品.
  • 响应文本将从Cloud Function返回输出(这是一个JSON数组)中获取信息
    • (res.body.items [?].name)
    • For example: When a user types in "I need product" / "Buy product now" / Show me products. The product input is taken as a parameter and implements it into my Cloud Function, which displays all products that uses product as a keyword.
    • The response text would get info from the Cloud Function return output (which is a JSON array)
      • (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文档中编写了完整的教程,其中包含IBM Cloud Functions和后端数据库.该代码在GitHub的此存储库中提供: github /IBM-Cloud/slack-chatbot-database-watson/.

      There is a full tutorial I wrote available in the IBM Cloud docs which features IBM Cloud Functions and a backend database. The code is provided on GitHub in this repository: github/IBM-Cloud/slack-chatbot-database-watson/.

      这是工作区文件,其中显示了如何将参数传递给函数:

      Here is the relevant part from the workspace file that shows how a parameter could be passed into the function:

      { "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": {} },

      稍后,例如以这种方式显示结果:

      Later on, the result is presented, e.g., in this way:

      "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 fancier formatting can be done, of course, by iterating over the result. Some tricks are here. The code also shows how to use a child node to process the result and to clear up context variables.

      要获取参数(如果您使用的是产品名称或类型),则需要访问输入字符串,然后找到产品"之后的部分.另一种方法是使用测试版功能"上下文实体"专为此类情况设计.

      To obtain the parameter, in your case a product name or type, you would need to access either the input string and find the part after "product". Another way is to use the beta feature "contextual entity" which is designed for such cases.

更多推荐

IBM Watson对话与交流IBM Cloud Functions:参数的用户输入

本文发布于:2023-11-26 19:23:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   用户   Watson   IBM   Functions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!