Auto-Twitter

Author: Anas Zahid (born October 20, 1991)
Article Name: Twitter Automation
March 3, 5:30pm

Introduction: 
This article shows how to automate the Twitter Account. The Code is written in C# and the tool used is Visual Studio 2010.The whole application of Auto-Twitter is completely automated. In this application there are multiple tasks which is done in sequenced after starting application automatically. The 1st task of this application is to auto-login to twitter.
The 2nd task of this project is to auto-status update. The 3rd task of this project is auto-follow. The 4th and the last task of this project is auto-comment. When we start our project  firstly the front page of the twitter is came over,then automatically write user name and password, then intend to the main home page of twitter. The status is updated according to the timing, if time is 6:00 Am – 10:00 Am then status is ‘Good Morning’, if time is 1:00 Pm – 4:00Pm then status is ‘Good Afternoon’, same as for Evening. If time is not matched with the given statements then a randomnly status is updated. Then automatically the name who wants you to add in your twitter account is written in search tab and intend to that screen and follow him or her.We are also working on auto-comment on Status (pending)..

Background:

The motivation for developing this app. was to enable some of our Twitter accounts to automatically follow other Twitter users based on the keywords they post. Some people want to update their status after sometime and by using this app. they can update their status automatically. 

Code with Explanation:

1st Task:

   class Twitter
{
public HtmlDocument ObjHTMLDocument = null;
public string homeURL = "https://www.twitter.com";
public string currentURL = "";
public Twitter()
{
}
public bool IsLogin()
{ 
          HtmlElementCollection collection = ObjHTMLDocument.GetElementsByTagName("input");
try
{
foreach (HtmlElement eInput in collection)
{
if (eInput.GetAttribute("class").Equals("js-signout-button"))
{
return true;
}
}
return false;
}
catch (Exception eObj)
{
MessageBox.Show(eObj.Message + "in isLogin");
return false;
}       }
public void login()
{
HtmlElementCollection theElementCollection;
theElementCollection = ObjHTMLDocument.GetElementsByTagName("input");
foreach (HtmlElement curElement in theElementCollection)
{
               if ((curElement.GetAttribute("name").Equals("session[username_or_email]")))
{
curElement.SetAttribute("value", "katrinaa5");
}
else if ((curElement.GetAttribute("name").Equals("session[password]")))
{
curElement.SetAttribute("value", "*****");
}
if (curElement.GetAttribute("value").Equals("Sign in"))
{
curElement.InvokeMember("click");
}
}
} //end method

This is a class of twitter,in which login method is used, which intend us to the twitter url, when this code open the twitter url then check the account that it I sign-in or sign-out. If this is equals to sign-out then it returns true. After that  if the username and password exists then it opens the account if not available then it show an error.

2nd Task:

  public void autofollow()
{
HtmlElementCollection theElementCollection;
theElementCollection = ObjHTMLDocument.GetElementsByTagName("input");
foreach (HtmlElement curElement in theElementCollection)
{
if ((curElement.GetAttribute("id").Equals("search-query")))
{
curElement.InnerText = "Salman khan";
Thread.Sleep(1000);
break;                }
}

This is a function of Auto-Follow on Twitter.This code checked the name or id of the person which you wants to add or follow then he match the id or name of the person and send a request.
3rd Task:

 public void composeTweet()
{
DateTime t1 = Convert.ToDateTime(DateTime.Now);
HtmlElementCollection theElementCollection;
theElementCollection = ObjHTMLDocument.GetElementsByTagName("textarea");
foreach (HtmlElement curElement in theElementCollection)
{
if ((curElement.GetAttribute("ClassName").Equals("twitter-anywhere-tweet-box-editor")))
{
String[] str = {   "Please Some1 Tweet me",
"I m feeling good",
"I am going to University",
"Status update from the visual studio yupeeeeee :P",
"This is a very special day",
"Bored, someone tweet me",
"Randomly status update from the Visual studio"
};
Random r = new Random();
}

This is the code for Compose-Tweet on twitter. Randomly auto status is update. But there is also a code which update a status by checking time. If it is morning then status is ‘Good Morning’  same case for afternoon and evening. This is just a example code of the project.