+ All Categories
Home > Technology > How to develop a bot from A to Z

How to develop a bot from A to Z

Date post: 22-Jan-2018
Category:
Upload: alexis-gion
View: 630 times
Download: 0 times
Share this document with a friend
40
IBM Watson Workspace How to develop a bot from A to Z
Transcript
Page 1: How to develop a bot from A to Z

IBM Watson Workspace

How to develop a bot

from A to Z

Page 2: 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.

Page 3: How to develop a bot from A to Z

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

Page 4: How to develop a bot from A to Z

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/

Page 5: How to develop a bot from A to Z

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

Page 6: How to develop a bot from A to Z

• 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

Page 7: How to develop a bot from A to Z

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

Page 8: How to develop a bot from A to Z

package.json

• Describes your node.js project

• Holds all dependencies you (optionally) add

Page 9: How to develop a bot from A to Z

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)

Page 10: How to develop a bot from A to Z

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

Page 11: How to develop a bot from A to Z

• 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

Page 12: How to develop a bot from A to Z

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

Page 13: How to develop a bot from A to Z

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.

Page 14: How to develop a bot from A to Z

Get Express going

Page 15: How to develop a bot from A to Z

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

Page 16: How to develop a bot from A to Z

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)

Page 17: How to develop a bot from A to Z

Middleware

Page 18: How to develop a bot from A to Z

Let’s try it

Page 19: How to develop a bot from A to Z

Workspace App Registration

Page 20: How to develop a bot from A to Z

Save the App Key !!!!!

Page 21: How to develop a bot from A to Z

Create a Test Space and add your App

Page 22: How to develop a bot from A to Z

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

Page 23: How to develop a bot from A to Z

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 !

Page 24: How to develop a bot from A to Z

Time to see this in action

Page 25: How to develop a bot from A to Z

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

Page 26: How to develop a bot from A to Z

Move to Bluemix

• Bluemix accounthttps://console.ng.bluemix.net/

• Make sure you know your• username / password

• ORG name

• Space name

Page 27: How to develop a bot from A to Z

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

Page 28: How to develop a bot from A to Z

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 !!!

Page 29: How to develop a bot from A to Z

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

Page 30: How to develop a bot from A to Z

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.

Page 31: How to develop a bot from A to Z

Execute !

Page 32: How to develop a bot from A to Z

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

Page 33: How to develop a bot from A to Z

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 !

Page 34: How to develop a bot from A to Z

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

Page 35: How to develop a bot from A to Z

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 :

Page 36: How to develop a bot from A to Z

Check your logs

Page 37: How to develop a bot from A to Z

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 !!!

Page 38: How to develop a bot from A to Z

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

Page 39: How to develop a bot from A to Z

Update your code

• React on the “message-annotation-added” eventType.

• See folder 8-NodeBotSentiment for an example.


Recommended