Works on my machine weekly project #1
Download the source code mentioned in this blog post.
I'm a hopeless code junkie. I love to write code. Most people do one thing for work and then another for their hobby. My girlfriend for instance works as an IT / Systems Engineer and her other thing is photography.
My other thing is writing more code. I never did this with any of my previous jobs (save dish washer... I did wash dishes when I was at home but I wasn't trying out new, cooler ways to wash them).
... So where was I? Oh, right code junkie. So, I really like to write code and what I've decided to do is start a new small project each week and try to use some new chunk of .Net or a new library and I'll post the end results of my efforts here for you all.
All of these projects will be offered under the CodeImpossible "works on my machine" code quality guarantee. But I'll never post something that flat-out doesn't work.
Sound good? Cool, let's kick it off - as the first entry into this space I'd like to present TweetCommander.
TweetCommander is a small .Net v3.5 console application that lies in wait, watching a twitter account for any new Direct Messages from another user (we'll call this person the "owner").
When a direct message from the owner is found, TweetCommander will check to see if it contains certain text, and depending on that text, will perform a series of actions on the machine it is running on.
TweetCommander will support three commands: "current_screenshot", "exit", and "set_interval".
Working with Twitter
All right so now we need to be able to interact with Twitter. Now, I don't want to write my own API library so I'll go out and get the latest copy of TweetSharp which will give me a nice, readable interface to twitter's API. After getting this built I'll be able to get the most recent direct messages using the following code:
Back
I'm a hopeless code junkie. I love to write code. Most people do one thing for work and then another for their hobby. My girlfriend for instance works as an IT / Systems Engineer and her other thing is photography.
My other thing is writing more code. I never did this with any of my previous jobs (save dish washer... I did wash dishes when I was at home but I wasn't trying out new, cooler ways to wash them).
... So where was I? Oh, right code junkie. So, I really like to write code and what I've decided to do is start a new small project each week and try to use some new chunk of .Net or a new library and I'll post the end results of my efforts here for you all.
All of these projects will be offered under the CodeImpossible "works on my machine" code quality guarantee. But I'll never post something that flat-out doesn't work.
Sound good? Cool, let's kick it off - as the first entry into this space I'd like to present TweetCommander.
TweetCommander is a small .Net v3.5 console application that lies in wait, watching a twitter account for any new Direct Messages from another user (we'll call this person the "owner").
When a direct message from the owner is found, TweetCommander will check to see if it contains certain text, and depending on that text, will perform a series of actions on the machine it is running on.
TweetCommander will support three commands: "current_screenshot", "exit", and "set_interval".
- Sending "current_screenshot" will tell TweetCommander to take a screen capture of the Windows desktop, upload it to TwitPic, and then send the url for that image to the owner user in a Direct Message.
- Sending "exit" will cause TweetCommander to exit
- The "set_interval" is followed by a number that represents the number of seconds TweetCommander should wait between requests for new direct messages from twitter. This is more to avoid the API limit than anything else.
Working with Twitter
All right so now we need to be able to interact with Twitter. Now, I don't want to write my own API library so I'll go out and get the latest copy of TweetSharp which will give me a nice, readable interface to twitter's API. After getting this built I'll be able to get the most recent direct messages using the following code:
var directMessages = FluentTwitter.CreateRequest()
.AuthenticateAs(
TWITTERACCOUNT_USERNAME,
TWITTERACCOUNT_PASSWORD)
.DirectMessages()
.Received()
.Since(Properties
.Settings
.Default
.LastProcessedCommandID)
.AsJson()
.Request()
.AsDirectMessages();
Thats pretty freakin' sweet I must say. Tweet# really takes the brain work out of working with twitter and there is no way to look at that code and not understand what it is doing immediately. Epic win.
To get this running on your machine, just grab the source from my box.net folder, and change these values at the top of the Program.cs file:
// this is our "owner account" we will only act upon direct messages
// send from this user
private static string TWITTEROWNER = "codeimpossible";
// this is our listener accounts username
private static string TWITTERACCOUNT_USERNAME = "someuser";
// this is our listener accounts password
private static string TWITTERACCOUNT_PASSWORD = "somepassword";
Note: The solution file for this contains a reference to a compiled version of the Tweet# library that contains a quick patch I made for an issue that affects uploading an image to Twitpic. However this issue has been fixed officially in the most recent source.
Let's talk about this