Contents


Share personality insights with a cognitive Twitter bot

Quickly create an app to analyze a user's tweets by using Bluemix, Watson Personality Insights, and Node-RED

Comments

This article was written using the Bluemix classic interface. Given the rapid evolution of technology, some steps and illustrations may have changed.

Node-RED simplifies complex programming into a browser interface where you can rapidly prototype applications by wiring together devices, APIs, and services. The Watson and Twitter nodes in Node-RED applications that you build with IBM Bluemix take that ease-of-use a step further by removing the need for API keys and complicated calls.

You'll create two related applications that help you explore the Watson Personality Insights service. Both apps answer the question of what someone's recent tweets (at least 50 meaningful tweets) say about that individual's personality.

First, you'll build a web app in which users enter a Twitter handle and the app returns detailed personality insights (from Watson) based on that account's tweets. Then, you'll add global context modules to the Node-RED interface as you build a second app that analyzes a user's tweets and creates a simple interaction with a cognitive Twitter bot. The bot's reply tweet gives the user's top Big 5 trait and includes a link to your web app's deeper personality analysis:

Along the way, you'll also learn how to add NPM packages to a Node-RED instance and reference those external functions within nodes.

Node-RED makes the building process — from idea to app — fast and simple, and it leaves room to add and alter nodes as your ideas expand.

What you'll need to build your application

Run the appGet the code

Step 1. Create the service instance and a Twitter app

Before you start building your Bluemix apps, it'll be useful to define the Personality Insights service that the app will use and to gather your Twitter credentials. In this step, you'll create the service and then register an app with Twitter to acquire credentials that will let your Bluemix apps collect tweets.

  1. Log in to your Bluemix account (or sign up for a free trial).
  2. In the Bluemix Catalog, select the Watson filter, and then select the Watson Personality Insights service. Bluemix catalog icon for Watson Personality Insights
  3. In the dialog box on the right, change the service name to tweetmewatson-PersonalityInsights, leave the service unbound, and click CREATE.
  4. In a new browser tab or window, open the Twitter Application Management page, and click Create New App.
  5. In the Application Details window, complete the Name, Description, and Website fields for your application. The Website field requires a URL for your app. Use http://yourappname.mybluemix.net, where yourappname is a unique name for your application. (Test the URL in a browser to ensure that it's unique.) You don't need a callback URL, so leave that field blank.
  6. Accept the Developer Agreement, and click Create your Twitter application.
  7. Select Keys and Access Tokens from the tabbed menu. Scroll down to the Your Access Token section and click the Create my access token button: Screenshot of the Create my access token button in Twitter

Keep the Twitter app information open in your browser or copy it to a text file; you'll need those credentials in the next step and again later in Step 5.

Step 2. Fork and deploy the code

Now that the service and Twitter credentials are primed, you can simply fork the web application code, update the credential information in the code, and then deploy the app — all from Bluemix DevOps Services.

  1. In the "What you'll need to build your application" section of this tutorial, click the Get the code button.
  2. In the overview page for the tweetmewatson project in Bluemix DevOps Services, click FORK PROJECT in the upper-right corner.
  3. In the dialog box, give the app the unique name that you chose in step 2, select the Make this a Bluemix Project check box, and click CREATE.
  4. In the overview page for your newly created Bluemix DevOps Services project, click the EDIT CODE button to open the editor.
  5. Click the manifest.yml file. Change the host and name values from tweethmewatson to the name that you gave your application. (If you used a name for your Watson Personality Insights service instance other than tweetmewatson-PersonalityInsights, replace tweetmewatson-PersonalityInsights with the name you used.)
  6. Open app.js in the editor.
  7. Update the tweet variable with the consumer key, consumer secret, access token, and access token secret with your Twitter credentials (from the open browser tab or text file). Twitter credentials
    Twitter credentials
  8. Click the arrow button next to the app name to deploy the app: Screenshot of the arrow button
    Screenshot of the arrow button

    It can take several minutes to deploy your application.

  9. When deployment is complete, click the Open the application URL symbol Screenshot of the icon for opening the app URL to open Bluemix application.
  10. Test the app by entering a Twitter handle and clicking Analyze to view the insights into the personality of the person: Screenshot of results of analyzing Twitter feed
    Screenshot of results of analyzing Twitter feed

