Sunday, August 9, 2009

Sending a Twitter Direct Message using VBScript and KiXtart

There are quite a few examples on the web for sending Tweets to your own timeline using scripting and Curl, etc. However, I didn't find a lot of helpful examples on sending direct messages via Twitter using VBscript or KiXtart, so here's what I figured out and it seems to work. For more information on using the Direct_Messages.New API methods, refer to the Twitter API.  Enjoy!

VBScript Version

Const username = "MyTwitterUserName"
Const password = "MyTwitterPassword"
Const recipient = "skatterbrainz"

Function Twitter_Send_Direct(strMsg, recipient)
Dim oXml, strTwitterURL
strTwitterURL = "http://twitter.com/direct_messages/new.xml"
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 = "Test Message (sent from vbscript)"

result = Twitter_Send_Direct(msg, recipient)
wscript.echo result

KiXtart Version

break ON
$username = "MyTwitterUserName"
$password = "MyTwitterPassword"
$recipient = "skatterbrainz"

Function Twitter_Send_Direct($strMsg, $recipient)
Dim $oXml, $strTwitterURL
$strTwitterURL = "http://twitter.com/direct_messages/new.xml"
$oXml = CreateObject("MSXML2.ServerXMLHTTP.3.0")
$oXml.Open("POST", $strTwitterURL, 0, $username, $password)
$oXml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
$oXml.Send("text=$strMsg&user=$recipient")
$Twitter_Send_Direct = $oXml.responseText
$oXml = 0
EndFunction

$msg = "Test Update (sent from kixtart script)"

$result = Twitter_Send_Direct($msg, $recipient)
? $result


Note the wrapping of arguments with matching parenthesis (…) when using KiXtart.  This is not required for VBScript.  A small but important thing to keep in mind if you use both languages (as I do quite often).  Another “feature” which I have mentioned in previous posts, is that KiXtart supports inline string/variable expansion, so you can bury variables within strings and they work fine, which is nice.  PowerShell and some other languages can do that, but not VBScript.

Post a Comment