Virtual Agent 🤖 with Fire 🔥 and Forget Skill — Serverless Action ⛅️

Blumareks
8 min readOct 14, 2021

This blog post is about connecting your Virtual Assistant — aka Chatbot — with Serverless Action via Webhook. The technology utilized here is IBM Watson Assistant, and IBM Cloud Functions — aka managed Apache OpenWhisk. The Serverless Action will be written in Python, but also it can be written in other languages like Javascript, Java, PHP, and even Swift.

In this blog you will be able to create a PayGo account in IBM Cloud, spin free services in IBM Cloud: Watson Assistant, and IBM Cloud Functions. You will connect them in the fire & forget process. And the entire process will take 10 minutes. Are you up to the challenge?

You are going to take the following 4 steps:

  1. Sign up/sign in to IBM Cloud
  2. Create a Serverless Action
  3. Expose an API based on the Serverless action
  4. Create a Watson Assistant service, and write a simple dialog
  5. Connect dialog to the exposed API via a Webhook

Let’s start!

Introduction

In 2020 I published a book on Serverless Swift with Apress. I am a great promoter of Serverless technology. And as I was asked by one of my business partners to develop a solution to create a Fire and Forget skill I thought that would be a great example for Serverless technlogy. Well, having a hammer everything seems like a nail, eh.

I this blog post I am going to use one of the general use cases — using API Gateway in order to connect API to your the front end system. Check this screen shot of the page from my book (the analog version of it).

Serverless Swift — check out the basic use case that you are going to use here.

You can learn more about Serverless technology, API Gateway, and why and when it is great to use Serverless for small orgs, startup and in enterprises, in my book — check the link at the end of this article.

Sign up/sign in to IBM Cloud

First you might want to set up your environment in IBM Cloud. There are plenty of free services, and you are going to use only those here. Use the following link to sign up:

cloud.ibm.com

Cloud.IBM.com — Signing up to IBM Cloud

As soon as you verify your email, and acknowledge the rights you will see the Dashboard of IBM Cloud.

Your Data on IBM Cloud is protected.

This is the Dashboard — from here you will start to spin new services, or tune the existing ones.

The Dashboard — you will use Catalog, the menu (the hamburger top left 🍔)

Congratulations! You have created an IBM Cloud account. You are going to create the cloud native “hello world” 🌎 🌏 🌍 next. Keep on reading!

Create a Serverless Action

Creating a Serverless Action is super simple process. Click on the menu (the hamburger 🍔 ), and then select Functions ((f)).

Accessing FaaS — Serverless Actions in IBM Cloud

In this process you will use most of the time defaults — however feel free to change things to adapt them to your needs. Look for my other blog posts and github repositories for the tips on how to do it.

This is the entry screen when you enter functions.
It might be required to create a namespace. This process allows you to create the IAM based namespace. There is also alternative way to create a CF based namespace (check my book for that)
Well, you might want to keep the defaults here.
So now, you can return to the entry screen — and click Start Creating.
Select Quickstart Templates
and select the Hello World template
Type a new package name, you might want to select Python from language drop down list. There are more languages available.
Your code template would show up, you might want to add parameters {“name”:”Serverless Swift”} using regular quotes and then click Invoke.
The first call every 5+ minutes will be longer — this is the cold start, every one right after will be warmed up — and would take less time — read more about it in Serverless Swift.

Congratulations! Your first Cloud Native Serverless Hello World is done! ✅ Now let’s make an API from it via API Gateway — read on.

Expose an API based on the Serverless action

As soon as your service is working now you are ready to expose it thru the IBM API Gateway. This is the best practice to hide, and protect your internal Cloud Functions with a free service of IBM API Gateway. There are couple reasons for that, like security, limits of calls per a period of time, grouping of several actions under a single API to name a few.

Follow the steps here:

Select APIs — the first time you call it this is the screen you would see.
Create a new API — give it a name, and the base path.
Create an operation as the POST method (you will pass the parameters!)
And pick the previously created action from the hello-world package (see the previous screenshots), and the name of the action “helloworld”. The response is “application/json”. Hit create!
Review your action…
and hit Create the final time. (adding the authentication, limits might be very much needed in API monetization approach — that is another subject.
There are monitoring capabilities available for your APIs. Check the Review and Test tab.
Please note (and copy) the full link to the API call including trailing “/hello” 👋
You might want to test it — the default hello world example looks for the “name” parameter in the invoking JSON.
This screen demonstrates that the API is ready and working!

Congratulations! 🎉 Your API from Serverless Action is working now.

Create a Watson Assistant service, and write a simple dialog

As soon as the backend is ready — you are ready to spin the Watson Assistant service — and create a small dialog, in which you would leverage the Function as a service to register the email of your customer.

Go to Catalog and search for Watson Assistant service to spin the Lite (free) version of the service.

From the IBM Cloud Dashboard or anywhere really, click Catalog.
You can create a single LITE free of charge service per account — that is all you need for now.

This would create a service — you can launch it from the Launch Watson Assistant button:

Create an assistant, and then a skill:

Create a skill:

First you need to create an intent (an action INTENT for the operation that the user would invoke on your backend):

You might want a user to be able to add an email to the newsletter list.

Add a user example for Watson to train on the following phrases:

Add other intent examples:

Add an entity “email” — now let’s allow a user to pass the variable — the @email

Select My Entities, and create a new one

provide the value “email” and the pattern detecting a proper email syntax (check other pattern examples here: https://cloud.ibm.com/docs/assistant?topic=assistant-entities#entities-patterns):

Other useful patterns in the documentation
\b[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b
Add a new “@email” entity.

As soon as the Intent and the entity are created build a dialog by adding a new node in the Dialog tab:

Connect dialog to the exposed API via a Webhook

In order to continue you need to select the Webhook tab. You will paste it in the Webhook field the API url as shown:

mind the trailing “/hello”

Above you will paste your API call for Serverless Action (mind the trailing name of the action — shown on the second screenshot here) — go to previous section if you need to revise what was the link to be pasted here.

Now you need to add the Webhook to the Dialog. Select the Dialog tab, then the node for the interaction with the backend — through the cog in the Dialog as shown here you are able to enable webhooks! (I also enabled Slots — since I am going to add also other parametrs to my API request).

Now one of the slots should be an email — the entity you need to use in this case is @email with a postfix .literal : @email.literal as it is shown on the example below:

Do not forget to add `.literal` to your @email.literal reference for the slots!

Now you are ready to add the Webhook, and pass the reference to the name parameter of the Serverless API.

(as you called them in the action — in your hello-world example you were asking for “name”). So here it goes:

And now you can test it with the Dialog.

Just for the sake of testing see what variables are stored in the Context in the Try it out window:

I hope you can collect urls, emails, and other patterns — that you would use with Serverless and other Backends.

Congratulations! 🎉 You connected your Watson Assistant based virtual agent to Serverless backend via Webhooks!

Before you leave…

Let me know if that worked for you — just clap! 👏 That would make me very happy. Read my other Python and Serverless action here: https://blumareks.medium.com/automate-web-forms-with-selenium-and-serverless-python-77f8203b4a22

And last, but not least:

You might want to checkout my book on Serverless Swift — https://www.apress.com/us/book/9781484258354

Thank you for reading this blog post/book. Follow me on Twitter: http://twitter.com/blumareks

--

--

Blumareks

I am a technology advocate for autonomous robots, AI, mobile and Internet of Things - with a view from both the enterprise and a robotics startup founder.