Category Archives: watson

Messenger2Watson(1): Connect Facebook to a Watson Chatbot

In Slack2Watson(1) and (2) I created a chatbot and integrated it into Slack Slash Commands. Now, let’s integrate the same chatbot into Facebook Messenger. To accomplish this I need to create a Facebook Application, add the Messenger Platform to my Facebook Application, create a Webhook, have admin access to a Facebook Page to generate a Page Access Token so I can send and receive messages send to the Facebook Page, and create the Node-RED flows to integrate the Facebook Messenger with the IBM Watson Conversation.

Steps:

  1. Create a Flow to Verify the Request for Webhook Edits in Node-RED,
  2. Create an Endpoint for the Redirect URL of the Webhook in Node-RED,
  3. Create a Facebook Application for the Messenger Platform,
  4. Enable Webhooks Integration with Node-RED,

Create a Flow to Verify the Request for Webhook Edits in Node-RED

To prepare the setup and configuration of the Facebook Application, the Messenger platform and Webhooks to enable a chatbot in Facebook Messenger, I will first create the Node-RED flows to implement the required server workflow.

The first flow is to verify the endpoint for the setup of the Facebook Application, using the ‘hub.challenge’ token.

  • Go to your Node-RED application on Bluemix at http://<username>-nodered-slackapp.mybluemix.net/,
  • Click the ‘Go to your Node-RED flow editor’ button,
  • If you’re not logged in yet, log in now,
  • Add a new flow tab and rename the flow ‘Facebook Messenger’,
  • To verify your endpoint during setup of your Webhook, or when you update an existing topic subscription of your Webhook, Facebook sends a GET request. The request will include:
    hub.mode=subscribe
    hub.challenge — a random string
    hub.verify_toke
  • Continue reading

Print Friendly, PDF & Email

Slack2Watson(2): Connect Slack to a Watson Chatbot with Node-RED

Using:

  • Slash Commands in Slack,
  • Watson Conversation service to create a ChatBot,
  • Node-RED to configure the Application Flow,

Using a chatbot, I want to automate the following scenario:

  1. user: Hello
  2. chatbot: Hello. Where are you?
  3. user: hi, i am at the Rubin Museum.
  4. chatbot: I love the Rubin Museum. Shall I give you some recomendations of my personal favorites?
  5. user: oh yes, I would love some recommendations.
  6. chatbot: do you like sculpture, paintings or ornaments?
  7. user: i prefer paintings!
  8. chatbot: ok, here are my favorite paintings at the Rubin Museum: a, b, c

Steps:

  1. Create the Watson Conversation for the Rubin Museum Scenario,
  2. Test the Conversation for the Rubin Museum Scenario,
  3. Setup Slash Commands in Slack,
  4. Create the Node-RED Flow to Watson Conversation,
  5. Add Token Validation of the Slack Request to Node-RED Flow,

Create the Watson Conversation for the Rubin Museum Scenario

  • Go to IBM Bluemix and login to your account,
  • To create the Watson Conversation, click the ‘Create Service’ button, which will take you to the Catalog, or
  • Go to the Catalog, Under ‘Services’ filter by ‘Watson’, or in the catalog browse to the Watson section,
  • Click the ‘Conversation’ service,
  • Agree or change the ‘Service name’ and the ‘Credentials name’, and click the ‘Create’ button,
  • Under the ‘Credentials’ tab, you will find the username, password, and workspace ID that you need later to configure access to the conversation service,
  • Click the green ‘Launch tool’ button,
  • First create a workspace, click ‘Create’, name the workspace ‘Watson2Slack-Workspace’, and click ‘Create’,
  • You are now in your Conversation workspace, and you should see 3 tabs: Intents, Entities, and Dialog,

    Continue reading
Print Friendly, PDF & Email

Creating a Nodejs App with Watson AlchemyAPI and CloudantDB

WIP

Requirements:

Overview:

  1. Setup
  2. Setup Details
  3. Add Watson AlchemyAPI
  4. Add Cloudant DB

