Twitter Direct Messaging Remotely Using cURL

By  on  
Twitter DM

Twitter's direct messaging functionality is great. While simple tweeting communicates "just a clever quip," direct messaging communicates "listen here b*tch, this is important enough that you need to get this message NOW via text message." Twitter's API allows you direct message remotely so now you can let your website DM you (and call you a b*tch) any time you want!

The Shell cURL Command

curl -u davidwalshblog:myPass -d "text=Testing a remote direct message via C
URL&user=fellowTweeter" http://twitter.com/direct_messages/new.xml

This example shows the cURL command as you'd type it in the terminal.

PHP Usage

$result = shell_exec('curl -u davidwalshblog:myPass -d "text=Testing a remote direct message via C
URL&user=fellowTweeter" http://twitter.com/direct_messages/new.xml');
This is how you could use the command in PHP.

Sample XML Response

<!-- Successful! -->
<?xml version="1.0" encoding="UTF-8"?>
<direct_message>
  <id>186275699</id>
  <sender_id>15759583</sender_id>
  <text>Testing a direct message.</text>
  <recipient_id>15759583</recipient_id>
  <created_at>Sat Jun 20 00:13:41 +0000 2009</created_at>
  <sender_screen_name>davidwalshblog</sender_screen_name>
  <recipient_screen_name>davidwalshblog</recipient_screen_name>
  <sender>
    <id>15759583</id>
    <name>davidwalshblog</name>
    <screen_name>davidwalshblog</screen_name>
    <location>Madison, WI, US</location>
    <description>Flexile Programmer, MooTools Core Team Member, JavaScript Fanatic, CSS Tinkerer, PHP Hacker, and web lover.</description>
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/57860553/footer-logo_normal.jpg</profile_image_url>
    <url>https://davidwalsh.name</url>
    <protected>false</protected>
    <followers_count>1834</followers_count>
    <profile_background_color>EEEEEE</profile_background_color>
    <profile_text_color>000000</profile_text_color>
    <profile_link_color>2A447B</profile_link_color>
    <profile_sidebar_fill_color>CDCDCD</profile_sidebar_fill_color>
    <profile_sidebar_border_color>999999</profile_sidebar_border_color>
    <friends_count>20</friends_count>
    <created_at>Thu Aug 07 04:18:53 +0000 2008</created_at>
    <favourites_count>3</favourites_count>
    <utc_offset>-21600</utc_offset>
    <time_zone>Central Time (US & Canada)</time_zone>
    <profile_background_image_url>http://s3.amazonaws.com/twitter_production/profile_background_images/15318223/twitter-background.png</profile_background_image_url>
    <profile_background_tile>false</profile_background_tile>
    <statuses_count>1734</statuses_count>
    <notifications>false</notifications>
    <verified>false</verified>
    <following>false</following>
  </sender>
  <recipient>
    <id>15759583</id>
    <name>davidwalshblog</name>
    <screen_name>davidwalshblog</screen_name>
    <location>Madison, WI, US</location>
    <description>Flexile Programmer, MooTools Core Team Member, JavaScript Fanatic, CSS Tinkerer, PHP Hacker, and web lover.</description>
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/57860553/footer-logo_normal.jpg</profile_image_url>
    <url>https://davidwalsh.name</url>
    <protected>false</protected>
    <followers_count>1834</followers_count>
    <profile_background_color>EEEEEE</profile_background_color>
    <profile_text_color>000000</profile_text_color>
    <profile_link_color>2A447B</profile_link_color>
    <profile_sidebar_fill_color>CDCDCD</profile_sidebar_fill_color>
    <profile_sidebar_border_color>999999</profile_sidebar_border_color>
    <friends_count>20</friends_count>
    <created_at>Thu Aug 07 04:18:53 +0000 2008</created_at>
    <favourites_count>3</favourites_count>
    <utc_offset>-21600</utc_offset>
    <time_zone>Central Time (US & Canada)</time_zone>
    <profile_background_image_url>http://s3.amazonaws.com/twitter_production/profile_background_images/15318223/twitter-background.png</profile_background_image_url>
    <profile_background_tile>false</profile_background_tile>
    <statuses_count>1734</statuses_count>
    <notifications>false</notifications>
    <verified>false</verified>
    <following>false</following>
  </recipient>
</direct_message>



<!-- ERROR RESPONSE -->
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/direct_messages/new.xml</request>
  <error>You cannot send messages to users who are not following you.</error>
</hash>

Loads of information if your DM is successful -- a simple error response if your attempt fails.

So why automate a DM? I think one handy time to implement direct message automation is when you want to receive text messages when events occur on the website. Say you're a salesperson and you have a "request a phone call" form on your site. Since you're out of the office much of the day (driving), you don't have the means to check your email. Instead of setting up a text messaging / SMS system, simply set up a Twitter account and take advantage of Twitter's texting capabilities.

Would you ever want automated tweets or direct messages from your website?

Recent Features

Incredible Demos

  • By
    MooTools 1.3 Browser Object

    MooTools 1.3 was just released and one of the big additions is the Browser object.  The Browser object is very helpful in that not only do you get information about browser type and browser versions, you can gain information about the user's OS, browser plugins, and...

  • By
    Create a Trailing Mouse Cursor Effect Using MooTools

    Remember the old days of DHTML and effects that were an achievement to create but had absolutely no value? Well, a trailing mouse cursor script is sorta like that. And I'm sorta the type of guy that creates effects just because I can.

Discussion

  1. PHP has a curl implementation. Use that instead of doing a command execution.

  2. hmm, could be nice with a new mootwitter plugin, for direct messaging from own site, or twitting new blogs directly in a short version on twitter

  3. Andreson

    When I use PHP to login to facebook, it logs me out of facebook in my web-browser.

    If you use the same cookie jar as your webrowser in the script, then this shouldn’t happen. I’ve had trouble doing this with Safari, because cURL can’t parse the .plist format it uses for it’s cookie jar. I believe Firefox uses a format for it’s cookie jar that cURL can understand, even on a Mac.

    Also, it’s probably a good idea to only login if you have to. Facebook keeps you logged in for a very long time, but unfortunately does not support multiple logins. To tell if I’ve been logged out, I look at the of the page that curl gives me microsoft test.

    Personally I think facebook should support multiple logins, but unfortunately there’s nothing I can do about this.

  4. OK, Question…. is there a way to have twitter trigger a web script upon RECEIPT of a tweet? I would like to write a “tweet response” script that replies to codes sent in a tweet and would like it to be event driven, upon an incoming tweet, rather than something clumsy an inefficient like a cron job checking for new tweets every 30 seconds… is this possible to anyone’s knowledge?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!