So far, you have created a functional stand-alone web app. In the remainder of this tutorial, you'll use Node-RED to create a Twitter bot that enables Twitter users to get Watson personality insights automatically. The bot output includes a link to a detailed analysis generated by the web app; otherwise, the web app and the bot app function relatively independently of each other.

Step 3. Create the bot

Now you'll use the Node-RED boilerplate to create a basic Node.js application, and then bind your existing Personality Service instance to it.

  1. Log in to your Bluemix account (if you're not still logged in).
  2. In the Bluemix catalog, select the Node-RED starter boilerplate.
  3. Leave the SDK for Node.js tile selected. In the dialog box on the right, give your app a unique name (different from the name of the web application that you already created) and click CREATE.
  4. Go to the Bluemix dashboard and select your app. Wait until the APP HEALTH section in the upper right shows that your app is running. Then, click BIND A SERVICE OR API.
  5. Select the Watson Personality Insights service that you created in Step 1 (tweetmewatson-PersonalityInsights), and click ADD to make it available to this application. Restage the app when prompted.
  6. When your app is running again, return to the Bluemix dashboard and open the app's overview page.
  7. In the newly bound Personality Insights service, click Show Credentials. Make a note of the username and password values, which you'll need in a later step.

Step 4. Get the twit module

Before you start using the Node-RED interface, you need a more complex set of credentials than the basic Twitter node in Node-RED supplies. Follow these steps to get twit, a Twitter API client for Node.js:

  1. In Bluemix, click ADD GIT in the upper right of the app overview page: Screnshot of the ADD GIT link in Bluemix
    Screnshot of the ADD GIT link in Bluemix
  2. Ensure that the check box for Populate the repository with the starter application package and enable Delivery Pipeline (Build & Deploy) is selected, and click CONTINUE: Screenshot of the Create Git Repository dialog box
    Screenshot of the Create Git Repository dialog box

    Bluemix will take a few moments to create your Git repository in Bluemix DevOps Services.

  3. After the success message is displayed, click CLOSE.
  4. Click the GIT URL that's now in the upper right corner of the overview page in Bluemix. (If you don't see the URL, refresh the page and try to add Git again.)
  5. In Bluemix DevOps Services, click EDIT CODE in your newly created project to open the code editor.
  6. Open the package.json file in the editor, and add this line to the dependencies to include the twit modules:

    "twit":"*",

    Screenshot of package.json with twit modules added to dependencies
    Screenshot of package.json with twit modules added to dependencies
  7. Open bluemix-settings.js in the editor. Add the twit module so that the line for the functionGlobalContext setting (at or near line 52) looks like the following line of code:

    functionGlobalContext: {twitModule:require('twit')},

    Screenshot of bluemix-settings.js with the                              functionGlobalContext variable updated
    Screenshot of bluemix-settings.js with the functionGlobalContext variable updated
  8. Push the local updates to the remote branch by clicking the Git repository icon Screenshot of the Git repository icon in the left toolbar.
  9. To apply all of the updates for the app, select the Select All check box and enter a comment about the updates: Screenshot of the Select All checkbox and Git comment text area
    Screenshot of the Select All checkbox and Git comment text area
  10. Click Commit to push the changes into the outgoing directory: Screenshot of the commit button
  11. For the outgoing directory in the left dialog box, click Push to send the changes to the remote branch and start a deployment in Bluemix: Screenshot of the Push button
    Screenshot of the Push button
  12. Click Build & Deploy in the upper right of the page to see the progress of the build and deploy stages. To view more details about the deployment, click View logs and history.

You have now finished the Bluemix DevOps Services updates. Now you're ready to get to work in Node-RED.

Step 5. Import and update the flow in Node-RED

