Schedule Tweets on Windows

Schedule Twitter Posts

Many people (myself included) often post an update to Twitter when publishing a new blog post. WordPress (the blog software I use) and most other blogging platforms include a nice feature that allows you to schedule blog posts to be published at a particular date/time. Wouldn’t it be great if we could also schedule our [...]

Author :  Joe Lennon
Joe is a 25-year-old web applications developer and technical writer from Cork, Ireland. For his day job, Joe builds web applications in PL/SQL and JavaScript for Core International, a leading provider of enterprise HR software in Ireland and the UK. Joe is also a published author, having his debut book, "Beginning CouchDB" published in 2009 by Apress. In addition, Joe is a regular contributing author to IBM's developerWorks technical library website. Joe is also contributing to a forthcoming IBM Press book, "Getting Started with .NET and DB2". Aside from software, Joe enjoys gaming, TV, eating out, motoring and frequent holidays in Spain. He lives in Cork with his girlfriend, Jill and their dog, Toby.

Many people (myself included) often post an update to Twitter when publishing a new blog post. WordPress (the blog software I use) and most other blogging platforms include a nice feature that allows you to schedule blog posts to be published at a particular date/time. Wouldn’t it be great if we could also schedule our tweets so that they are posted when our blog post is published? There are countless other potential uses for scheduled tweets. Maybe you’re launching a new product and want to announce it on Twitter on Monday morning at 9am, but are afraid you’ll get caught up on something else – set an automated Tweet and away you go.

Before I begin, I’ll start off by saying that this is a very simple example of scheduling tweets. It will give you a very simple introduction to the Twitter API, the curl tool and the Windows Task Scheduler. There is no pretty graphical user interface, and configuring your scheduled tweets is fairly manual to say the least. If you would like more features, why not incorporate them into your own little application? I’m certainly thinking of doing so. Also – I believe there is a WordPress plugin that will post a tweet when your blog post is published – if this fits your needs, great! Again – this is merely a simple example of what you can do with the Twitter API, it’s not intended to be the holy grail solution for scheduling tweets!

For the sake of this tutorial I am going to assume you’re using the Windows operating system. If you’re using Mac OS, Linux or another Unix variant, the technique used in this tutorial could be easily amended to work with cron. If enough people demand a Mac/Linux version of this tutorial, I’ll write one.

The first thing you’re going to need to do is download cURL. This command-line utility allows you to transfer files over a host of internet protocols, and is perfect for interacting with the Twitter API. It is free and open source, and is available for a wide range of platforms. Visit the cURL download page and download the No-SSL version provided by Daniel Stenburg. At the time of writing, the latest version available was 7.19.4. Once you have downloaded the ZIP file, extract the contents to c:\curl. If you need an archiving utility, download the excellent 7-Zip. That’s all there is to installing cURL! To test it out, open a command prompt (Start->Programs->Accessories->Command Prompt) and change to the curl directory (cd \curl). Enter the following command:

curl http://www.google.ie

Listing 1 – cURL’ing Google.ie

This should spit back a ream of continuous HTML code. This is the HTML source for the Google.ie home page. What you see should be similar to the screenshot below:

Figure 1 - cURL'ing Google.ie

Figure 1 - cURL'ing Google.ie

Now that we have verified that cURL is working, let’s see how we can use it to interact with the Twitter API. The Twitter API is what is known as a RESTful web service, meaning that it can be queryed using a URI, and will return data in a particular MIME type. Twitter can return data in the XML, JSON, RSS or Atom MIME types. Let’s start working with Twitter by retrieving the Public Timeline in RSS format. In your command prompt, issue the following command:

curl http://twitter.com/statuses/public_timeline.rss

Listing 2 – cURL’ing the Twitter Public Timeline

This should bring back the RSS feed for the Twitter Public Timeline, as seen in the following screenshot:

Figure 2 - cURL'ing the Twitter Public Timeline

Figure 2 - cURL'ing the Twitter Public Timeline

