Getting started with automation on Twitter

Tweet

Using Twitter applications that create automated replies is an engaging way to encourage your audience to interact with your own API. Getting started with these types of apps is fairly easy and quite fun.

Creating a Twitter application that auto-replies to user Tweets can be broken down into three steps:

  1. Attache to the Twitter streaming API to receive input.
  2. Process input to formulate a reply.
  3. Output a reply using the Twitter REST API.

Applications can get more complex as you introduce requirements such as rate-limiting, so to keep it simple and easy to follow, we will only concentrate on these three steps.

Step 1: Attaching to the Twitter streaming API

Our source of Tweets to which the application will reply is the Twitter user stream API. By attaching to the user stream, the application will receive all activities you would normally view on your main timeline on Twitter.com. Attaching to the user stream requires authentication, so you will need to create a Twitter App to generate the API keys and tokens. In this example, Python and a library called Tweepy is used to make the connection to the user stream.

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitterApi = API(auth)

class ReplyToTweet(StreamListener):

    def on_data(self, data):
        # process stream data here

    def on_error(self, status):
        print status

if __name__ == '__main__':
    streamListener = ReplyToTweet()
    twitterStream = Stream(auth, r)
    twitterStream.userstream(_with='user')

Connecting to the stream using _with='user' will filter all timeline activities to those that directly pertain to the user. Once connected to the stream, activities are passed to the on_data function in the ReplyToTweet class in JSON format. We can now process these activities individually and decide if – and how – the application should respond.

Step 2: Processing input to formulate a reply

Activities on the streaming API come in as messages. It is important to know there are different message types and the messages streamed will not be limited to just Tweets.

For example, the first message received will be a preamble that verifies that you are connected to the API. This preamble contains a list of friend IDs for the account represented by this particular user stream. Some other message types include Tweet deletions and Tweet favorites. This application should only respond to Tweets with @mentions, so we need to check for this before we process the message.

def on_data(self, data):

    jsonData = json.loads(data.strip())
    
    retweeted = tweet.get('retweeted', False)
    from_self = tweet.get('user',{}).get('id_str','') == account_user_id

    if retweeted is not None and not retweeted and not from_self:

        tweetId = jsonData.get('id_str')
        screenName = jsonData.get('user').get('screen_name')
        tweetText = jsonData.get('text')

        chatResponse = chatbot.respond(tweetText)

In the on_data function, we are now checking two requirements: that the Tweet is not a Retweet and that the Tweet was not sent from the user that owns the application. If the retweeted property is missing from the message, then we can assume the activity is not a Tweet and ignore it.

If the message meets our requirements, we will grab the tweetId, screenName and tweetText. Now we have everything we need to generate a reply. This is where you can get creative by processing the Tweet text with your own logic based on your unique situation.

You can use your own APIs or mash up multiple data sources to generate interesting interactions with users. For this example we use the Eliza chat package in the NLTK python library. Eliza is a primitive chat bot developed in the 1960’s using natural language processing. We will pass the tweetText to Eliza, which will return a string that can be Tweeted in reply.

Step 3: Outputting a reply using the Twitter REST API

Before we send out the Tweet, we prepend the user’s screenName to the reply string to create our replyText . We also need to ensure that the string is no more than 140 characters. If you are including a URL in your reply, factor in the shortened length of the t.co URL.

replyText = '@' + screenName + ' ' + chatResponse

if len(replyText) > 140:
        replyText = replyText[0:137] + '...'

twitterApi.update_status(replyText, tweetId)

Once we have the Tweet text ready to go, we use Tweepy to post the replyText to the status/update endpoint of the REST API. Notice how we send along the tweetId of the Tweet we reply to as well. This posts the status as a reply and creates a conversation on the Twitter platform. That closes the loop and the process repeats for the next message. You now have the basics of creating an auto-reply application.

Additional considerations

This was a very barebones code example to get you started. The basic concept is fairly simple, but the execution can get more complex when you factor in API Rate Limiting and limiting input to prevent user abuse. Beyond rate limiting, there are additional considerations you should account for in your application. Please familiarize yourself with the following to help create the best experience for both you and your users:

There are many creative ways to use automation on Twitter such as movie recommendations, flight status, weather forecasts and even customer service automation. Check out the source code on GitHub and start building your own auto-reply app today. We can’t wait to see what you build!

Tags