Share |

Marcus' Macro Blog

Mostly tips, tutorials, articles and news about Macro Scheduler & Windows Automation
April 20th, 2009 by Marcus Tettmar

Way back in the deep and distant past when the Internet was new and Bill Gates thought it was just a passing fad, I remember reading about a Cola vending machine on a University campus that some frivolous young boffins hooked up to the ‘net so that you could check its inventory from anywhere in the world using an old fashioned network command called “finger”. Why? Because they could.

Fast forward to the technologies of the current day and the latest trend of Twitter, and history is repeating itself. In the last week I’ve read about a restaurant that can take orders via Twitter, a bakery tweeting the emergence of fresh loaves from the oven; and, utterly pointless, some guys who created a system which sends a tweet every time their cat enters or exits its cat flap. Why? Well, because they can I guess.

Not wanting to be left out I decided to write some Macro Scheduler code to tweet status updates and monitor replies. Why? Well there might be a good reason for being able to do this – I’m sure someone will have one. Perhaps you have a client who wants you to set up a system to monitor the movement of his cat, process restaurant orders, or your local baker wants an automated fresh-loaf tweeter! But mostly, it’s because we can.

You’ll find the Twitter API documentation here. Here’s the code to Tweet a status update:

Let>username=YOURTWITTERNAME
Let>password=YOURPASSWORD

//Tweet from Macro Scheduler
Let>url=http://%username%:%password%@twitter.com/statuses/update.xml
Let>message=Kitty just left the buildng
HTTPRequest>url,,POST,status=%message%,result

Being serious for a moment I can see how a macro that monitors an application might want to post status updates to Twitter, or a backup script could alert you by Twitter when there’s a problem. It might be a public system, but don’t forget that Twitter profiles can be made private too, and Tweets can be viewed on and sent from your BlackBerry, iPhone, or even by SMS.

The following script sets up a loop which monitors your Twitter stream for “mentions” of your username. This might form the basis of a script which retrieves orders. Perhaps it could listen to Twitter for commands and carry out actions based on what message was sent. Or perhaps you just want a macro which does something when a cat decides to head out for the night. Use your imagination.

Let>username=YOURTWITTERNAME
Let>password=YOURPASSWORD
Let>ini_file=%SCRIPT_DIR%\twit.ini
Let>_delay=30

VBSTART
VBEND

//monitor twitter username "mentions" loop
Label>monitor_loop

Let>url=http://%username%:%password%@twitter.com/statuses/mentions.xml
HTTPRequest>url,,GET,,result

//remove the  portion (I don't need it and it avoids distinguishing the text IDs from the user IDs.
RegEx>[^>](.*?),result,0,user_matches,nf,1,,result

//extract all texts
RegEx>(?<=)[^>]*?(?=),result,0,text_matches,num_texts,0
If>num_texts>0
  //extract all ids
  RegEx>(?<=)[^>]*?(?=),result,0,id_matches,num_ids,0

  //get last known
  Let>last_known_id=0
  IfFileExists>ini_file
    ReadIniFile>ini_file,SETTINGS,LAST_ID,last_known_id
  Else
    WriteLn>ini_file,wlnr,
  Endif

  //iterate through texts
  Let>k=0
  Repeat>k
    Let>k=k+1
    Let>this_id=id_matches_%k%
    If>this_id>last_known_id
      Let>msg_text=text_matches_%k%
      /*
      msg_text contains the message
      Use your imagination here!
      For now we'll show it in a message
      */
      MessageModal>msg_text
    Endif
  Until>k=num_texts

  //store last ID
  EditIniFile>ini_file,SETTINGS,LAST_ID,id_matches_1
Endif

Wait>_delay
Goto>monitor_loop

The script retrieves the 20 most recent “mentions”. It stores the last seen ID in an INI file so that on the next check it ignores those it has seen before, only retrieving messages with a larger ID number.

This is a quick and dirty solution with no error checking, using RegEx to parse the XML that is returned by the call to Twitter. You may prefer to use the MS XML object as shown here.

Whether this proves useful or completely pointless, I hope you have fun. If you’re using Macro Scheduler with Twitter, please add a comment below to let us know how … and why!

Don’t forget you can follow me on Twitter where I may occassionally say something useful.

Related posts:

  1. Macro Scheduler 12.1.6 – Now With Macro Recording in Script Editor
  2. Calling Macro Scheduler Functions from PowerShell
  3. Macro Scheduler 12.1.7 Available
  4. Macro Scheduler 12.1.5 Available
  5. About the MacroScript SDK – How to Run Macro Scheduler Code Within Your Own Applications

