Saturday, October 17, 2009

Making Your Computer Tweet You When Bad Things Happen

Twitter has a decent, if not minimalist API with which you can tap into with pretty much anything that can execute a process.  Scripting is a natural “fit” for this, but so is mainstream/classic-style/meat-and-potatos “programming”.  Pick your language, it doesn’t really matter.  After all, anyone that says you *have* to use a particular language for general tasks is a complete idiot.  (pssst: it’s the same as saying all you need is a hammer, it works on everything, including hose clamps).

I fell off the trail a bit, I’ll get back on.  Where was I?  Oh yeah…

So, let’s JUST SAY you have a Twitter account and a Windows computer (XP or later).  And maybe you’d like to find a way to leverage that (sorry, I know “leverage” was voted one of the top 100 most overused and annoying words for 2009, at least it didn’t make the top 10), but the path from A to Q (forget Z) is a bit fuzzy?  Hmmm?  Fear not.


  1. A Twitter account
  2. A Script
  3. An Internet connection
  4. A Scheduled Task
  5. Something to trigger this contraption to work

A Twitter Account

You probably don’t want your computer tweeting you, but instead should have it DM (that’s Direct Message) you.  That way your computer’s messages aren’t laid bare to the public, but are instead restricted to just YOU.  You might think you can just use your own Twitter account to DM yourself, but you can’t.  Twitter doesn’t allow that.  So you need to create another Twitter account just for your computer to use. Or do you?  Actually, from the web or most Twitter clients (TweetDeck, UberTwitter, etc.) that is true.  However, with just the API and a script, you can DM yourself.  Weird, but true (for how long is anyone’s guess).

A Script

The pieces for this part are posted over on my other blog here and another example of reading DM folders here.  But for the sake of sparing your poor overworked fingers from more torturous mouse clicks, I’ll drop it below.  For this example, I’m using a VBScript file.  You can use PowerShell, KiXtart, BAT or whatever the **** you prefer, I really don’t give a ****.

Const username  = "Computer_TwitterName"
Const password = "Computer_TwitterPassword"
Const recipient = "My_Twitter_Name"

Function Twitter_Send_Direct(strMsg, recipient)
Dim oXml, strTwitterURL
strTwitterURL = ""
Set oXml = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXml.Open "POST", strTwitterURL, False, username, password
oXml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXml.Send "text=" & strMsg & "&user=" & recipient
Twitter_Send_Direct = oXml.responseText
Set oXml = Nothing
End Function

msg = "Warning Event 129 NTP Synch Discovery Failure (computername)"
result = Twitter_Send_Direct(msg, recipient)
wscript.echo result

Save the script as “twitter_event_129.vbs” in a folder called C:\scripts.

An Internet Connection

Not to be a smartass or anything, but if you’re reading this, there’s a pretty good chance you already have this.  If not, well then…

A Scheduled Task

I’m going to use Windows Vista and Windows 7 for this exercise.  Why? Because I absolutely hate XP.  I hate it more than OSX or Linux.  But, I absolutely LOVE my Windows 7 and Windows Server 2008.  And one reason why is that this makes it MUCH easier to combine steps 4 and 5, and overall it just looks better on the eyes.  I’ll get back to this in a minute.

Something to Trigger this Contraption to Work

For this exercise, I’ll make this engine fire up whenever the Windows Event system logs an “Warning” Event of type 129 (that’s a NTP time synch discovery problem).  If you can’t find any such events, pick whatever suits you.  Just replace the ID number with the appropriate number below.

Since I’m using Windows 7, I’m going to combine steps 4 and 5 as follows:

  1. Open the Event Viewer console

  2. Navigate to Windows Logs / System

  3. In the Actions panel, click “Filter Current Log…”

  4. Check “Warnings”

  5. In the box with “<All Event IDs>” type in “129” and click OK

  6. You should see just those events now.

  7. Right-click on one of the events, and click “Attach Task to this Event…”

  8. The default title will be highlighted.  You can change it if you want or leave it as-is, but I would suggest you enter something in the Description box to explain what this “task” is for and what it does.  For my example, I’m going to replace the default title “System_Microsoft-Windows-Time-Service_129” with “Send Twitter DM for Event 129 NTP Warnings” and in the Description box enter “Call a script to send a Twitter DM when event ID 129 (warning) for NTP synch discovery is logged” and click Next >

  9. Click Next > on the “When an Event is Logged” panel.

  10. Select “Start a Program” and click Next >

  11. Click the Browse… button to locate my .VBS script

  12. Click Next >

  13. Click Finish

You will see a confirmation message appear…

image (yes, you can just click OK)

But, we’re not done yet…

  1. Open the Windows Task Manager

  2. Under Task Scheduler Library, click Event Viewer Tasks

  3. Right-click on the task you just created and select “Properties”

  4. Click the “General tab, click the “Change User or Group…” button

  5. Type in “SYSTEM” and click “Check Names” and the name should become underlined (validated).  Click OK.  The user account box should now display “NT AUTHORITY\SYSTEM”.

  6. Just below that, change the option to “Run whether a user is logged on or not”

  7. Click the “Actions” tab.

  8. Click the “Edit” button

  9. Change the Program/Script box to “cscript

  10. In the “Add arguments” box, enter /nologo C:\scripts\twitter_event_129.vbs and click OK

  11. Click the “Settings” tab.

  12. Change the “Stop the task if it runs longer than:” value to 1 hour.

  13. Click OK to save the changes

Right-click on the Task and click “Run” to force it to run.  Go to your Twitter client to verify you received a DM.  If not, go back and verify your settings and the account and password settings within your script.


No comments:

Post a Comment