Skip to Content
Technical Articles
Author's profile photo Daniel Wroblewski

Write a quick webhook in Kyma for your chatbot

There was a question about using scripting to do some manipulation of the memory, as well as more sophisticated looping through memory data. I suggested using a webhook — I had written a tutorial about easily writing a Python webhook in SAP Cloud Platform (now SAP BTP).

But then someone suggested you could write and deploy a very fast webhook in Kyma, by creating a function in Node.js or Python and exposing it with an API rule.

So I decided to write a quick webhook, base don the question, to learn how to do it. So here it is.

NOTE: My coding (and other technical aspects of this blog) might not be the best, and I may not have used the Kyma features in the best way. Please feel free to comment how to improve this, especially to teach me some of the Kyma features, APIs, security tools and other stuff available in the environment, and generally why to use Kyma.

I would especially be interested in how best to debug such scripts, since I basically made errors and then ran script, got Internal Server Error, and then had to search by hand for the mistake.

Enable Kyma

I opened my trial account, and entered my subaccount (trial).

On the overview page, there is a button to enable your Kyma environment.

You only have to supply a name for your cluster (I entered dbw). BTP then needs to be left alone for about an hour to set up the environment.

After the Kyma environment is created, add the necessary permissions:

  1. Go to SecurityRole Collections.
  2. Click on the KymaRuntimeNamespaceAdmin role collection.
  3. Click Edit, add your account email, and click Save.
  4. Do the same for the KymaRuntimeNamespaceDeveloper.

You may need to refresh the page. After setting the roles, I still got a permission error but eventually it went away, either by refreshing or logging back in.


Create Function

In your subaccount, go to Instances and SubscriptionsEnvironments, and then select your Kyma environment.

On the right opens a panel with information about the environment. Click Go to Dashboard.

Open your default namespace, and click Functions.

Click Create Function, accept the defaults, and click Create.

I created a quick function that would accept the webhook request body, and would do what was sought in the Looping through an Array question.

  main: function (event, context) {
    var memory =["conversation"]["memory"]
    var facts_counter =  memory["facts_counter"]
    var facts_cat = memory["facts_cat"];

    for (i = facts_counter; i < memory["facts"].length; i++) {
      if (memory["facts"][i]["cat"] == facts_cat) {
          memory["facts"][i]["seen"] = 1;

    var reply = {replies : [{type : "text", content : "No match"}]};

    if (i < memory["facts"].length) {
      reply["replies"][0]["content"] = memory["facts"][i]["value"];    
      reply["conversation"] = { "memory" : memory  };
    return JSON.stringify(reply);

The first part gets information from the default request body sent when you create a webhook, as described in Body Configuration.

The second part does some calculations, and the last part creates the JSON response body that is required from a webhook to create the response, as described in Getting Response Using Webhook.

I’m a newbie in all the Kyma stuff. I got some information about what objects I can access in my script at Serverless Kyma documentation.


Expose Function

In the Kyma dashboard, go to API Rules, and click Create API Rule.

  • Enter a rule name.
  • Enter a host name.
  • Select your function.
  • Click Create.

This creates a URL for accessing your function. You can click it to run the function, or use Postman. If you started by creating the default Hello World function, this would print text to the browser, which is a good way to test everything before you write your full function.

Call from Chatbot

Finally, to use the webhook, add a webhook action in your chatbot. Here you can see that I set the data in the memory, and then called the webhook, which by default sends the conversation JSON as the request body, as described in Body Configuration.

All in all, it was a little easier than how I deployed my Python script via the Cloud Foundry CLI.

See all tutorials in the SAP Tutorial Navigator

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.