Date post: | 22-Jan-2018 |
Category: |
Technology |
Upload: | alexis-gion |
View: | 630 times |
Download: | 0 times |
IBM Watson Workspace
How to develop a bot
from A to Z
Today’s mission
Create an IBM Workspace App that listens to the conversation and can take action when needed.
Agenda
• Workstation setup
• Node.js & npm
• Using Express
• Workspace App registration
• Create your first Workspace Post
• Webhooks
• Move to Bluemix
• Start listening to your space
Workstation Setup
• Write your code in any text editor you want. I’ll be using ATOM in this presentation.https://atom.io/
• Node.js & npmhttps://nodejs.org/en/download/current/
• Bluemix command line tools (cli)• Cloudfoundry cli
https://github.com/cloudfoundry/cli/releases
• Bluemix clihttp://clis.ng.bluemix.net/ui/home.html
• Postman to testhttps://www.getpostman.com/
Resources needed
• You can type some things manually this day, but all the code is also available as a zip file
• For every part we work on there’s a folder which builds upon the previous one, so the last folder contains the final code we’ll reach today.
https://ibm.ent.box.com/v/iww-course
• open source, cross platform runtime environment• built on Google Chrome’s Javascript V8 engine• used with npm (node package manager) to manage external libraries• single threaded but highly scalable• open source• initial release : 2009
Node.js
Node.js ... hello world
• Create a folder for your project (anywhere)
• Open a command prompt and navigate to this folder
• Issue the command :npm init
• Give it a name (lowercase), all the rest can be left default
• Result is a new file in your folder called package.json
package.json
• Describes your node.js project
• Holds all dependencies you (optionally) add
Let’s write a bit of code
• Open your text editor and navigate to your folder
• with atom – after installing the atom command line tools – you can launch it from the command line by typing :atom .(the dot will open Atom in the current folder)
Let’s write a bit of code
• Create a file called index.js
• Type your first javascript code ...
• Go back the command prompt and run itnode index.js
• Project of the Node.js foundation• Most popular library to create a web/application server on Node.js• Started by Strongloop developer• open source• initial release : 2010
Express.js
Adding a library to our project
• use npm (node package manager) to download and install packages
• command :npm install <package name> -Sornpm install <package name> --save
• The –S flag will add the package to the dependencies in the package.json
Install Express in our app
• In our folder, run the commandnpm install express –S
• after installing• you will have a folder called
node_modules in your app directory
• the package.json file will have been updated with this dependency.
Get Express going
Run it
• Create a folder public in your project folder and put some static html content in there (or use the content from the resources)
• Start the express server
Make a REST API server
• Add new package to your project : body-parsernpm install body-parser –S
• Add code to• start listening to /test-message
• handle JSON using express “middleware” (with body-parser)
Middleware
Let’s try it
Workspace App Registration
Save the App Key !!!!!
Create a Test Space and add your App
Let’s make the app post a message to your space
We need code to :
• Create an authentication token
• Write the message to the space
We will need :
• two new packages to do REST calls from our servernpm install request –Snpm install request-json –S (easier to use for json than request)
• Code from the resources ... no need to reinvent the wheel, this is something you can re-use every time. use code from 4-NodeHelloBot folder
Try it
• Don’t forget to put your app id & secret into the APP_xxx variable !
• Don’t forget to put your space id into the SPACE_ID variable !
Time to see this in action
Webhooks
We need our server to be accessible over the internet ...
... so let’s push it to Bluemix first !
(of course, Bluemix is not a requirement. If you have another way to make your server accessible, go for it !)
Move to Bluemix
• Bluemix accounthttps://console.ng.bluemix.net/
• Make sure you know your• username / password
• ORG name
• Space name
Bluemix Push
There are many ways, this is how I do it
• Create a manifest.yml file
• Create a small script with the commands require to push your code to Bluemix
Configure this for your environment
• Manifest file :• Update the name as you wish
• host + domain is your fully qualified hostname, so host needs to be available
• domain for UK Bluemix is ‘eu-gb.mybluemix.net’
• Script file• Make sure it’s executable on Mac or Linux
• update the second command with• your username (-u)
• your ORG name (-o)
• your space name (-s)
• package.json ... see next slide !!!
Bluemix requires a run statement in package.json
Bluemix needs to know how to start your application
(it needs to know that we issued ‘node index.js’)
One more thing ...
As the package.json holds all dependencies, it’s not necessary to upload the ’node_modules’ folder, Bluemixcan take care of that as well.
Add a “.cfignore” file to your folder and add the files/folders which don’t need to be uploaded to Bluemix.
Execute !
Back to the Webhooks
• Add the hostname of your server and add a path on which the workpaceplatform will call your server.(in the example ‘/callback’)
• At the time of registration the hostname needs to be resolvable in DNS.
• Choose an event to which you want to “listen”
• Hit Save
Capture the Webhook Secret !!!
The webhook is currently inactive, we need to create some code on our server to activate it.
For this, we need the Webhooksecret, so make sure to copy it !
Let’s get the webhook enabled
We need code to :
• Start listening to the webhook callback URI (/callback in our case)
• Perform the webhook verification
We will need :
• a new packages to create the security tokennpm install crypto –S
• Code from the resources ... no need to reinvent the wheel, this is something you can re-use every time. use code from 6-NodeWebhooks folder
Enable the webhook
When the code update has been done :• Start the server locally to check that you don’t have any
errors.• Push your code to bluemix• Enable the webhook :
Check your logs
Let our bot react to a keyword
• Look to the code in folder 7-NodeBotReply.
• Use it ... update if you want
• Push to Bluemix
• Try !!!
Let’s add a bit of Watson ...
• IBM Work Services will process your conversation and make the results of the analysis available to you through annotations
• Edit your webhook
• Start listening to annotations
Update your code
• React on the “message-annotation-added” eventType.
• See folder 8-NodeBotSentiment for an example.