You don’t have to do everything on your own when it comes to online business. In fact, there is so much of online business that can be automated that those who realize this will be most successful in the future. As all other conventional businesses – always aim to cut your costs in the long run and automate everything.
Contents
In this Twitter tutorial I will show you how to create your own auto follow and unfollow application that will help run Twitter social accounts automatically and grow them exponentially every day. This can be done in many ways and technologies. I chose to use C# .NET as it’s the easiest way to make both GUI and back end programming at once and run it on your own PC without any server.
Open Visual Studio and create a new C# .net project: File -> New project -> Other languages -> Visual C# -> Windows -> Windows Forms Application.
We will make a GUI in a form of Windows forms because you can simply drag and drop elements into the form without thinking about HTML and CSS programming.
In our new project go to Form.cs [Design] file and drag and drop some text fields and buttons. Here is how our application looks like:
The most important fields here are: twitter account credentials, a field to provide who you are going to follow and a status log. There will be some errors in the beginning, so the log is a very useful thing to have. We have also added fields to limit the number of followers. This is due to Twitter limitations. If you follow more than 1000 people (or something) in one day your account will be locked. This application will make our following base as organic as possible.
In order to make an automated Twitter following app you will need to create an authorized app and get credentials on your Twitter profile. Read Twitter documentation on how to get it.
First things first – we will need to authorize our app on Twitter. Make four text fields for credentials and a “Save credentials” button.
The code for saving our credentials will be placed in the Form.cs file. If you use all the default values than it will look similar to ours:
In order to store credentials we created a Cred class:
class Cred { public string Description { get; set; } public string ConsumerKey { get; set; } public string ConsumerSecret { get; set; } public string TwitterAccessToken { get; set; } public string TwitterAccessTokenSecret { get; set; } public string File { get { return Description + ".xml"; } } }
Within our form we make a list with Credentials. This is for several twitter accounts:
List<Cred> credentialsList = new List<Cred>(); private void saveCredentialsClicked(object sender, EventArgs e) { if (txtConsumerKey.Text != "" && txtConsumerSecret.Text != "" && txtTwitterAccessToken.Text != "" && txtTwitterAccessTokenSecret.Text != "") { saveSettings(); } } void saveSettings() { credentialsList.Add(new Cred { Description = textDecription.Text, ConsumerKey = txtConsumerKey.Text, ConsumerSecret = txtConsumerSecret.Text, TwitterAccessToken = txtTwitterAccessToken.Text, TwitterAccessTokenSecret = txtTwitterAccessTokenSecret.Text }); lstCredentials.FormattingEnabled = false; lstCredentials.DataSource = credentialsList; lstCredentials.SelectedIndex = 0; }
When we click the button “Save credentials” they will be saved in the credentials list.
Now it’s time to connect to Twitter. The best way is to use the Twitter API.
Create a Form.cs class variable that will have a reference to the API:
TwitterAPI m_TwitterAPI = null;
We used one of the libraries (linq2twitter) in order to easily connect to Twitter.
Create a new class TwitterApi that will have TwitterContext and AuthObject. These are objects from the Linq2Twitter library. If you choose another library than you will need to check out how to connect using these libraries.
using LinqToTwitter; using LinqToTwitter.Common; class TwitterAPI { TwitterContext twitterCtx = null; AuthObject auth = null; }
In the constructor of the TwitterApi class check if credentials are ok and then set them:
public TwitterAPI(Cred credendials) { auth = new AuthObject(); auth.auth = new SingleUserAuthorizer { Credentials = new SingleUserInMemoryCredentials { ConsumerKey = credendials.ConsumerKey, ConsumerSecret = credendials.ConsumerSecret, TwitterAccessToken = credendials.TwitterAccessToken, TwitterAccessTokenSecret = credendials.TwitterAccessTokenSecret } }; }
First, we need to get data from the account we would like to follow users from.
Followers can be received with a Friendship class of the Twitter API. Create a method called getFollowersForUser and create a simple SQL-like syntax to get the followers:
public List<User> getFollowersForUser( string userid, ref string cursor ) { //Authorize and check limits List<User> follow_users = new List<User>(); string curs = cursor; Friendship i_follow = ( from friend in twitterCtx.Friendship where friend.Type == FriendshipType.FollowersList && friend.UserID == userid && friend.SkipStatus == true && friend.Cursor == curs && 1 == 1 select friend ).SingleOrDefault(); if ( i_follow != null ) { cursor = i_follow.CursorMovement.Next; follow_users = i_follow.Users.ToList(); } else { cursor = "0"; } return follow_users; }
Twitter is struggling with a lot of spam, so they implemented a set of rules that you must follow or your account will be banned.
In our application, we are mostly interested in the rules regarding the maximum number of following. Those are somewhat complex and change from time to time. Anyway, you need to limit how many people you can autofollow and unfollow with this application.
In order to follow the user call a method CreateFriendship with a callback function to handle exceptions and errors. Here is the code:
Action<User> followUserDelegate = null; public void followUser( string userid, Action<User> delgt ) { followUserDelegate = delgt; var cb = new TwitterAsyncResponse<User>(); var act = new Action<TwitterAsyncResponse<User>>( followUserCallback ); getRateLimits().WaitIfNeeded(); twitterCtx.CreateFriendship( userid, null, true, act ); } void followUserCallback( TwitterAsyncResponse<User> response ) { User user = response.State; Exception except = response.Exception; if (user != null) { followUserDelegate(user); } }
Similar for unfollowing users:
Action<User> unfollowUserDelegate = null; public void unfollowUser( string userid, Action<User> delgt ) { unfollowUserDelegate = delgt; var cb = new TwitterAsyncResponse<User>(); var act = new Action<TwitterAsyncResponse<User>>( unfollowUserCallback ); getRateLimits().WaitIfNeeded(); twitterCtx.DestroyFriendship( userid, null, act ); } void unfollowUserCallback( TwitterAsyncResponse<User> response ) { User user = response.State; Exception except = response.Exception; if ( user != null ) { unfollowUserDelegate( user ); } }
A callback function can also be used to update the status of the application and log all exceptions and errors.
Tips to get a lot of real followers:
We hope this article was helpful. Let us know if there is something missing!