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.
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.
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 Name
and theDescription of App
- Click on
Add an outbound webhook
- Give the webhook a name (e.g. "listen for messages") and check the
message-created
webhook. 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/webhook
so don't forget to add/webhook
to the end of the URL (if you don't know where the app will be deployed, use a sample URL for now, likehttps://twitter.acme.com/webhook
and you can modify that later) - Click on
Register app
- This will give you the App ID, App secret and webhook secret. You need to save these to environment variables called
TWITTER_CLIENT_ID
,TWITTER_CLIENT_SECRET
, andTWITTER_WEBHOOK_SECRET
respectively
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
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_CLIENT_ID
,TWITTER_CLIENT_SECRET
,TWITTER_WEBHOOK_SECRET
,TWITTER_CONSUMER_KEY
,TWITTER_CONSUMER_SECRET
,TWITTER_ACCESS_TOKEN_KEY
andTWITTER_ACCESS_TOKEN_SECRET
, provide the right values and save them. - Stop and start the app so the values take effect.
You are almost there!!!
- 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!?!
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
Apps
to go the apps menu - Click on your app to add it to the space
- Type in
@twitter #watsonworkspace
and the app should respond with the 3 latest tweets that mention that hashtag
Have fun!!
- Install Node.js 6+.
- Run
npm install
to install dependencies - Run
npm run build
to compile the code - Set the environment variables as per above.
- Run
npm start
- 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.
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.
Pull requests welcome!
Licensed under Apache 2.0 (see LICENSE)
Depends on:
- body-parser
- express
- request
- request