Pages

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.

2 comments:

  1. I have got some similar examples on my site about VbScripts and Twitter:

    - VbScript to post to Twitter
    - List your Followers with VbScript
    - Post to Twitter with HTA for a more user friendly interface
    - And there are also some easy examples how to get the xml response more readable in the FAQ's.


    Regards,
    Erick
    Contact | RSS

    ReplyDelete
  2. I saw those and they are really good. I was looking specifically for how to send and query Direct Messages though. I wasn't able to find any clear examples, so I basically adapted yours and some others along with the Twitter API docs and figured it out by trial and error. Especially the KiXtart examples, due to the differences in how methods are invoked.

    ReplyDelete