Tweet your Blog API post with the Twitter Durpal Module

The Drupal Twitter module will automatically tweet whenever you add content to your web site. This can be set to tweet when you post a page, story or blog content type. However this only occurs when you create content interactively using the create content form on your web site.

My Drupal web site is used primarily as my bloging software and I create my content off line on my Mac using Textmate. The textmate blogging module supports the xmlrpc API very nicely. See my previous blog entry for information on how do this.

However, when posting to my blog from Textmate using the Blog API the twitter module stays mute. I did some googlesearch (research using google) and didn't find a solution. However, I noticed that the twitter module also allows you to create a "post to twitter action". Once you've done this you can set a trigger to fire the twitter action which will fire when you post from the API.

Here's how to set it up. I am running Drupal 6.6 and YMMV with this or other versions.

Twiter Module

After you install the twitter module go into you administrator -> build -> modules and enable the twitter module and the twitter actions modules. The twitter actions module is installed with the twitter module.

Enable Twitter Modules

Trigger Module

Also, ensure that the trigger module is enabled. This is an optional core module so you will not need to download and install it as you did with the twitter module.

Create Post to Twitter Action

Once you have the twitter modules enabled navigation to administrator -> site configuration -> actions. Navigate to the bottom of the page to the "Make a new action available" select "Post a message to twitter..." in the drop down and press the create button.

Create Post To Twitter Action

After pressing the create button you will be brought to the "Create an advanced action page". This page is pretty straight forward. Enter a description for the action, which I left as the default "Post a message to twitter". Also, enter your twitter account id and password. Yes, the moudule is going to need your twitter password in order to tweet from your account. In the message text box specify the message you would like tweeted when you post to your blog. Below the message text box is a small help text that shows what tokens are available for this action. I use the following message:

Blogged: %title (%node_url)

Configure Post To Twitter Action

Twitter Trigger

After you have set up to action you need to configure an trigger that will, well, "trigger" that action. So navigate to administer -> site building -> triggers. At the top of the triggers setup select the "content" tab. In the "Trigger: after saving a new post" section select the "Post a message to twitter" action.

Assign Post To Twitter Action to New Post Trigger

After you click the assign button you should see the action listed for this trigger.

Post To Twitter Action Assigned to Trigger

That's all there is to it. Now when you post a blog entry (or any content type) from the xmlrpc API the trigger will fire and the action will take place and tweet your message using your twitter account. Sweet.

Caveats

All this works great. However I have found two small issues.

  1. If you setup a twitter account on the "My Account" page and post content using the online form as administrator by default it is going to post to twitter. You need to make sure you click off the post to twitter before you publish the content or you will get two tweets for the post.

  2. The tweet will contain the /node/#### URL and not the auto alias created by the pathauto module. If anyone knows a token that will give me the alias. Let me know.

Comments

Only tweeting public nodes?

I am using an access control module so that some (forums) nodes are made private and are not to be tweeted - how can this be accounted for in the actions? I only want publicly accessible nodes to be tweeted.

resolved issue I think.

I altered the twitter_actions.module so that it automagicly converts the url (using existing code) Then I disabled twitter posting in settings/twitter since my trigger/action will send a tweet for me. I modified/add code starting around line 160 // Node-based variable translation is only available if we have a node. if (isset($node) && is_object($node)) { $variables = array_merge($variables, array( '%uid' => $node->uid, '%node_url' => twitter_actions_shorten_url(url('node/'. $node->nid, array('absolute' => TRUE))), '%node_type' => node_get_types('name', $node), '%title' => $node->title, '%teaser' => $node->teaser, '%body' => $node->body ) ); } $message = strtr($context['message'], $variables);

module_load_include('inc', 'twitter'); twitter_set_status($context['screen_name'], $context['password'], $message); }

/** * Very lightweight helper function to generate a TinyURL for a given post. */ function twitter_actions_shorten_url($url) { if (module_exists('shorten')) { return shorten_url($url); } else { $response = drupal_http_request("http://tinyurl.com/api-create.php?url=" . $url); if ($response->code == 200) { return $response->data; } else { return $url; } } }

Wow!

Works as advertised. Really a powerful thing to be able to use feedapi with twitter. Thanks so much for posting this info, greatly appreciated.

Great post!

Helped me to find out a Twitter Drupal module exists! :)