Twitter App Sample for IBM Watson Workspace
App built with node.js to listen to messages posted in a space in IBM Watson Workspace and returns back recent tweets about a given topic.
This app will listen for
@twitter <keyword/hashtag/@user/phrase> and give you a sense of what people are saying about that in Twitter.
The Watson Work platform provides spaces for people to exchange messages in conversations. This app shows how to listen to a conversation and receive messages on a Webhook endpoint, then send response messages back to the conversation. It also demonstrates how to authenticate an application and obtain the OAuth token needed to make Watson Work API calls.
Deploy the app
Assuming you just want to take this code and get it running before hacking it, the first step is to get it deployed to a live on a server so that IBM Watson Workspace validates it's up and working before pushing messages to the app.
To facilitate things, you can click the button below and it'll get it going to Bluemix very easily, however this is NOT required. Feel free to deploy on any server you want.
Note: you can run this code locally, but then you would need to change the webhook code to get the URL of the callback. See Appendix for instructions.
Once the code is live, make note of the URL (e.g. http://mysuperawesomeurl.mybluemix.net) as you'll need this later.
How to register the Twitter App with IBM Watson Workspace
Now let's register the app to get some API keys and get things going. First, we need several API keys from Twitter. You can get them by going go to: https://apps.twitter.com . In a couple of steps you can super easily register a "bot". That will then give you a few keys that you will also need to put in your environment variables as such:
In Terminal (or any Terminal emulator such as Git Bash or Cygwin):
export TWITTER_CONSUMER_KEY=... export TWITTER_CONSUMER_SECRET=... export TWITTER_ACCESS_TOKEN_KEY=.... export TWITTER_ACCESS_TOKEN_SECRET=...
Second, let's register the app with IBM Watson Workspace and get some keys!
- Go to the developer apps page
- On the left, enter the
App Nameand the
Description of App
- Click on
Add an outbound webhook
- Give the webhook a name (e.g. "listen for messages") and check the
message-createdwebhook. This is how we'll listen to messages in a space
- In the callback URL, specify the URL for your app. This code assumes that the webhook listener is at
https://yoururl/webhookso don't forget to add
/webhookto the end of the URL (if you don't know where the app will be deployed, use a sample URL for now, like
https://twitter.acme.com/webhookand you can modify that later)
- Click on
- This will give you the App ID, App secret and webhook secret. You need to save these to environment variables called
At this point the webhook is not enabled since the system has not been able to verify it's up and running. While the webhook is probably running on Bluemix, it doesn't have the variables set
Configure your app
Now that we have the API keys, set them as environment variables. If you are using Bluemix, you can set them up following these steps:
- Go to the Bluemix console and click on your app
- Go to Runtime -> Environment Variables
- Define the 7 variables from above:
TWITTER_ACCESS_TOKEN_SECRET, provide the right values and save them.
- Stop and start the app so the values take effect.
You are almost there!!!
Enable the webhook
- Assuming all environment variables are set (see above), go back to the IBM Watson Work Services apps page and click on the pencil icon to edit your app
- Check the box to enable your webhook
- Save your changes by clicking on "Edit app"
This now does an HTTP POST verification call to ensure your webhook is setup properly. This will also ensure that all your variables and code are good to go. If so, the webhook is now enabled and it's ready to be used! How easy was that!?!
Add the Twitter app to a space to test
We are almost there! Now we need to add the app to spaces that we want to listen for messages, and where the app will post messages to.
- Head out to IBM Watson Workspace and go to your favorite space
- Go into the space settings
- Click on
Appsto go the apps menu
- Click on your app to add it to the space
- Type in
@twitter #watsonworkspaceand the app should respond with the 3 latest tweets that mention that hashtag
Appendix: How to Start Twitter app locally
- Install Node.js 6+.
npm installto install dependencies
npm run buildto compile the code
- Set the environment variables as per above.
- The server will now run on localhost:3000.
You can use curl to submit a query and test it (or just deploy to Bluemix and test it there!) to simulate a call from Workspace to your webhook. Or check out the Greeter sample for further instructions on how to run locally.
What API does the app use?
The app uses the [Watson Work OAuth API] (https://workspace.ibm.com/developer/docs) to authenticate and get an OAuth token. It implements a Webhook endpoint according to the Watson Work Webhook API to listen to conversations and receive messages. Finally, it uses the [Watson Work Spaces API] (https://workspace.ibm.com/developer/docs) to send back greeting messages.
How can I contribute?
Pull requests welcome!
License and Dependencies
Licensed under Apache 2.0 (see LICENSE)