Now that you've configured the environment and deployed a basic app, you need to get a flow into Node-RED. Building a flow from scratch would involve choosing the right nodes to use and writing JavaScript code to get the data out of Twitter. But you can skip that timely process and simply import an existing flow from the tweetmewatson project in Bluemix DevOps Services. This step walks you through importing a flow and configuring authentication with the Twitter nodes. Then, you'll test the app.

  1. In the "What you'll need to build your application" section of this tutorial, click the Get the code button.
  2. In the tweetmewatson source code, select the nodered directory, and open tweetmewatsonflow.txt. Copy the file contents to your clipboard.
  3. In Bluemix, check APP HEALTH in your app's overview page to confirm that your app is running (and restart it if necessary). Then, click the route for the app: Screenshot of application route
    Screenshot of application route
  4. In your running app, click the Go to your Node-RED flow editor button.
  5. In your Node-RED workspace, click the hamburger menu button, and select Import > Clipboard: Screenshot of import in Node-RED
    Screenshot of import in Node-RED
  6. Paste the contents of tweetmewatsonflow.txt into the Import nodes window: Screenshot of the pasted flow code
    Screenshot of the pasted flow code
  7. Click Ok.
  8. Click anywhere in the workspace pane in the middle to drop the flow onto the workspace. You now have the full flow for the app, which is ready for you to customize by adding your Twitter information.
  9. Double-click the Twitter input node (labeled Tweet Mention) on the far left of the flow to open the Edit twitter in node dialog box:

    This node simplifies the process of authenticating with Twitter for basic tweet monitoring. Click the pencil icon to the right of the Twitter field. Node-RED will now walk through authenticating with Twitter.
  10. Click the Click here to authenticate with Twitter button), and then click Authorize app to authorize Node-RED to access your Twitter account. When you get the "Authorised you can close this window and return to Node-RED" message, return to Node-RED, and click Add to add the new user.
  11. In the for field, replace @WhatsMyBig5 with your Twitter handle and replace #tweetmewatson with a unique but fun hashtag. The app will scan all new public tweets that match these two terms. You can also use only the Twitter handle or only the hashtag. Click Ok. Screenshot of updated Log in as field
    Screenshot of updated Log in as field
  12. In your Node-RED flow, open the Get Tweets Using Twit function node. Copy the credentials from the Twitter app that you created in Step 1 into the correct lines in the tweet variable definition. Remember to keep the single quotes around the text. Click Ok to close the edit dialog box. Screenshot of the Get Tweets Using Twit with updated credentials
    Screenshot of the Get Tweets Using Twit with updated credentials
  13. The bot app sends (along with the top Big 5 Trait) a link to the stand-alone web app, appending the user's Twitter handle to the web app's URL. To update the URL with your app's route, open the Tweet Body node and find http://tweetmewatson.mybluemix.net?user={{handle}}. Replace tweetmewatson with the name of the web application that you created in Step 2.
  14. Edit the Tweet Reply node, select your username from the list, and click OK: Updating the Tweet Reply node
    Updating the Tweet Reply node

    Node-RED creates a credential node to store this data so that you have to configure a Twitter account only once.

  15. Open the Watson Personality Insights node. Enter the user name and password for your Personality Insights service that you noted at the end of Step 3.
  16. Click the Deploy button at the top of the page. Then, switch to the debug tab.

Step 6: Test your apps

  1. In the Bluemix dashboard, verify that both apps are running (and restart them if necessary).
  2. Send a tweet that includes the handle or hashtag that you chose in Step 5.
  3. In the debug tab, you see the raw output. In Twitter, you should see a reply from your Twitter bot app.

Conclusion

In this tutorial, you learned how to fork a Bluemix DevOps Services project and two different ways to deploy an app from Bluemix DevOps Services. You also learned how to import a flow into a Node-RED application, add a module to a Node-RED instance, reference the module within the global context, access the Watson Personality Insights service without ever entering your API credentials, and use the Twitter nodes to listen and to create replies.

Node-RED makes the building process — from idea to app — fast and simple, and it leaves room to add and alter nodes as your ideas expand. I showed only one use of the Watson services here; you can easily plug a dozen other Watson nodes into your Node-RED flow to create a more complex interaction.


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Cognitive computing, Internet of Things, Cloud computing
ArticleID=1018190
ArticleTitle=Share personality insights with a cognitive Twitter bot
publish-date=10272015