Wednesday, 15 July 2020

Build Your Own Twitter Bot - Retweeting

<< Project Setup                                                Table of Contents                                                        Sending Tweets >>

In this post, we will be creating an AWS Lambda function for retweeting a number of tweets based on specific hashtags. This function will be scheuled to execute once an hour.

Before we get started, you need to setup a Twitter Developer account.

You will need to:
1. Create a Twitter Account
2. Apply for a Developer account: https://developer.twitter.com/en/apply-for-access
3.  Generate an API Key & Secret and an Access Token & Secret for your account.
4. App permissions are set to read and write.


To be able to retweet, our TwitterBot library will have to be able to:

  • Authenticate
  • Search for tweets based on hashtags
  • Retweet those tweets


Interface

In our TwitterBotService solution and project, create an Interface called ITwitterBot:




Authenticate

The Authenticate method will accept the API Key & Secret and Access Token & Secret, which you setup for your Twitter Developer account, as parameters and will return the authenticated user instance.

Create a TwitterBot concrete class that implements ITwitterBot



This method uses Tweetinvi (Twitter API wrapper) to authenticate and to return the our Twitter accounts user.


SearchTweets

Add the implementation for TwitterBot.SearchTweets for searching tweets with a certain hashtag:



The classes we use for searching for tweets belong to Tweetinvi:

  • SearchTweetsParameters
  • Search
  • ExceptionHandler


We use SearchTweetParameters to specify that we want the 10 most recent english tweets based on the hashtags set in the searchQuery parameter string.

Search.SearchTweets will search for the tweets based on these parameters and will return the tweets as IEnumerable<ITweet>

If we receive a null, then there will be an exception and that can be retrieved by calling ExceptionHandler.GetLastException()

Finally, we only want to return tweets that do not belong my twitter account, that aren't retweets, and
were tweeted in the last hour.


RetweetFavouriteAndFollow

Add the implementation for TwitterBot.RetweetFavouriteAndFollow for retweeting, favouriting and following the users of tweets.



In this method we simply loop through IEnumerable<ITweet> tweets retweeting and favouriting the tweets. We then select the distinct users of the tweets, so we only follow tweeters once.



RetweetFunction

Now that we have our TwitterBot API ready for retweeting, let's set up our AWSLambda Retweet function.

1.Open OccultTwitterBot solution and project and add a new class called RetweetFunction (or just rename the default Function class).

This class and FunctionHandler is what will be invoked when we trigger our Retweet AWSLambda event.

2. Add the following code:


We create an instance of our TwitterBot class and authenticate passing in our twitter account credentials.

We call twitterBot.SearchTweets and pass in the hashtags that we want to retweet.

If we have tweets to tweet, we then retweet the, and log useful information to AWS CloudWatch log using LambdaLogger.

In a later post, I will show you how to deploy our function using AWS CloudFormation and setting up a CloudWatch event.

In the next post, we will set up an AWS Lambda function for sending tweets.

<< Project Setup                                                Table of Contents                                                        Sending Tweets >>

No comments:

Post a Comment