6 Responses to “Twittering from Macro Scheduler with the Twitter API”

  1. Hi Marcus,

    While I am not tweeting, this is a great and practical sample script. For those Macro Scheduler users who like me are a bit RegEx-challenged, there is no better (and very affordable) tool (a RegEx IDE, really) than Jan Goyvaerts’ RegExBuddy. Like you Marcus, Jan is an inspired and very capable independent software developer who creates the “glue” tools and utilities that make our lives easier.

    Macro Scheduler developers will find more info and a free trial at http://www.regexbuddy.com/.

    Thank you (and Jan) for doing the great work you do.

    –Jim–

  2. Marcus Tettmar says:

    Indeed. I also use RegExBuddy and in fact I recommended and linked to it in my last blog post: http://www.mjtnet.com/blog/2009/04/30/regular-expressions-for-dummies/

  3. Grimeball says:

    Marcus

    I’m looking to use this Twitter publishing in my business. I am an absentee owner for a car repair shop, and I’m looking to have updates sent to me for the daily sales, number of cars worked on, etc. I currently have a process where I have used Macro Scheduler to automate taking a backup of the database through the sceens in my Point of Sale system (POS). I then transmit the database to my home through FTP. I have a script that I can run to restore the database at home, but I don’t automate it because I may be working on the computer and don’t want it to. It’s a 10-15 minute process to restore the database so I need to make a commitment to loading it when I want to see the shops performance.

    This publishing through Twitter looks to be a godsend. I can extract the information through a script, an update a private twitter profile with the data. I’ve already setup the private profile for the shop, and I’m following it from my personal twitter account.

    I am going to write the script this weekend that extracts the data from my POS and post to the tweet. you were looking for meaningful ways this information may be used…so I thought you may be interested.

    Looking at the code, its very easy. Obviously, your example is using basic http authetication. Looking through the twitter api documentation, at some point (not scheduled) they are going to sunset basic auth and only support the openAuth standard.

    Do you know how if openAtuh can be integrated into MacrScheduler?

    Another example of how I use Macro Scheduler is after my database is ftp’d home and i do a restore, I have a script that can be run, and using a product called qdbc (An ODBC type driver for Quickbooks), I update my accounting software with sales information from the store into my business G/L on QB. I wrote the scipt, but didn’t write error checking into it so its not terribly stable. SOmething I need to work on…but its my errors at this point.

    I’ts wonderful when it works though…If I know I’m not going to be at my PC for the night, I’ll setup a 1 time run at home. Then, My script kicks off at the store, the backup is performed and ftp’d home. My script restores the database (I have a copy of the POS installed on my home PC), reads the data from the last update to the current days information, and updates my Quickbooks data.

    This entering of paperwork would be about 20 minutes per day…6 days a week…52 weeks a year. That’s 4 days and 8 hours a year your products save me. Thank you.

  4. Marcus Tettmar says:

    See: http://apiwiki.twitter.com/FAQ

    Twitter haven’t published a date for oAuth support yet. The beta still requires a browser for authentication which rather puts the scuppers on desktop/mobile integration.

    They have said “HTTP Basic Auth will likely be deprecated after a lengthy grace period.” I’ve seen at least 6 months mentioned. A lot of clients depend on it, so it may be longer. Note it also says “likely”, not “will”. And it can’t happen until oAuth works for desktop and mobile apps without requiring a browser.

    So I think you’ll be OK with basic HTTP authentication for a while. But when/if they announce it will be deprecated we can re-implement with oAuth then.

  5. Branden says:

    I think the possibilities are endless of what you can use automated twitter messages for. However a few ideas I had in mind.

    I personally would like some type of alert system when scripts finish. When the script finished it would then twitter me privately, which then would be routed to me via SMS.

    I think more hardware based things with twitter integration would be great. Laundry done? Tweet. Oven/Microwave done? Tweet. Torrent done? Tweet. This of course is probably out of the scope of Macro Scheduler but the thought of having all the important information I want to notify me would be godsend.

    Of course there are other uses too. A more childish method might be for some online game. Hit a captcha code and it tweets you. All the cheating teens would rejoice!

    Stock hits a certain price? tweet. dvd finishes burning? video done encoding? tv recording done? order tracking number available? tweet, tweet, tweet, tweet.

    I think you get the point. I just wish more software and hardware provided this natively. Keep us posted on any oAuth solutions for sure Marcus.

Leave a Reply