Great – but we’re not very interested in the public timeline are we? Let’s tell Twitter who we are and ask for the timeline of only the people we are following. This time, we will ask for a response in the JSON (JavaScript Object Notation) format. Please be sure to substitute your own Twitter username and password for the values below. And no, my Twitter password is not “password”!

curl -u joelennon:password http://twitter.com/statuses/friend_timeline.json

Listing 3 – cURL’ing our Friends’ Timeline

In this example, we are using HTTP authentication to tell Twitter who we are, and asking it to return a JSON representation of our friends’ timeline. For a sample of the output, see Figure 3 below:

Figure 3 - cURL'ing our Friends Timeline

Figure 3 - cURL'ing our Friends Timeline

At this point, you may be wondering what use all of the mumbo-jumbo we are receiving as a response is to us. In fact, in this tutorial, it’s not much use to us at all as we are only interested in updating Twitter. If we wanted to display our own, our friends or public tweets however, we would be able to parse this information and display it in a readable format. The Twitter API provides an array of methods for pulling back information like this, including:

  • public_timeline
  • friends_timeline
  • user_timeline
  • show
  • replies
  • friends
  • followers
  • many more…

If you would like to delve deeper into these methods, and the Twitter API in general, check out the REST API Documentation on the Twitter API Wiki. It has a host of information and examples on how to use the Twitter API. Now let’s get back to the tutorial!

Up until this point, we have concentrated mainly on retrieving data from Twitter. But we want to send updates to Twitter! Luckily, cURL allows us to neatly send POST data along with our HTTP request. To post an update, issue the following command in your command prompt:

curl -u joelennon:password -d status="Testing out using cURL for Twitter updates" http://twitter.com/statuses/update.xml

Listing 4 – Update Twitter with CURL

As previously, you will receive a response in the format you specified in the request (in this case, XML). This should look similar to the screenshot below:

Figure 4 - Posting a Twitter Update

Figure 4 - Posting a Twitter Update

But, more importantly, if you check out your Twitter page, you will see that your timeline has been updated with a new post – you guessed it, the one we just sent. To change the content of the post, simply change the text between the quote symbols in Listing 4 above. I think  you’ll agree, using the Twitter API with curl is pretty easy! Now let’s take things a step further and create a script that will post the update for us, so that we don’t have to issue the long curl command every time we want to post.

Open a text editor (Notepad will do fine) and add the command from Listing 4 above to it. Feel free to change the status text to something else! Now go to File -> Save As and save it as “twitter.bat” in the C:\curl directory. Be sure to include the quotes when you are saving the file, otherwise Notepad will probably try to save it as .txt file, and you’ll end up with a file named twitter.bat.txt instead!

Now go back to your command prompt and ensure that you have changed in to the C:\curl directory. Enter the following command:

twitter.bat

Listing 5 – Running our Twitter script

Hey presto, your Twitter update has been posted! While this is nice and short, it’s still a bit of a pain as we have to modify the twitter.bat file everytime to change the status text. Let’s fix that. Re-open twitter.bat in Notepad, and change the contents to the following:

@ECHO OFF
SET STATUS=%*
c:\curl\curl.exe -u joelennon:password -d status="%STATUS%" http://twitter.com/statuses/update.xml

Listing 6 – Update twitter.bat file

We have changed the batch file so that it does not display the command each time it runs, and it sets a variable, STATUS to all the arguments entered when the command is executed. We then use this STATUS variable in our curl command instead of static text. This allows us to enter the status text we wish to update Twitter with when we run our script. This time, try running the command in Listing 7 below:

twitter.bat Passing arguments to the batch file

Listing 7 – Running our updated script

Well would you look at that, it’s submitted the Twitter update using the text we specified after the twitter.bat command! Take a step back and look at what you’ve just created – a Twitter updater client! Sure it’s basic, but it works! Now that we have the script to post our Twitter updates created, let’s look at how we can schedule it to automatically post an update at a specified date and time.

