0

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

  • 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)

Example layout from 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"
    }
  }
data_henrik
  • 16,724
  • 2
  • 28
  • 49
John C
  • 517
  • 2
  • 6
  • 16

1 Answers1

0

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: https://github.com/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.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • Right now my Cloud Function already gives the product I need when a product name is passed into the parameters. Is there an easy way to just get a user input right after an Intent used? For example: ("I need _product_" / "Buy _product_") The intent would be "I need" or "buy," and Watson Conversation gets the String that is right after that Intent? – John C Jul 16 '18 at 09:34
  • I added some more to the answer. – data_henrik Jul 16 '18 at 09:51