Saturday, October 17, 2009

Using Twitter to Control Your Computer

I’ve blogged about this before, but not with a lot of detail.  So here goes.

In my previous post, I explained how to setup a “system” that has your computer sending a DM (Direct Message) Tweet to your Twitter account.  But in this scenario I’m turning it around, so that you will Tweet your computer and have it perform actions based on what you Tweet.

What you do with this is up to you.  Anything you can do with a script is fair game.  Anything.  The script will reside on your computer.  You will send a DM to a Twitter account, which your computer will monitor on a scheduled recurrence.  Your script will read and parse the DM container and look for specific phrases that it is set to recognize and will execute a task accordingly.  Simple enough?  Good.  I thought so.

Ingredients:

  1. A Twitter Account
  2. Another Twitter Account
  3. A List of Things You Intend to Ask Your Computer To Do
  4. A Script
  5. A Scheduled Task

A Twitter Account

This is YOUR Twitter account actually.  You will need this in order to send a DM (Direct Message) Tweet to your computer, which requires…

Another Twitter Account

This is the account your computer will monitor.  Remember to lock it down so only your “other” Twitter account can access it.

A List of Things for Your Computer To Do

You need to sit down and make a list of what specific tasks you want to be able to request of your computer.  Yes, you COULD make it so you Tweet any shell operation and have it blindly execute that, but trust me: THAT IS DANGEROUSLY STUPID.  In fact, so stupid, you should have your balls stomped with ice climbing boots in a rowdy Irish pub just for thinking it.  Need some pointers? Here’s a simple list to get you started:

  • Reboot or Shutdown a computer
  • Disable a User Account
  • Add/Remove A User to/from a Group
  • Start/Stop a Service
  • Start a Batch or Backup Job

For the first example, I’m going to simply have the computer tweet me back.  That way I know the plumbing is working and the toilets flush properly.  To do this, I’m going to make a script that reads my DM list and parses it for a specific phrase.  When that phrase is found, it will simply tweet me back (by DM) to let me know it got it.

A Script

The script I’m using for this example is written in VBscript, but any language/platform will work fine as long as it can invoke the Twitter API.  This script will access my Computer Twitter account, fetch the DM queue, iterate through it for messages containing a specific phrase of “TWEET_ME_BACK @userid” and then simply turn around and send a DM to the “@userid” that sent it (which, in this situation, is me).

'----------------------------------------------
' filename: twitter_monitor.vbs
' author: skatterbrainz
' http://scriptzilla.blogspot.com
' give props, don't rip me off please?
'----------------------------------------------
Const username = "Computer_TwitterName"
Const password = "Computer_TwitterPassword"
Const replyTo = "My_Twitter_Account"

Function Twitter_Get_Direct(strUser,strPass)
wscript.echo "querying for direct messages..."
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)

'----------------------------------------------
' if direct messages were found, iterate them
'----------------------------------------------

If Trim(result) <> "" Then
wscript.echo "direct messages were found!"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = "false"
xmlDoc.loadXML(result)
For each x in xmlDoc.documentElement.childNodes
msg = x.Text
If InStr(1, Ucase(msg), "TWEET ME BACK") <> 0 Then
id = Left(msg, 9)
wscript.echo id, ": ", Mid(Msg, 20)
wscript.echo "sending tweet-dm..."
TweetBack()
DeleteDM id
End If
Next
End If

'----------------------------------------------
' send tweet via direct-message
'----------------------------------------------

Sub TweetBack()
Dim retval, strMsg : strMsg = "I got your message!"
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=" & replyTo
retval = oXml.responseText
Set oXml = Nothing
End Sub

'----------------------------------------------
' delete a direct-message using the id number
'----------------------------------------------

Sub DeleteDM(id)
Dim retval
Dim oXml, strTwitterURL : strTwitterURL = "http://twitter.com/direct_messages/destroy/" & id & ".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()
retval = oXml.responseText
Set oXml = Nothing
End Sub


Note that in the iteration section of code above I invoke the XMLDOM object to parse the direct_message result.  This is because it returns a single XML stream that contains all of the direct messages.



A Scheduled Task



Because your computer needs to read the Twitter account DM queue, it needs to “poll” for it using a recurrent task.  The easiest way to do this is by creating a scheduled task.  Each time the task is initiated, it will execute the script and go back to sleep until it’s scheduled to run again.  I’m using Windows 7 for this example, so here goes…




  1. Click on the Windows button (formerly “Start” button)


  2. Type in SCHED and as soon as “Task Scheduler” appears at the top, press Enter.


  3. Click on Task Scheduler Library in the left-hand panel


  4. Right-click on Task Scheduler Library and select “Create Task


  5. In the Name box, enter “Monitor Twitter Account”


  6. In the Description box, enter “Monitor Twitter account ___ for Direct Messages to parse” (fill-in your computer Twitter account name).


  7. Select “Run whether user is logged on or not”


  8. Click “Change User or Group” button, enter “SYSTEM” and click “Check Names”


  9. Once the name is underlined, click OK.  You should see “NT AUTHORITY\SYSTEM” in the user account name box. (see Fig. 1)


  10. Click the Triggers tab.


  11. Change the “Repeat task every:” to “15 Minutes”


  12. Check “Stop task if it runs longer than:” and set it to “14 Minutes” (see Fig. 2)


  13. Click OK, to return to the Create Task form.


  14. Click the Actions tab, click the New button


  15. Enter “cscript” for the program.  Enter “/nologo c:\scripts\monitor_twitter.vbs” in the arguments box (see Fig. 3, and 4)


  16. Click OK to save the new Task



Figure 1 Fig. 1



Figure 2 Fig. 2



Figure 3 Fig. 3



Figure 4 Fig. 4

No comments:

Post a Comment