Skip to content
This repository has been archived by the owner on Dec 8, 2018. It is now read-only.

Commit

Permalink
fix: Added IAM API key support (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Green authored and germanattanasio committed Jul 26, 2018
1 parent 23c7e0c commit 8d71337
Show file tree
Hide file tree
Showing 29 changed files with 99 additions and 203 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ASSISTANT_PASSWORD=
# OR IAM API key and URL
ASSISTANT_IAM_APIKEY=
ASSISTANT_IAM_URL=
ASSISTANT_URL=https://gateway.watsonplatform.net/assistant/api

###################################################
# Optional params, delete any which are not used! #
Expand Down
273 changes: 88 additions & 185 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,242 +1,145 @@
# Watson Assistant (formerly Conversation) Car Dashboard Demo Application
[![Build Status](https://travis-ci.org/watson-developer-cloud/car-dashboard.svg?branch=master)](http://travis-ci.org/watson-developer-cloud/car-dashboard)
[![codecov.io](https://codecov.io/github/watson-developer-cloud/conversation-simple/coverage.svg?branch=master)](https://codecov.io/github/watson-developer-cloud/car-dashboard?branch=master)
<h1 align="center" style="border-bottom: none;">Watson Assistant (formerly Conversation) Car Dashboard Demo Application</h1>

<h3 align="center">This Node.js app demonstrates the Watson Assistant service in an interface simulating a cognitive car dashboard.</h3>

This application demonstrates how the Watson Assistant service uses intent capabilities in an animated car dashboard UI.

For more information about Watson Assistant, see the [detailed documentation](https://console.bluemix.net/docs/services/conversation/index.html#about).

[See the demo](http://conversation-demo.mybluemix.net/).

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<img src="readme_images/bluemix.png" width="200"/>](#bluemix) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<img src="readme_images/local.png" width="200"/>](#local)

## How the app works
The app interface is designed and trained for chatting with a cognitive car. The interface allows the user to enter input either
using text, in the edit field at the bottom of the UI or via speech via by pressing the mic button.
Your questions and commands are run against a
small set of sample data trained with intents like these:

* `turn_on`
* `weather`
* `capabilities`

These intents help the system to understand variations of questions and commands that you might submit.
For example, if you say *"Wipers on"* or *"I want to turn on the windshield wipers"*, the system
understands that in both cases your intent is the same and responds accordingly.

# <a name="bluemix"></a> Getting Started using IBM Cloud

![](readme_images/deploy-on-bluemix-simple-app.png)

## Before you begin
1. Ensure that you have an [IBM Cloud account](https://console.ng.bluemix.net/registration/).

1. Ensure that you have the necessary space available in your IBM Cloud account. This action deploys 1 application and 3 services.
* You can view this on your IBM Cloud Dashboard. Tiles will show what space you have available.
* For example, for Services & APIS


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/services.PNG)

## Deploy the App
1. Click this button to Deploy to IBM Cloud.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [![Deploy to IBM Cloud](https://bluemix.net/deploy/button.png)](https://bluemix.net/deploy?repository=https://github.com/watson-developer-cloud/car-dashboard)

1. Log in with an existing IBM Cloud account or sign up.

1. Select your Organization, Toolchain Name, Region, and Space, then click the `Deploy` buton.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/deploy.PNG)

* This performs multiple actions:
- Creates the app
- Creates a Watson Assistant service instance that the user needs for workspace creation
- Creates instances for a Speech To Text service and Text To Speech service

* Your `car-dashboard` app is ready now, click `Delevery Pipeline` to deploy your app.
<p align="center">
<a href="http://travis-ci.org/watson-developer-cloud/car-dashboard">
<img alt="Travis" src="https://travis-ci.org/watson-developer-cloud/car-dashboard.svg?branch=master">
</a>
<a href="#badge">
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
</a>
</p>
</p>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/toolchain-ready.png)

1. Once your app has been built and deployed, navagate to your IBM Cloud Dashboard and [import a workspace](#workspace).
For more information about Watson Assistant, see the [detailed documentation](https://console.bluemix.net/docs/services/conversation/index.html#about).

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/deploy-finished.png)
![Demo](readme_images/demo.gif)

## <a name="usingCloudfoundry"></a> Using Cloudfoundry CLI tool to deploy your application
[See the demo](http://conversation-demo.mybluemix.net/).

To build the application:
## Prerequisites

1. Download and install the [Cloudfoundry CLI](https://github.com/cloudfoundry/cli) tool.
1. Sign up for an [IBM Cloud account](https://console.bluemix.net/registration/).
1. Download the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview).
1. Create an instance of the Watson Assistant service and get your credentials:
- Go to the [Watson Assistant](https://console.bluemix.net/catalog/services/conversation) page in the IBM Cloud Catalog.
- Log in to your IBM Cloud account.
- Click **Create**.
- Click **Show** to view the service credentials.
- Copy the `apikey` value, or copy the `username` and `password` values if your service instance doesn't provide an `apikey`.
- Copy the `url` value.

1. Git clone the project `https://github.com/watson-developer-cloud/car-dashboard`
## Configuring the application

1. Navigate to the `car-dashboard` folder
1. In your IBM Cloud console, open the Watson Assistant service instance

1. Connect to IBM Cloud in the command-line tool:
2. Click the **Import workspace** icon in the Watson Assistant service tool. Specify the location of the workspace JSON file in your local copy of the app project:

For US Region
`<project_root>/training/car_workspace.json`

```sh
3. Select **Everything (Intents, Entities, and Dialog)** and then click **Import**. The car dashboard workspace is created.

$ cf api https://api.ng.bluemix.net
4. Click the menu icon in the upper-right corner of the workspace tile, and then select **View details**.

```
5. Click the ![Copy](readme_images/copy_icon.png) icon to copy the workspace ID to the clipboard.

```sh
![Steps to get credentials](readme_images/assistant-simple.gif)

$ cf login -u <your user ID>
6. In the application folder, copy the *.env.example* file and create a file called *.env*

```
cp .env.example .env
```
1. Create the Watson Assistant service in IBM Cloud (our CLI is being updated, for now, use the `create-service` conversation command):

```sh
7. Open the *.env* file and add the service credentials that you obtained in the previous step. The Watson SDK automaticaly locates the correct enviromental variables for either `username`, `password`, and `url` or the `apikey` and `url` credentials found in the *.env* file.
$ cf create-service conversation free watson-assistant-service
Example *.env* file that configures the `apikey` and `url` for a Watson Assistant service instance hosted in the US East region:
```
ASSISTANT_IAM_APIKEY=X4rbi8vwZmKpXfowaS3GAsA7vdy17Qh7km5D6EzKLHL2
ASSISTANT_URL=https://gateway-wdc.watsonplatform.net/assistant/api
```
1. Push it live:

```sh
- If your service instance uses `username` and `password` credentials, add the `ASSISTANT_USERNAME` and `ASSISTANT_PASSWORD` variables to the *.env* file.
$ cf push <application-name>
Example *.env* file that configures the `username`, `password`, and `url` for a Watson Assistant service instance hosted in the US South region:
```
The name you use determinates your application URL initially, such as `<application-name>.mybluemix.net`.

# <a name="local"></a> Getting Started locally

## Before you begin
ASSISTANT_USERNAME=522be-7b41-ab44-dec3-g1eab2ha73c6
ASSISTANT_PASSWORD=A4Z5BdGENrwu8
ASSISTANT_URL=https://gateway.watsonplatform.net/assistant/api
```
1. Ensure that you have an [IBM Cloud account](https://console.ng.bluemix.net/registration/). While you can do part of this deployment locally, you must still use Bluemix.
8. Add the `WORKSPACE_ID` to the previous properties
1. In IBM Cloud, [create a Watson Assistant Service](https://console.bluemix.net/docs/services/conversation/getting-started.html#gettingstarted).
- Copy the Service Credentials for later use.
- [Import a workspace](#workspace)
1. **OPTIONAL**: If you want to use **Text To Speech** and/or **Speech To Text** in your locally runnning app, create a `text-to-speech` service and/or a `speech-to-text` service like you did in step 2.
* Copy the Service Credentials for later use.
```
WORKSPACE_ID=522be-7b41-ab44-dec3-g1eab2ha73c6
```
## Running locally
The application uses [Node.js](http://nodejs.org/) and [npm](https://www.npmjs.com/).

1. Copy the credentials from your `watson-assistant-service` service in IBM Cloud to a `.env` file in the root.
- Look at `.env.example` as an example to create your `.env` file.

1. **OPTIONAL**: If you want to use Text To Speech and/or Speech To Text in your locally runnning app, copy the credentials from your `text-to-speech` service and/or `speech-to-text` service in IBM Cloud to a `.env` file in the root.
* Look at `.env.example` as an example to add to your `.env` file.
1. Install the dependencies
1. Use the Watson Assistant tooling app to create a workspace, as described above, and add the workspace ID environment variable to the `.env` file. For details about obtaining the workspace ID, see Step 6 - 7 in the next section.

1. Install [Node.js](http://nodejs.org/).

1. Open the terminal, go to the project folder, and run this command:
```sh
```
npm install
```
1. Build the UI by running this command:
```sh
npm run build
1. Run the application
```
1. Start the application by running this command:
```sh
npm start
```
1. Open `http://localhost:3000` in a browser.

_Note: If you are interested in deploying you local application or the changes you have made locally to IBM Cloud, go to [this section](#usingCloudfoundry)_

# <a name="workspace"></a> Import a workspace

1. You need to import the app's workspace. To do that, go to the IBM Cloud Dashboard and select the Watson Assistant service instance. Once there, select the **Service Credentials** menu item.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/credentials.png)
1. Select **ADD CREDENTIALS**. Name your credentials then select **ADD**.
1. Return to the **Manage** menu item and select **Launch Tooling**. This opens a new tab in your browser, where you are prompted to login if you have not done so before. Use your IBM Cloud credentials.
1. Download the [exported JSON file](https://raw.githubusercontent.com/watson-developer-cloud/conversation-simple/master/training/car_workspace.json) that contains the Workspace contents.
1. Select **Import**. Browse to (or drag and drop) the JSON file that you downloaded in Step 4. Choose to import **Everything(Intents, Entities, and Dialog)**. Then select **Import** to finish importing the workspace.
1. Refresh your browser. A new workspace tile is created within the tooling. Select the _menu_ button within the workspace tile, then select **View details**:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![Workpsace Details](readme_images/details.PNG)
<a name="workspaceID">
In the Details UI, copy the 36 character UNID **ID** field. This is the **Workspace ID**.
</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ![](readme_images/workspaceid.PNG)
1. Return to your application, either in your local dev environment, or in IBM Cloud. If running on IBM Cloud, you need to [add environment variables](#env).
For more information on workspaces, see the full [Watson Assistant service documentation](https://console.bluemix.net/docs/services/conversation/configure-workspace.html#configuring-a-conversation-workspace).
# <a name="env"></a> Adding environment variables in IBM Cloud
1. In IBM Cloud, open the application from the Dashboard. Select **RUNTIME** on the left side menu.
1. View the application in a browser at `localhost:3000`
1. Select **ENVIRONMENT VARIABLES** and scroll down to **USER-DEFINED**.
## Deploying to IBM Cloud as a Cloud Foundry Application
1. Select **ADD**.
1. Login to IBM Cloud with the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview)
1. Add a variable with the name **WORKSPACE_ID**. For the value, paste in the Workspace ID you [copied earlier](#workspaceID). Select **SAVE**.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/env.png)
1. Restart your application.
# Troubleshooting in IBM Cloud
#### In the Classic Experience:
- Log in to IBM Cloud, you'll be taken to the dashboard.
- Navigate to the the application you previously created.
- Select **Logs**.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/logs.PNG)

- If you want, filter the LOG TYPE by "APP".

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/filter.PNG)

#### In the new IBM Cloud:
- Log in to IBM Cloud, you'll be taken to the dashboard.
- Select **Compute**
```
ibmcloud login
```
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/compute.PNG)
1. Target a Cloud Foundry organization and space.
- Select the application you previously created.
- Select **Logs**.
```
ibmcloud target --cf
```
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/logs1.PNG)
1. Edit the *manifest.yml* file. Change the **name** field to something unique.
For example, `- name: my-app-name`.
1. Deploy the application
- If you want, filter the Log Type by selecting the drop-down and selecting **Application(APP)**.
```
ibmcloud app push
```
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![](readme_images/filter1.PNG)
1. View the application online at the app URL.
For example: https://my-app-name.mybluemix.net
### With CLI
```
$ cf logs < application-name > --recent
```
## License
# License
This sample code is licensed under Apache 2.0.
Full license text is available in [LICENSE](LICENSE).
This sample code is licensed under Apache 2.0.
Full license text is available in [LICENSE](LICENSE).
## Contributing
# Contributing
See [CONTRIBUTING](CONTRIBUTING.MD).
See [CONTRIBUTING](CONTRIBUTING.md).
## Open Source @ IBM
Find more open source projects on the
[IBM Github Page](http://ibm.github.io/).
## Open Source @ IBM
Find more open source projects on the
[IBM Github Page](http://ibm.github.io/).
[demo_url]: http://conversation-simple.ng.bluemix.net/
[doc_intents]: (https://console.bluemix.net/docs/services/conversation/intents-entities.html#planning-your-entities)
[docs]: https://console.bluemix.net/docs/services/conversation/index.html
[docs_landing]: (https://console.bluemix.net/docs/services/conversation/index.html)
[node_link]: (http://nodejs.org/)
[npm_link]: (https://www.npmjs.com/)
[sign_up]: bluemix.net/registration
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"gulp-useref": "^3.0.8",
"helmet": "^3.1.0",
"vcap_services": "^0.2.0",
"watson-developer-cloud": "^3.5.1",
"watson-developer-cloud": "^3.7.0",
"whatwg-fetch": "^2.0.1"
},
"devDependencies": {
Expand Down
Binary file removed readme_images/app_create.PNG
Binary file not shown.
Binary file added readme_images/assistant-simple.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed readme_images/bluemix.png
Binary file not shown.
Binary file removed readme_images/compute.PNG
Binary file not shown.
Binary file added readme_images/copy_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed readme_images/createproject.PNG
Binary file not shown.
Binary file removed readme_images/credentials.png
Binary file not shown.
Binary file added readme_images/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed readme_images/deploy-finished.png
Binary file not shown.
Binary file removed readme_images/deploy-on-bluemix-simple-app.png
Binary file not shown.
Binary file removed readme_images/deploy.PNG
Binary file not shown.
Binary file removed readme_images/details.PNG
Binary file not shown.
Binary file removed readme_images/env.png
Binary file not shown.
Binary file removed readme_images/filter.PNG
Binary file not shown.
Binary file removed readme_images/filter1.PNG
Binary file not shown.
Binary file removed readme_images/importGA.PNG
Binary file not shown.
Binary file removed readme_images/import_intents.png
Binary file not shown.
Binary file removed readme_images/local.png
Binary file not shown.
Binary file removed readme_images/logs.PNG
Binary file not shown.
Binary file removed readme_images/logs1.PNG
Binary file not shown.
Binary file removed readme_images/services.PNG
Binary file not shown.
Binary file removed readme_images/toolchain-ready.png
Binary file not shown.
Binary file removed readme_images/viewyourapp.PNG
Binary file not shown.
Binary file removed readme_images/workspace_details.png
Binary file not shown.
Binary file removed readme_images/workspaceid.PNG
Binary file not shown.
26 changes: 9 additions & 17 deletions routes/conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,14 @@

const AssistantV1 = require('watson-developer-cloud/assistant/v1'); // watson sdk

var assistant;

if (process.env.ASSISTANT_IAM_APIKEY !== undefined && process.env.ASSISTANT_IAM_APIKEY.length > 0) {
assistant = new AssistantV1({
'version': '2018-02-16',
'url': process.env.ASSISTANT_IAM_URL || '<url>',
'iam_apikey': process.env.ASSISTANT_IAM_APIKEY || '<iam_apikey>',
'iam_url': 'https://iam.bluemix.net/identity/token'
});
} else {
assistant = new AssistantV1({
'version': '2018-02-16',
'username': process.env.ASSISTANT_USERNAME || '<username>',
'password': process.env.ASSISTANT_PASSWORD || '<password>'
});
}
/**
* Watson SDK automaticly search for the correct enviromental variables in the .env
* file i.e. username and password or IAM credentials
*/
var assistant = new AssistantV1({
version: '2018-02-16'
});

/**
* Updates the response text using the intent confidence
Expand Down Expand Up @@ -66,7 +58,7 @@ const updateMessage = (input, response) => {
};


module.exports = function(app) {
module.exports = function (app) {

app.post('/api/message', (req, res, next) => {
const workspace = process.env.WORKSPACE_ID || '<workspace-id>';
Expand Down Expand Up @@ -96,4 +88,4 @@ module.exports = function(app) {
return res.json(updateMessage(payload, data));
});
});
};
};

0 comments on commit 8d71337

Please sign in to comment.