Setup

  1. Login to Bluemix, in a separate tab login to Github,
  2. In Bluemix open Catalog->Boilerplates and create a ‘Node.js Cloudant DB Web Starter’ Boilerplate, named ‘<username>-nodejs-app1’, click CREATE
  3. Go to ‘Overview’, scroll down to ‘Continuous delivery’ and click ‘Enable’
  4. In the ‘Toolchain Settings’, change the name of the Toolchain to ‘<username>-nodejs-app1’, and click Create,
  5. If the Github THINK or CODE icon displays a configuration error, from the block’s dropdown icon in the topright, click the ‘Configure’ link to correct the configuration of your Github account,
  6. Go to your Github account and make sure that the repository for the new Bluemix app is created successfully,
  7. Copy the Git URL, on localhost open a commandline terminal, change to your development directory and clone the new repository,
    cd ~/dev/src/bluemix
    git clone https://github.com/remkohdev/remkohdev-nodejs-app1.git
    cd remkohdev-nodejs-app1
  8. Open the project directory in your favorite editor,
  9. Open the ‘package.json’ file and change the ‘name’ property to ‘<username>-nodejs-app1’,
  10. From the commandline, in your project directory,
    git status
    git add .
    commit -m "change package.json name property"

Continue reading

Print Friendly, PDF & Email

Getting Started: Creating a Java-Liberty App with Watson on Bluemix

An extended version of this tutorial, adding a Cloudant NoSQL Database and D3js data visualization, is available here.

Requirements:

  • Bluemix account
  • Github account

Steps:

  1. Create the StarterApp
  2. Add the Toolchain or Continuous Integration (CI)
  3. REST API Primer
  4. Authentication in REST API
  5. Getting the Bluemix Configuration
  6. Add the AlchemyData News API Client
  7. Add a Web Form
  8. Implement the AlchemyData News API Request
  9. Create Authorization Header for Basic Auth

1. Create the StarterApp

Continue reading

Print Friendly, PDF & Email

Creating a Java-Liberty App with Watson AlchemyAPI, CloudantDB and D3js

Requirements:

  • Git
  • Github account
  • Java
  • Eclipse for J2EE
  • WebSphere Liberty with JavaEE7
  • WebSphere Liberty plugin for Eclipse
  • Add *.cloudant.com.crt to the WepSphere Liberty Profile (wlp) server’s Java Key Store (JKS)
  • Optional: WAS AdminCenter 1.0
  • Optional: Maven

Steps:

  1. Create the StarterApp
  2. Add the Toolchain or Continuous Integration (CI)
  3. Setup Localhost
  4. Add JSPs and Servlet for News Search
  5. Add REST API that calls the AlchemyData News API
  6. Save the AlchemyData News Search Results in CloudantDB
  7. Add a D3js Sentiment Score Graph
  8. Configure the CloudantDB for Querying
  9. Show History of Search Results

1. Create the StarterApp

  1. Sign in to Bluemix.net,
  2. Go to Catalog,
  3. Under ‘Boilerplates’, click the ‘Java Cloudant Web Starter’,
  4. For ‘App name’ and ‘Host name’ enter ‘<username>-liberty-watson’,
  5. Click the ‘Create’ button,
  6. In ‘Application Details’ click the ‘Overview’ link,
    bluemix_application_details
  7. Review the application configuration,
  8. Click ‘Connections’ and click ‘Connect New’ to create the following services:
  9. If no AlchemyAPI service already exists, create a new AlchemyAPI service by clicking the ‘Connect New’ button,
  10. from the Catalog, filter by ‘Watson’, select the AlchemyAPI service, click the ‘Create’ button, and click ‘Restage’
  11. Note: by default you can only create 1 instance of the AlchemyAPI service under your organization in a single space, creating a second instance or connecting an existing service in another space will cause an error. If an instance already exists, click the ‘Connect Existing’ instead, select the existing AlchemyAPI service and click the ‘Connect’ button, click ‘Restage’,

2. Add the Toolchain or Continuous Integration (CI)

Continue reading

Print Friendly, PDF & Email

Creating Sentiment Line Chart for the News with Watson, Python, and D3js

Requirements:
You must have Python installed. Check to see if you have Python installed from the commandline:
python --version

Table of Contents:

  1. Create a Starterapp
  2. Git Clone and Setup
  3. Create Additional Folders and Files
  4. Commit and Push Changes to Repository, Build and Deploy
  5. Create an AlchemyAPI service
  6. Create a Cloudant NoSQL service
  7. Add the Basic Workflow for Request-Response
  8. Get News using AlchemyAPI
  9. Create Helper Functions
  10. Save Responses in Cloudant
  11. Parse Response for D3js
  12. Draw the Line Chart in D3js

