Scott Hanselman

Windows Home Server Twitter Notification Plugin

July 14, '09 Comments [7] Posted in Coding4Fun | Home Server | Source Code
Sponsored By

A while back, the very wise Brendan Grant sent me some sample code that would use Twitter to report the health of one's Windows Home Server. I always meant to update the code to use TweetSharp to talk to Twitter, as well as add some robustness for connected/disconnected scenarios, but I'm just never going to get around to it. Instead, here it is as he sent to me.

There's a REALLY vibrant community around Windows Home Server plugins and if you've got a WHS and you want it to do something that it doesn't do, I'd encourage you to jump in.

Even as I'm posting this, I'm sure there are better and more interesting implementations. However, I like what Brendan has done to abstract away the core COM-based API of WHS for use in managed code.

Here's the full program...note again that the PostTweet() method is hacked together and should use a more robust technique:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Serialization;
using Microsoft.HomeServer.SDK.Interop.v1;

namespace Twitter_Test
{
class Program
{
static string username = "";
static string password = "";

static void Main(string[] args)
{
IWHSInfo info = new WHSInfoClass();
//Register application name
info.Init("WHS Twitter Client");

NotificationCallbackClass notificationClass = new NotificationCallbackClass();
//Register notification callback class
info.RegisterForNotifications(notificationClass);

//Check current state
Console.WriteLine("Current State: " + notificationClass.GetHealthState().ToString());

notificationClass.HealthChanged += new EventHandler(notificationClass_HealthChanged);

Console.WriteLine("Monitoring for health changes. Press to exit.");
Console.ReadLine();
}

static void notificationClass_HealthChanged(object sender, HealthChangedEventArgs e)
{
Console.WriteLine("Current State " + e.Health.ToString());
PostTweet(username, password, "Your Windows Home Server's health is now: " + e.Health.ToString());
}

private static Status PostTweet(string username, string password, string message)
{
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
// determine what we want to upload as a status
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + message);
// connect with the update page
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
// set the method to POST
request.Method = "POST";
// set the authorisation levels
request.Headers.Add("Authorization", "Basic " + user);
request.ContentType = "application/x-www-form-urlencoded";
// set the length of the content
request.ContentLength = bytes.Length;

request.ServicePoint.Expect100Continue = false;

// set up the stream
Stream reqStream = request.GetRequestStream();
// write to the stream
reqStream.Write(bytes, 0, bytes.Length);
// close the stream
reqStream.Close();

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

StreamReader sr = new StreamReader(response.GetResponseStream());
string s = sr.ReadToEnd();


XmlSerializer ser = new XmlSerializer(typeof(Status));
object o = ser.Deserialize(new StringReader(s));
Status status = o as Status;

return status;
}
}
}

There interesting part is the Eventing part where he makes changes in your Home Server turn into .NET Events via callbacks. Check the code for details. You can get events when Physical Disks are changed, when Backup States change, or when basically anything happens. There's a number of folks on Twitter already who have their Windows Home Servers tweeting.

If you've got, or you're using a plugin to report your Home Server status on Twitter (or SMS or whatever) leave a comment and I'll update the post! I'm sure there are better solutions than this little sample.

Here's the code if you want it, and remember, it may kill your pets. If so, don't blame me as I'll deny everything. It's a sample you found on the Internet, what did you expect?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Wednesday, July 15, 2009 9:35:35 AM UTC

@WHSTweet
is a spiffy ready-to-install plugin for WHS...

Wednesday, July 15, 2009 2:06:35 PM UTC
Why set the Authentication: Basic headers manually when you could just set the Credentials property to a NetworkCredential?
request.Credentials = new NetworkCredential(username, password);
Wednesday, July 15, 2009 2:48:23 PM UTC
Woot! Dakota State University represents again. Way to go Brendan!! Now I'm jealous. Very jealous
Chris
Wednesday, July 15, 2009 6:13:43 PM UTC
Thanks Scott, this looks really cool. I'm loving my WHS.
Peter
Thursday, July 16, 2009 11:39:38 AM UTC
Using Basic auth over an unencrypted connection?? I don't use Twitter, but I would think they have SSL, right?
Silvio
Friday, July 17, 2009 5:26:12 AM UTC
Shouldn't use ASCII. I've been burnt TOO many times where I've shared source code them someone in Lithuania says all their text turns to question marks.
Kevin
Thursday, July 23, 2009 8:10:46 PM UTC
You guys should check out the Twitterizer project: http://code.google.com/p/twitterizer/ ... it might help simplify the code and it's got a quickly growing community behind it.
Ricky Smith
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.