O'Reilly

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>http://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>http://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?

Track.js Error Reporting

Upcoming Events

Recent Features

Incredible Demos

  • Implement the Google AJAX Search API

    Let's be honest...WordPress' search functionality isn't great. Let's be more honest...no search functionality is better than Google's. Luckily for us, Google provides an awesome method by which we can use their search for our own site: the Google AJAX Search API....

  • Create Twitter-Style Dropdowns Using jQuery

    Twitter does some great stuff with JavaScript. What I really appreciate about what they do is that there aren't any epic JS functionalities -- they're all simple touches. One of those simple touches is the "Login" dropdown on their homepage. I've taken...

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!

Recently on David Walsh Blog

  • OâReilly Velocity Conference â Amsterdam

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • CanIUse Command Line

    Every front-end developer should be well acquainted with CanIUse, the website that lets you view browser support for browser features.  When people criticize my blog posts for not detailing browser support for features within the post, I tell them to check CanIUse:  always up to date, unlike...

  • Generating Alternative Stylesheets for Browsers Without @media

    If your CSS code is built with a mobile-first approach, it probably contains all the rules that make up the "desktop" view inside @media statements. That's great, but browsers that don't support media queries (IE 8 and below) will simply ignore them, ending up getting the...

  • Serve a Directory with PHP

    Many developers have a giggle at PHP, even looking down at the language, but let's be honest:  most of our blogs are powered by it (WordPress) and it's a great language to dabble around with.  I cut my teeth on PHP, though I prefer to avoid PHP these days. But...

  • Tips you can Use to Build an Excellent eCommerce Experience

    There are many reasons to build a good eCommerce experience if you are planning to build an eCommerce website. Visitors can place items in their cart and leave your site without making a purchase. In fact, three out of every four of them will do so....