This week at Will Hall Online we’ve been building a system on one of our sites which periodically tweets a link to an existing piece of content, along with some custom text, a description and hashtags.
Possible solutions included:
- Writing a custom module
- Try to use the contributed twitter_queue module
- Construct something using the standard Twitter contrib module, views and rules.
We went for the third option in the end, firstly because the working of the
twitter_queue module were somewhat mysterious, and secondly because writing a custom module seemed like unnecessary extra effort when we could just use the enormously powerful views and rules modules to come up with something robust and flexible.
A quick read-up on the functioning of the Twitter Post module happily led us to (http://drupal.stackexchange.com/questions/50796/how-can-i-auto-tweet-any-node-of-a-specific-type-to-twitter)[this post on Stack Exchange (thanks filmoreha)], which pointed us generally in the right direction. However, it’s a bit sketchy on the details, so we thought we’d blog something that steps you through it a bit more.
The process, as loosely outlined by filmoreha, is as follows:
- Install and enable Rules, Views, Views Bulk Operations, Flag and Twitter, if you haven’t already.
- Set up the Twitter module to utilise a twitter account.
- Create a flag to be used to denote which content needs tweeting. Make it a global flag which can only be used by administrator level users.
- Build a view which displays an unformatted list of the titles of all of the content you want to tweet, and a views bulk operations field which includes the ability to flag and deflag content.
- Flag all of this content with the flag using views bulk ops. You may want to sort your content at this point as well.
- Change the filter on the view so that it only displays flagged content.
Create a rule triggered when cron is run, which has three actions:
- Loads the first node returned by the view (we used the miscellaneous component of the Rules Bonus Pack for doing this, which is very easy, but we suspect there’s another way of doing this without using what is an unstable project). You are required to assign a label to the loaded node at this point.
- Posts to Twitter. If you’ve managed to get action 1 working properly, you should have access to tokens for the message value of the twitter post. Obviously you can put whatever else you want in the message, but make sure you won’t exceed the 140 character limit.
- Unflag the node using the ‘unflag a content’ action. Again, you should access to the node in the content and user data selector fields. For the user field, you can just use the node’s :author, because the flag is global and therefore either on or off, regardless of who is assigning it.
That should be sufficient to get the site tweeting content every time cron is run.