Pages

Wednesday, August 12, 2009

Fetching Twitter Direct Messages Using VBScript or KiXtart

Thanks to the Twitter API documentation (which is lacking examples) and the MSDN reference library on using MSXML/XMLHTTP methods, I’ve put together a simple script to query your Twitter DM inbox and fetch the 20 most recent items (that’s the built-in default of the Twitter API, by the way).  Enjoy!

VBscript Example:
Const username = "MyTwitterUserName"
Const password = "MyTwitterPassword"

Function Twitter_Get_Direct(strUser,strPass)
Dim oXml, strTwitterURL : strTwitterURL = "http://twitter.com/direct_messages.xml"
Set oXml = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXml.Open "GET", strTwitterURL, False, strUser, strPass
oXml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXml.Send()
Twitter_Get_Direct = oXml.responseText
Set oXml = Nothing
End Function

result = Twitter_Get_Direct(username, password)
wscript.echo result


KiXtart Example:



$username = "MyTwitterUserName"
$password = "MyTwitterPassword"

Function Twitter_Get_Direct($strUser,$strPass)
Dim $oXml, $strTwitterURL
$strTwitterURL = "http://twitter.com/direct_messages.xml"
$oXml = CreateObject("MSXML2.ServerXMLHTTP.3.0")
$oXml.Open("GET", $strTwitterURL, 0, $strUser, $strPass)
$oXml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
$oXml.Send
$Twitter_Get_Direct = $oXml.responseText
$oXml = 0
EndFunction

$result = Twitter_Get_Direct($username, $password)
? $result


I could probably post this in PowerShell also, but nobody seems to care, so unless someone asks I won’t bother.  Anyhow: putting this together with the previous examples and blog rants, using a little duct tape and glue, you can assemble a nifty little system for monitoring remote servers using Twitter, as well as sending control commands to the remote server to execute.  That’s right.  You can use the above script on a server to fetch DM messages and process them.  I’ll leave that to your imagination, but it does work and is pretty simple to do.  The only caveate is Twitter availability (not very good) and how robust you want to make the contraption.  Cheers!

No comments:

Post a Comment