Santa’s Little Twitter Bot

Simon Burns
Nov 8, 2016 · 3 min read

Santa’s little Twitter bot can tell you if you’ve been naughty or nice. It uses the Watson Assistant service to understand the tweet, and the Watson Tone Analyzer service to determine whether or not you have been naughty.

To try it out just tweet @elfcheck something, for example:

@elfcheck have I been good?

The bot will then analyze your Twitter timeline to determine tone. If it finds too much anger or disgust it will put you on the naughty list.

Examples of some things you can ask:

  • @elfcheck what list am I on?
  • @elfcheck have I been good?
  • @elfcheck what is a reindeer?
  • @elfcheck how many reindeer are there?
  • @elfcheck what is Santa doing?
  • @elfcheck where is Rudolf?
  • @elfcheck how fast are the reindeer?
  • @elfcheck for christmas I would like a Watson Dinosaur

This was really simple to get going, and here’s a quick run-through of what I did.

Setting up Twitter

The first thing you need to do is register a Twitter app. Once you’ve done that, make a note of your ‘consumer key’, ‘consumer secret’, ‘access token’ and ‘access token secret’. Also make sure that your app has ‘write’ permission.

In my code I used the nodejs twitter package as this makes the API really easy to use. You can instantiate it like this (obviously filling in your keys, etc):

var TwitterPackage = require('twitter');
var secret = {
consumer_key: '12345',
consumer_secret: '12345',
access_token_key: '12345',
access_token_secret: '12345'
}
var twitter = new TwitterPackage(secret);

Setting up Watson

Watson lives in the IBM Cloud platform, so you will need an IBM Cloud account if you don’t already have one. Once there, you can add the Assistant and Tone Analyzer services.

In my code I used the nodejs watson-developer-cloud package. You can instantiate Assistant and Tone Analyzer as follows (adding in the credentials from your services in IBM Cloud):

// Tone Analyzer
var watson = require('watson-developer-cloud');
var tone_analyzer = watson.tone_analyzer({
username: '12345',
password: '12345',
version: 'v3',
version_date: '2016-05-19 '
});
// Assistant
var ConversationV1 = require('watson-developer-cloud/conversation/v1');
var conversation = new ConversationV1({
username: '12345',
password: '12345',
version_date: '2016-07-01'
});

@elfcheck requests

Now to watch for references to @elfcheck you can use the Twitter stream API:

twitter.stream('statuses/filter', { track : '@elfcheck' },
function (stream) {
stream.on('data',
function (tweet) {
// do stuff
}
)
}
);

Retrieve the timeline

Now we know @elfcheck was mentioned, we need to get the timeline of the user:

twitter.get('statuses/user_timeline',
{ screen_name : tweet.user.screen_name },
function (error, response) {
// do stuff
}
);

Analyze the tone

Now we have the text of the user’s timeline, we can pass it into the Watson Tone Analyzer API (allText is a concatenation of all the tweets):

tone_analyzer.tone({ text: allText },
function(err, tone) {
// do stuff
}
);

Now this returns a lot of information about tone, so to keep things simple, I just looked for “anger” or “disgust” in the tone of the whole text. If either of these was scored highly then the user was put on the “naughty list”. The tone was then put into the context for the Conversation API.

Converse

I built a quick Watson Assistant workspace that could respond to questions such as “Have I been good?”, and then I could call this via the API (passing in the Twitter user name and tone result):

var message = {
input: { text: tweet.text },
workspace_id: '93430f5f-462f-4041-87e8-ad2ddcbb274c',
context : {
name : '@' + tweet.user.screen_name,
naughty : naughty
}
};
conversation.message(message,
function(err, convoResponse) {
// do stuff
}
);

If you haven’t used Watson Assistant before then check out the Conversational Directory for more articles.

Tweet a response

Now we just need to tweet the response:

var statusResponse = {
status : convoResponse.output.text[0],
in_reply_to_status_id : tweet.id_str
}
twitter.post('statuses/update', statusResponse,
function (error, tweetReply, response) {
// do stuff
}
);

See which list you are on

Hopefully that quick run through is enough to get you going with your own Twitter bots or to try out the Watson Assistant and Tone Analyzer services.

Be sure to try out Santa’s Little Twitter Bot to see which list you are on…

Santa’s Little Twitter Bot has had an upgrade to use Watson Personality Insights to recommend Christmas gifts.

If you want to know more about building bots with Watson, have a look in the Conversational Directory.

Simon Burns

Written by

User Experience Developer | IBM Watson

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade