Virtual Agent 🤖 with Fire 🔥 and Forget Skill — Serverless Action ⛅️
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:
- Sign up/sign in to IBM Cloud
- Create a Serverless Action
- Expose an API based on the Serverless action
- Create a Watson Assistant service, and write a simple dialog
- 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).
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
As soon as you verify your email, and acknowledge the rights you will see the Dashboard of IBM Cloud.
This is the Dashboard — from here you will start to spin new services, or tune the existing ones.
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)).
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.
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:
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.
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):
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
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):
\b[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b
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:
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:
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