using System; using System.IO; using TwitterVB2; // from http://twittervb.codeplex.com/ Note: To build, I had go to Project->microSpeak140 properties->Application and select the target framework of .NET 3.0 (i.e., not the default 4.0), and then it built (then put it back to 4.0). class Speak140 { // To make this program work, I went to https://dev.twitter.com/apps/, and got my own Consumer Key and Secret: string ConsumerKey = replace this with what you got off the Twitter site string ConsumerSecret = replace this with what you got off the Twitter site string fName = string.Empty; string userToken = string.Empty; string userSecret = string.Empty; TwitterAPI twitter140 = null; bool LoggedOn = false; /// /// A file can be specified which will store the credentials /// public Speak140(bool autoLogon = true, string userFile = "microSpeak140.txt") { LoggedOn = false; twitter140 = new TwitterAPI(); fName = userFile; if (autoLogon) Logon(); } /// /// Open fName and read the user's credentials (if the file exists), else send user to a twitter page /// to get a 7-Digit PIN which we then use to get and save their credentials (we write to fName) /// public bool Logon() { bool success = false; bool promptUser = false; if (File.Exists(fName)) { try { userToken = string.Empty; userSecret = string.Empty; StreamReader mFile = new StreamReader(fName); userToken = mFile.ReadLine(); userSecret = mFile.ReadLine(); if (userToken != null && userSecret != null && userToken != string.Empty && userSecret != string.Empty) twitter140.AuthenticateWith(ConsumerKey, ConsumerSecret, userToken, userSecret); else promptUser = true; mFile.Close(); success = true; } catch { promptUser = true; } } else promptUser = true; if (promptUser) { string PIN = PromptUserForPIN(); if (PIN != String.Empty) { if (twitter140.ValidatePIN(PIN)) { userToken = twitter140.OAuth_Token; userSecret = twitter140.OAuth_TokenSecret; twitter140.AuthenticateWith(ConsumerKey, ConsumerSecret, userToken, userSecret); success = true; if (File.Exists(fName)) File.Delete(fName); StreamWriter mFile = new StreamWriter(fName); mFile.WriteLine(userToken); mFile.WriteLine(userSecret); mFile.Close(); } else Console.WriteLine("Could not authenticate PIN {0}.", PIN); } else Console.WriteLine("You did not enter a valid PIN"); } LoggedOn = success; return success; } /// /// If the user has not registered with us before, we'll ask them to go to the twitter web site and get a 7-digit key /// string PromptUserForPIN() { string PIN = string.Empty; Console.WriteLine("Step 1: Go here, " + twitter140.GetAuthenticationLink(ConsumerKey, ConsumerSecret)); Console.Write("Step 2: Enter the 7-Digit PIN> "); PIN = Console.ReadLine(); return PIN; } /// /// Say what's on your mind. We'll publish it to twitter if it is less than 140 characters. /// public bool Say(string message, bool confirmBeforeSend=true) { Console.WriteLine("Sending: " + message); if (message.Length > 140) { Console.WriteLine(" Contains {0} too many characters.", message.Length - 140); return false; } if (message.Length < 140) Console.WriteLine(" {0} unused characters; you can write more.", 140 - message.Length); else Console.WriteLine(" 140 character limit reached."); if (LoggedOn) { bool send = true; if (confirmBeforeSend == true) { Console.WriteLine(); Console.Write("Press the Y-key to post this, any other key to abort>"); ConsoleKeyInfo ci = Console.ReadKey(); send = (ci.KeyChar == 'y' || ci.KeyChar == 'Y'); Console.WriteLine(); } if (send) { TwitterStatus s = twitter140.Update(message); Console.WriteLine("Posted: {0}, length {1}", s.Text, s.Text.Length); } else Console.WriteLine("Aborted. Nothing posted."); } else Console.WriteLine("You are not authenticated. Consider deleting your credentials file: {0}", fName); return true; } public bool myWall(int count=0) { bool ret = false; if (LoggedOn) { int ct = 1; foreach (TwitterStatus tweet in twitter140.HomeTimeline()) { Console.WriteLine(tweet.User.ScreenName + " : " + tweet.CreatedAt + " : " + tweet.Text); if (ct == count && count > 0) break; ct++; } } return ret; } /// /// Show the status of the given person /// /// name of person /// number of posts to show /// public string theirWall(string name, int count=0) { string ret = null; if (LoggedOn) { int ct = 1; foreach (TwitterStatus tweet in twitter140.UserTimeline(name)) { Console.WriteLine(tweet.User.ScreenName + " : " + tweet.CreatedAt + " : " + tweet.Text); ret = tweet.Text; if (ct == count && count > 0) break; ct++; } } return ret; } public bool iFollow() { bool ret = false; if (LoggedOn) { foreach (TwitterUser user in twitter140.Friends()) Console.WriteLine(user.ScreenName); } return ret; } public bool followMe() { bool ret = false; if (LoggedOn) { foreach (TwitterUser user in twitter140.Followers()) Console.WriteLine(user.ScreenName); } return ret; } }