DevNet 1056 WIT Spark API and Chat Bot Workshop

Preview:

Citation preview

Intro to Sparkan introduction to REST APIs and Creating Spark Bots

Adrienne Moherek and Tessa MeroDEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 3

• REST API Basics:• What is an API?• Why APIs?• How does it work?

• Tour of Cisco Spark API• Interactive Documentation Hands-on• Postman Hands-on• Python examples

Agenda Part 1 (REST Basics and Spark)

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

• What are Chat Bots?• What are the Benefits?• Integrations vs Bots• Getting Started with a Collaboration API• Create a Chat Bot• Using Cloud9 (c9.io)• Create Webhook Events• Test Your Bot!

Agenda Part 2 (Build a Spark Bot)

DEVNET-1056 4

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Who Are We?

DEVNET-1056 5

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Cisco DevNet

• What is Cisco DevNet?• What is the Purpose?• Why do we go to events?

• We’re on Facebook and Twitter @CiscoDevNet

• Free tools and sandboxes

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

What is an API?

7Presentation ID

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 8Presentation ID

Application Programming Interface

”It’s a way for two pieces of software to talk to each other”

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

For a long time.. Humans were the only users

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

For a long time.. Humans were the only users

1. User asks for data or takes action by interacting with UI

2. Software displays results in User Interface (UI)

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

But what about when the user is another software system….

1. Software asks for data or takes action by interacting with API

2. Software returns results via API

My Software System

Your Software System

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Why APIs?

12Presentation ID

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Why are API’s so important?

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

API Growth from 2000 to 2016

2000 2005 2007 2009 2011 2013 2015 20160

5000

10000

15000

20000

25000

30000

35000API's

API's

Source: 2016 data from nordicapis.com. 2005-2015 data from rubenverborgh.github.io and 2000-2004 data from blog.cutter.com

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

The API is the User Interface for software systems

15Presentation ID

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

APIs are sets of requirements that govern how one application can talk to another.

16Presentation ID

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

An API is like an electrical outlet.What would it be like to power a hair dryer without an outlet?- Open wall- Unsheath wires- Splice wires

together- Understand all the

wires in the wall

The outlet is a service that conforms to specifications.- sockets deliver 120

volts of alternating current (AC) operating at 60Hz

- Sets expectation on behalf of consuming devices and provider.

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

An API is like …

An API (Application Programming Interface) is best thought of as a contract provided by one piece of computer software to another.

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

APIs help developers create apps that benefit the end user.

1. Yelp asks for Map Data

2. Google Maps returns map data

via API

Users sees list of

restaurants close to

them

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 20Presentation ID

-- Programmable Web

APIs are often referred to as “an engine of innovation.”

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Web Pages, Requests, Response & REST APIs

21Presentation ID

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 22

View a Web Page

Presentation ID

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 23

View a Web Page

Presentation ID

Request GET /index.htm HTTP/1.1

ResponseHTTP 200 OK

<html>

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Good• 200 OK• 201 Created• 204 No Content

Common HTTP Status CodesBad• 400 Bad Request• 403 Forbidden• 404 Not Found• 500 Internal Server

Error• 550 Permission

Denied

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

REST APIs use Request and Response to

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 26

Get Data using an API

Presentation ID

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 27

Get Data using an API

Presentation ID

Request GET /rooms

ResponseHTTP 200 OK

{json data}

API Consumer API Provider

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Example Spark API Flow

1. My App asks Spark for members

in a room

2. Spark returns member data via

API

4. Users sees diff of users on

team and in room

Cisco Spark

3. My App Compares

members of a room with team

members

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 29DEVNET-1056

Methods

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

What is so great about REST?

30

• Rooms• People• Memberships• Messages

Easy to use:

• In mobile apps• In console apps• In web apps

Spark REST APIs

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Setup with Spark

31DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 32

Let’s get your computer setup1. Create an account with Spark - https://web.ciscospark.com

2. Login to the Spark app - https://web.ciscospark.com ORDownload the Spark app - https://www.ciscospark.com/downloads.html

3. Bookmark the Spark developer documentation - https://developer.ciscospark.com(You will need to login to use the interactive documents)

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Cisco Spark for DevelopersTour of Cisco Spark APIs

33DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 34

Cisco Spark

Secure & Open

Complete & Simple

Spark for DevelopersUser Integrations, APIs/SDKs, etc.

Spark Hybrid ServicesCloud + Prem

Partner ServicesInterconnect

Message Meeting Call

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 35

‘Your App’ now with

Cisco Collab!

Spark APIs Extend Cisco Collaboration Cloud

GET

POST

DELETE

PUT /Rooms

/Memberships

/Messages

/Teams

/People

/Webhooks

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 36

Cisco Spark API v1/People /Rooms /Membership /Messages /Teams /Webhooks

List People List Rooms List Memberships List Messages List Teams List Webhooks

Create a Room Create a Membership Create a Message Create a Team Create a Webhook

Get Person Details Get Room details Get Membership details Get Message details Get Team details Get Webhook details

Update a Room Update a Membership Update a Team Update a Webhook

Delete a Room Delete a Membership Delete a Message Delete a Team Delete a Webhook

GET

POST

DELETE

PUT

DELETE DELETE DELETE

PUT PUT

POST POST POST

GET GET GET

GET GET

GET

GET GETGET

DELETE

PUT

POST

GET

GET

DEVNET-1056

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Demo: List Rooms

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

For a long time.. Humans were the only users

1. User asks for data or takes action by interacting with UI

2. Software displays results in User Interface (UI)

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 39

View your Spark roomsIf this is your first time using Spark, you will only see 1 Spark room

1. What is the name of the room?

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

But what about when the user is another software system….

1. Software asks for data or takes action by interacting with API

2. Software returns results via API

My Software System

Your Software System

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 41

List Spark Rooms

Presentation ID

Request GET /rooms

ResponseHTTP 200 OK

{json data}

API Consumer API Provider

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 42DEVNET-1056

1. Login to Spark Developer Docshttps://developer.ciscospark.com

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 43DEVNET-1056

2. Click on Documentationhttps://developer.ciscospark.com

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 44DEVNET-1056

3. Click on Rooms and then click on List Roomshttps://developer.ciscospark.com

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 45DEVNET-1056

4. Turn on Test Modehttps://developer.ciscospark.com

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Method URL

Response

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 47DEVNET-1056

5. Click on runhttps://developer.ciscospark.com

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Method URL

Response

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Demo: Create a room

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

For a long time.. Humans were the only users

1. User asks for data or takes action by interacting with UI

2. Software displays results in User Interface (UI)

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 52

Create a Spark roomLet’s create a new Spark room

1. Ask 2 people next to you for the email addresses they used for Spark

2. Click on the “+” and enter in their email addresses

3. Then type them a message

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

But what about when the user is another software system….

1. Software asks for data or takes action by interacting with API

2. Software returns results via API

My Software System

Your Software System

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 54

Create a New Room

Presentation ID

Request POST /rooms

ResponseHTTP 200 OK

{json data}

API Consumer API Provider

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

MethodURL

Response

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 56DEVNET-1056

Did it work? Go to the Spark app and see!https://web.ciscospark.com

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Add people to your room

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Try to add yourself to your room What happened?

Add your neighbor to your roomWhat happened

Challenge: List memberships for room

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 60DEVNET-1056

Cisco Spark for DevelopersSpark API calls in Python

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 61

Using Python to Make Requests

• A benefit of REST is that you can invoke Spark from any language

• Code it yourself

• Or generate code from Postman

• OAuth 2 to inject user API token

Do It Yourself … with Postman code assistants

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 62

Using Python to Make Requests

1. Generate a python skeleton from Postman, 2. Run from the command line

Do It Yourself … with Postman code assistants

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Using Postman to make API request

63DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 64

http://getpostman.com• Want to code along? Install POSTMAN now• Free tool to forge API requests, examine responses

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Same API Call in

POSTMAN

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

method

url

requestheaders

status code

response headers

content-type

responsebody

query parameters

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Find your Spark Developer TokenLogin to developer.ciscospark.com

Copy Developer Token

Developer Token

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 68

JSON syntax• square brackets for lists

• curly braces for objects

• name/value pairs for properties, separated by commas

• pick an online editor

• http://www.jsonlint.com

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Q & A