A feature of the Windows operating system that is often overlooked is the Task Scheduler. This allows you to create scheduled tasks that will run on a certain date and time. This tool has a GUI interface and a command-line interface for scheduling tasks, but it can be quite complex. Instead, we are going to use the at command, which is included with any NT-based Windows platform (NT/2000/XP/Vista/Server). This is a very basic and simple to use command-line scheduler.

I am writing this particular paragraph at 2:26pm. The line below will automatically send a tweet at 2:27pm. Change the time to the 24-hour value for a time that’s a few minutes into the future for you (otherwise it will send the next time 2:27pm comes around, and you might have to wait a long time to test it worked!)

at 14:27 cmd /c c:\curl\twitter.bat This is a scheduled tweet!

Listing 8 – Scheduling a tweet

The at command is quite simple, but it has some nice features that allow you to schedule tasks to run at set intervals – for example every Monday at 9.00am. For further information, see this Microsoft Knowledgebase article on the at command. Note in the above example that we preceded our twitter script file with the command “cmd /c”. This might not mean anything to you, but it is required in order for the script to work, as we are running a batch file and not a regular executable. This basically tells the scheduler to start a command window, run the specified command and close the command window when done.

Congratulations, you now have a way of automating your tweets so that they are posted at a set date/time or even at a regular interval. It’s not the prettiest solution in the world, but it’s simple and you have probably learned a bit about the Twitter API (and batch files and the scheduling tasks!) in the process. If you have any questions or need some help with this, feel free to leave a comment and I’ll do my best to help you out!

