Studio Freya
  • Blog
  • About
    • Advertise
    • Review your product
    • Contact us
Home > C# .NET > How to make Twitter auto follow/unfollow program in Visual Studio with C# .NET

How to make Twitter auto follow/unfollow program in Visual Studio with C# .NET

By guestpost, last updated October 27, 2019

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

  • 1Create a project
  • 2Make GUI
  • 3Create Twitter APP
  • 4Authorize app
  • 5Connect to Twitter
  • 6Get data
  • 7Limitations
  • 8Follow and unfollow
  • 9Tips to get real followers

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.

Create a project

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.

Make GUI

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:

twitter unfollow gui

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.

Create Twitter APP

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.

Authorize app

First things first – we will need to authorize our app on Twitter. Make four text fields for credentials and a “Save credentials” button.

save credential gui twitter follow app

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:

save credentials start form main file code

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.

Connect to Twitter

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
		}
	};
}

Get data

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;
}

Limitations

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.

Follow and unfollow

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 real followers

Tips to get a lot of real followers:

  • Choose people that have followers you are interested in. If you write kids books choose some mom with many kids that tweets a lot and has many real followers.
  • Choose people with active followers. Check for how many times each tweet of that person gets retweeted. The more active followers a person have the more the chance that they are all real.
  • Do not follow people who never tweeted. Chances are small that they check their account.
  • Tweet often yourself. At least once or twice a day in the morning and in the evening.

We hope this article was helpful. Let us know if there is something missing!

How to make Twitter auto follow/unfollow program in Visual Studio with C# .NET was last modified: October 27th, 2019 by guestpost

Related posts:  

Build folder explorer with c# .Net

How to show folder size in Windows

Open and select file or folder with Explorer in C#

How to setup VPN for mobile devices with PPTP Linux server

How to program backup software

guestpost

shoes for developers
planetpsyd banner

Studiofreya

  • Home
  • About
  • Blog

What We Do

  • WordPress
  • Our games
  • Books
  • Store

Support

  • Contact us
  • Privacy policy
  
Scroll to top
Copyright @ 2012-2019