DEVNET-1056 69

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 70DEVNET-1056

Build a Spark Bot

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

• What are Chat Bots?• What are the Benefits?• Integrations vs Bots• Getting Started with a Collaboration API• Create a Chat Bot• Using Cloud9 (c9.io)• Create Webhook Events• Test Your Bot!

Agenda (Build a Spark Bot)

DEVNET-1056 71

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 72

• Cisco Spark API Account: https://developer.ciscospark.com/

• Create Cloud9 Account: http://c9.io• JSON Viewer: http://tiny.cc/json-viewer

• C9 Alternative (local tunnels): Localtunnel.me or ngrok.com

What You Will Need:

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 73

Objective

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 74DEVNET-1056

What are Chat Bots?

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 75

Clippy!

DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 79DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 80DEVNET-1056

What are the Benefits?

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 81DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1056 82

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1056 83

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 84DEVNET-1056

Integrations vs Bots

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

An integration acts as YOU and can see and do

the things you can do.

DEVNET-1056 85

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1056 86

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1056 87

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

A bot appears as another member of the room, but can only see messages

that mention it.

DEVNET-1056 88

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 89DEVNET-1056

Getting Started with a Collaboration API

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Have 2 things open in your browser:

1. web.ciscospark.com

2. Developer.ciscospark.com

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Creating a Chat Bot

91DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Click on “My Apps” and Add New (+)

DEVNET-1056 92

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Select ”Create a Bot”

DEVNET-1056 93

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Add a Bot Image

http://bit.ly/SparkBot-512x512

DEVNET-1056 94

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1056 95

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Copy API Access Token

DEVNET-1056 96

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Copy API Access Token

DEVNET-1056 97

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 98DEVNET-1056

Deploy a bot on Cloud9

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

c9.io/signup

DEVNET-1056 99

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1056 100

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

A closer look at the Github URL:

https://github.com/CiscoDevNet/node-sparkbot-samples.

DEVNET-1056 101

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Install Node.js in Cloud9• In Cloud9, look at the shell terminal at the bottom.

• Type: “npm install” – The nodejs libraries used by our code sample get downloaded to your Cloud9 Workspace

• Now go to helloworld.js under “examples”

• Click “Run” at the top

DEVNET-1056 103

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Create Variables• Click the ENV button at the bottom right of the Cloud9

Terminal

• Create two variables:NAME VALUE

DEBUG sparkbotSPARK_TOKEN yourtokenkeyhere

DEVNET-1056 104

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Make Your Application Public to Share

DEVNET-1056 106

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Create Webhook events

107DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1056 108

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Create Cisco Spark Webhooks

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

200 Success!• Click “Run”

• See a “200/success” displayed – webhook assigned to unique identifier

• Fired off every time a new message is added to a room your bot is a member of

DEVNET-1056 110

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Create Another Webhook• Receive an event every time our bot is added to a room• Modify “resource” field: replace “messages” with

“memberships”

• Click “Run” again

• See a ”200 / success” message!

DEVNET-1056 111

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Test your Cisco Spark Bot

112DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Test Bots Response – type /hello

DEVNET-1056 113

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Mention your bot in Group rooms

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Type /hello to test Group chat with bot

DEVNET-1056 115

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Go Further?• Add a few breakpoints in helloworld.js, restart in debug mode• Add some commands. Find samples here:

https://github.com/CiscoDevNet/node-sparkbot-samples• Note: Lots of nodejs bot frameworks exist for Cisco Spark,

from basic to advanced in various languages.

DEVNET-1056 116

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public

Complete Your Online Session Evaluation

Don’t forget: Cisco Live sessions will be available for viewing on-demand after the event at CiscoLive.com/Online

• Please complete your Online Session Evaluations after each session

• Complete 4 Session Evaluations & the Overall Conference Evaluation (available from Thursday) to receive your Cisco Live T-shirt

• All surveys can be completed via the Cisco Live Mobile App or the Communication Stations

117DEVNET-1056

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 118

Continue Your Education• Demos in the Cisco campus

• Walk-in Self-Paced Labs

• Lunch & Learn

• Meet the Engineer 1:1 meetings

• Related sessions

DEVNET-1056

Thank You

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1056 119