You can import AlchemyAPI requests into Postman with this Postman collection.

The source code for the application can be viewed or cloned from Github.

1. Create a Starterapp

  1. Go to Catalog > Boilerplates
  2. Click the ‘Python Flask’ starterapp
  3. For name enter <username>-newssentiment
  4. Go to Overview
  5. Under ‘Continuous Integration’ click ‘Add GIT Repo and Pipeline’ to add a DevOps platform, select ‘Populate the repo with the starterapp packageand enable Build & Deploy pipeline’ > Click Continue > Click ‘CLOSE’.
  6. Click ‘EDIT CODE’.
  7. The very first time you login to the ‘DevOps’ environment you will need to pick a username for the ‘DevOps’ environment.
  8. In the left menu of icons, click the top folder icon, and click ‘Git URL’ to copy the Git repository url.
  9. If you prefer to edit in the online editor in Bluemix, click ‘EDIT CODE’ button and then click the second pencil icon in the left menu of icons.
  10. I will continue to work on localhost instead.

Continue reading

Print Friendly, PDF & Email

Calling a Watson service in Java

Overview

Note: this API needs to be updated to v2. Identify is now a resource on the Language Translation API.
http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/language-translation/api/v2/

Making an HTTP REST API POST request in Java is in principle as simple this:
Request request = Request.Post(serviceURI)
.addHeader("Authorization", basicAuthorization)
.bodyString(body, ContentType.APPLICATION_FORM_URLENCODED);
Executor executor = Executor.newInstance();
String response = executor.execute(request).returnContent().asString();

This code, uses the Apache Fluent API, which exposes only the essentials of the Apache HTTPClient.

The Authorization header is required for authentication by the Watson API, and is a base64 encoded string.
String auth = username + ":" + password;
String basicAuthorization = "Basic "+ Base64.encodeBase64String(auth.getBytes());

The POST request is “x-www-form-urlencoded” and in Java consists of a List<NameValuePair>:
List<NameValuePair> params = new ArrayList<NameValuePair>();

Continue reading

Print Friendly, PDF & Email

Call IBM Watson APIs to add Machine Learning to your App

You can easily add very cool IBM Watson APIs to your application. You need to sign up for Bluemix, create an application and bind APIs or services to the app. This will create credentials (username and password) for each API or service.

Create a Bluemix acccount

First if you have not signed up for a Bluemix account yet, go to http://ibm.biz/bluemixnyc and Sign Up.

Create an App

Once you have confirmed your account, go to the Catalog and choose any Runtime. We will only use the runtime to bind services to, which will give us API credentials. For example, name the app ‘my-app1’.

Add a Service

Now, to add Services either go to Catalog > Services, and add a service to your app, or go to your app’s admin page. Bluemix is based on Cloud Foundry, so apps in Bluemix are listed under ‘CF Apps.’ Go to Dashboard > CF Apps > ‘my-app1’, and click the ‘Add a Service or API.’ I will add Personality Insights to my app in this example. Edit the required fields and click the Create button. Bluemix might ask you to restage your app. Now we’re ready to call the Personality Insights API.

Call the Watson API

To make a REST call to a Watson API, you must add an Authorization header with a Base64 encoded “Basic :” string. To generate the Basic Authorization header, use the colon as a separator for username and password and Base64 encode the Authorization string.

String username = "username from your service credentials";
String password = "password from your service credentials";
String s = username + ":" + password;
byte[] b = s.getBytes();
String enc = Base64.encodeBase64String(b);
String authorization = "Basic "+enc;

To call the API with the above credentials use the following curl statement from command line.

curl -X POST -H "Authorization: Basic BzAvEATz4UYqKfU5JA66GSIwKNgdATGiADR8YiWaOSH2Purd6tedhKUJaPa3KLshLq==" -H "Content-Type: text/plain" -H "Accept: application/json" -H "Accept-Language: en" -H "headers: true" -d 'put your text here' https://gateway.watsonplatform.net/personality-insights/api/v2/profile

For the API reference details go here
http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/personality-insights/profile

This curl command will return a response object that you can visualize with the Visualize Personality Insights API.
http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/personality-insights/visualize

Print Friendly, PDF & Email