15 Comments on Schedule Twitter Posts

  1. Joe Lennon says:

    Hi everyone,

    I was asked by a reader how you could change this script to read the contents of a text file and tweet that instead of passing in the arguments. If you have a single-line text file, you can simply change the line that reads:

    SET STATUS=%*

    to

    FOR /F "tokens=1,2* delims=~" %%G IN (C:\tweet.txt) DO SET STATUS=%%G

    This will tell your script to read the contents of C:\tweet.txt. However if your text file has more than one line, it will only tweet the last line from the file. If you want it to tweet each line of the text file, you can simply move the curl call inside the loop, so your batch file would look like this:

    @ECHO OFF
    FOR /F "tokens=1,2* delims=~" %%G IN (C:\twitter.txt) DO c:\curl\curl.exe
          -u joelennon:password -d status="%%G" http://twitter.com/statuses/update.xml

    Make sure the code above is all on a single line or it will not work, I have put it over multiple lines for display purposes.

  2. brownbear says:

    thanks for this tip. i noticed that the updates show the source as “web”. is it possible to change to something else like what some twitter clients do?

  3. Joe Lennon says:

    @brownbear You’ll need to register for OAuth authentication in order to change the source of the tweet (and you will need to use OAuth in your application to authenticate with the Twitter API). For more information about this see the following FAQ:
    http://apiwiki.twitter.com/FAQ#HowdoIget%E2%80%9CfromMyApp%E2%80%9DappendedtoupdatessentfrommyAPIapplication

    For more about OAuth see:
    http://apiwiki.twitter.com/OAuth-FAQ

    For example code for using OAuth (unfortunately I don’t think you’ll be able to use it in your batch script) see:
    http://apiwiki.twitter.com/OAuth-Examples

  4. Ryan says:

    GREAT tutorial! It is rare to read a technical tutorial that is both easy to understand and follow.

    My question is in regards to pulling your update from .txt file. I want to set up a text file with a list of tweets (in my case, Bible verses) that will update every morning (kind of a “Daily Bible Reading” tweet). So, my questions are:

    1) To schedule a daily tweet, would I use– at 8:00 every:M,T,W,Th,F,S,Su cmd /c c:\curl\twitter.bat –Is that correct?

    2) Is there anyway to tell it to not tweet the same thing. Ideally, I’d want it to just go down the list, in order, releasing a new tweet ever day.

    Any thoughts or advice you have would be helpful.

    Thanks
    _ryan

  5. Joe Lennon says:

    Hi Ryan,

    Thanks for your kind words about the tutorial. I’m glad you found it useful. I’ll try my best to answer your questions now.

    To send a tweet every morning you would use the following syntax (note the slash before every):

    at 8:00 /every:M,T,W,Th,F,S,Su cmd / c:\curl\twitter.bat

    You can check what tasks are scheduled using the following command:

    at

    And you can delete tasks using the following syntax:

    at [id] /delete

    (where id is the ID number of the task you wish to delete. You can get this with the previous command).

    For example:

    at 1 /delete

    As for your second question, it is possible. Assuming you have a file C:\curl\tweet.txt with each tweet on an individual line, the following batch file will tweet the last line of this text file
    each time it is called, and will remove that line from the text file when it’s done. As a result, it will not tweet the same line again. The downside of this is that when your tweet.txt file
    runs out of entries it will no longer work.

    @ECHO OFF
    FOR /F "tokens=1,2* delims=~" %%G IN (C:\curl\tweet.txt) DO SET STATUS=%%G
    c:\curl\curl.exe -u joelennon:password -d status="%STATUS%" http://twitter.com/statuses/update.xml
    TYPE c:\curl\tweet.txt | FIND /v "%STATUS%" > c:\curl\tweet.tmp
    TYPE c:\curl\tweet.tmp > c:\curl\tweet.txt
    DEL C:\curl\tweet.tmp

    If you have any problems getting this to work, be sure to let me know and I’ll see if I can help!

  6. Joe Lennon says:

    Just a quick note about the previous comment – this script deletes EVERY line in the tweet.txt file where it encounters the contents of the line it just tweeted. So if you have a line like the following

    a

    You can expect every line in your file that contains the letter a to be deleted as soon as the above line gets tweeted. The line

    TYPE c:\curl\tweet.txt | FIND /v "%STATUS%" > c:\curl\tweet.tmp

    basically says – read the file tweet.txt and output every line that DOES NOT contain the contents of the STATUS variable to the file tweet.tmp. The next line copies the contents of tweet.tmp to tweet.txt, and then we delete the tweet.tmp file. I hope this explains a bit better what exactly this script is doing. Like I said, if you have any specific questions, be sure to ask!

  7. Ryan says:

    Joe, THANKS! Can’t wait to give this a try. Will let you know how it goes.

  8. Ryan says:

    For some reason the

    at 8:00 /every:M,T,W,Th,F,S,Su cmd / c:\curl\twitter.bat

    wasn’t running. I couldn’t figure it out so I ended up scheduling the task via the task scheduler and everything seems to be in working order now. Again, thanks SO much for the help!

  9. Peter says:

    I’m running Vista as an administrator. When I run the “at” command line, I get “Access is denied”. It is not possible to set a batch file to run as administrator but perhaps a shortcut to the batch file might do it.

  10. Thanks for this awesome tutorial on how to automate Tweets on Windows. Although I don’t use Windows, I think it won’t take much to get it running on Linux. I just checked and my distro has curl installed, so it should work right away.

    I will post here once I’ve got it running.

  11. Fantastic! The script would be very useful for my twitter usage. Now, what are the chances of doing a Mac version? :-)

  12. Hi again, I got it running on my Ubuntu Linux without any problem.
    Thanks for the nice script.

  13. lear says:

    hi, thanks, this is awesome! may I ask you to post a complete script for osx that allows me scheduling of tweets read from text file (each tweet in a single line of multiple lines text file)? I am pretty lame I am afraid and if i would try to do it myself will do lots of errors…for sure! once again, thank you. very nice tutorial and you seem to have an awesome way of explaining things!

  14. Dave says:

    the single use of the tweetsfile.bat works, but not this AT command … thoughts? Thank you.
    at 8:00 /every:M,T,W,Th,F,S,Su cmd / c:\curl\tweetsfile.bat

  15. Bill says:

    Wow, this is a great tutorial and very easy to follow. Can’t wait to try it out

Leave a Reply