Making a bot that wishes Good Morning in R.

             Well, I had been wondering since many weeks what should I do in my free time. I get so many of ideas always, very few with a clear thought of how it needs to be done. One of those ‘ideas’ was to make a Twitter bot. There are so many things a Twitter bot can do. Just to keep it simple I had decided let’s make a bot first and then decide what will it do.

             Making a Twitter bot with R is not at all difficult. There are so many of online resources already available to register our app on Twitter. One of them can be found here. Once we have registered our app, authorized it we are all set to perform various operations. The twitterR package in R is specially designed to play with the twitter API. We can use the searchTwitter() function with a string input to find tweets with specific string or use updateStatus() function to post a tweet.

       One can find the entire code here on Github. The first 6-7 lines are just authorization steps. We need to provide our consumer_key, consumer_secret, access_token, access_secret from our app and provide all this in the setup_twitter_oauth function in R. Next, the line

searchTwitter('#GoodMorning', n=1, lang = 'en')

searches tweets which contains text #GoodMorning. n are the number of tweets to be searched. I have kept it as only 1 here and the language I selected was English. There are many other parameters in this function which we can tune. You can give a range of dates for the tweets to be searched using the since and until. Also we can extract tweets from specific location using the geocode parameter and so on. More information available at ?searchTwitter.      

 tweetDetailsDF <- twListToDF(searchResult)

               The next function which we use is twListToDF takes an object of twitteR class and converts it into data frame with various information of that tweet like text, favorited, created at etc.

  if(!tweetDetailsDF$isRetweet)

            We then check that if the tweet is not retweeted which means that the user has explicitly used/written #GoodMorning then only tweet him back with good morning. This check (if the tweet is retweet) was added later because it wouldn’t be much appropriate to give a reply on something the user hasn’t tweeted about. Getting a reply on some other tweet wouldn’t be much appreciated. Hence, the check. The inreplyTo parameter gives our (Good Morning) tweet as a reply to the tweet which user had originally posted.

Advertisements

One thought on “Making a bot that wishes Good Morning in R.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s