sms

Twitter SMS script update

I updated my Update multiple twitter accounts via SMS from one phone script to use OAuth, and made a note about gathering OAuth credentials for plain-text storage at Getting twitter OAuth tokens & secrets.

(Most apps are a little more interactive than this one and would store the details in a database, but it seems overkill for this application!)

Finally thought of a use for my twitter script!

Yes, it took some time, but as I was sat in Wetherspoon's last night, I realised something else I could hook in to my twitter script.

We have a budget system that lives on the web server - we visit a web site, add purchases or income, and at the end of the month we should be able to work out how much we've saved (or not). Having bought my dinner, I was reading Facebook on the phone, when I thought it'd be useful if I could update the budget system from my mobile.

Voila! I could text to twitter with a certain hash tag and my script could handle it! Brilliant!

Admittedly in this case it's not perfect - I can only add basic information - but now the budget system will display a "temporary purchase" and ask you to fill in the fine detail the next time you visit the site. So, any time I spend money, I can text the details before I forget, and finish it off later on.

I then thought about another system I always wanted to start again - I used to keep closer track of my mileage and petrol costs, and thus miles per gallon. It's tricky to remember to jot down the mileage every time - so maybe if I could update by text, it'd help? Well, I've set up a system to record the information now, so I'll have to wait and see if I actually use it.

Finally - for general reminders - I set up the tags e[mail], r[eminder] and n[ote] to email me the tweet, for perusal at a later date.

I also set the script up to log actions so I had some sort of idea what was going on, or what strings didn't work.

I've updated the Update multiple twitter accounts via SMS from one phone page with the new script.

Taking Twitter SMS further

Ok, so my Update multiple twitter accounts via SMS from one phone covered how to use a script to send tweets from one account to many others. Simple and effective.

What if you wanted to do more? What if you wanted to control more with your tweets?

It'd be very easy to hook the script into a facebook app to update your status, for example (though I think you can already set up facebook to update your status via SMS). Or perhaps you could have a special code to look for that would do something on your server? I can't think of anything I'd want to do on mine, but then it only hosts websites - if I had a server in my house hooked up to other devices, it could do anything...

Update multiple twitter accounts via SMS from one phone

Tags:

Ever wanted to update multiple twitter accounts from your phone, but you don't have a smartphone and have to rely on SMS? It is possible - see Update multiple twitter accounts via SMS from one phone.

Update multiple twitter accounts via SMS from one phone

Ever wanted to update multiple twitter accounts via SMS, but only have one phone?

There is a sneaky way of doing this - provided you don't mind losing ~3 characters!

1. Create a new account

Yes, create yet another account. This account should have no friends or followers, and should have the tweets protected. That's right, so no-one can see them. You should set your phone up to update this account... bear with us! We'll refer to this account as the "Bucket" account.

2. The script of cleverness

What we now do is set up a script to read the tweets from this bucket account and forward them on to the correct real account. How? Start your message with a hash tag reference, and make sure the script knows about this tag!

There is a great example of this up and running and accepting new registrations at bucket.harrybailey.com but I chose to implement my own, mostly because I'm just like that, but for a few reasons listed below.

So say I have 3 accounts I want to update - 'a', 'b' and 'c'. I can assign these to appropriate hash tags - in this case, '#a', '#b' and '#c'. Then, when the script reads the tweets from the bucket, it checks for a hash tag, and knows which account to forward the tweet to. It will also remove the hash tag.

What if there is no hash tag? I set up a default account option, so I don't have to remember to add tags for my most-used twitter account.

I've then set up three tags - #e, #r and #n - to send me an email - and if any other tags are detected, it'll email me with an error message.

It will also log all actions, to make bugfixing easier.

There's an example under #p for calling another script that could process the tweet - see Finally thought of a use for my twitter script! for my ideas.

Harry's site is excellent - but currently is restricted to listing 3 accounts (including the bucket account), so only allowing you update two different accounts; and it only checks every 5 minutes. That's an API rate limit issue, but since my script is only going to do my accounts, I've been able to set the script to run every minute with a cron job. And, of course, with my script being on my site, it allows me to do much more...

Here's my PHP code. I used abraham's twitteroauth libraries but hopefully this is a simple enough example to be ported to other libraries and languages.

<?
 
define('CONSUMER_KEY', 'MYKEY');
define('CONSUMER_SECRET', 'MYSECRET');
 
include('twitteroauth.php');
 
$mailTo = 'default@email.addresss';
 
$account['a'] = 'account_1_name';
$token['a'] = 'account_1_token';
$secret['a'] = 'account_1_secret';
$account['b'] = 'account_2_name';
$token['b'] = 'account_2_token';
$secret['b'] = 'account_2_secret';
$account['bucket'] = 'bucket_account_name';
$token['bucket'] = 'bucket_account_token';
$secret['bucket'] = 'bucket_account_secret';
 
$defaultAccount = 'a';
 
// Connect to Bucket
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['bucket'], $secret['bucket']);
$tweets = $connection->get('statuses/user_timeline', array('count' => 5));
 
if (is_array($tweets))
{
  foreach($tweets AS $tweet)
  {
    $matches = array();
    $accountRef = $defaultAccount;
    if (preg_match('/^#([a-z])/', $tweet->text, $matches))
    {
      // update the account reference
      $accountRef = $matches[1];
      // strip the tag
      $tweet->text = substr($tweet->text, 3);
    }
 
    switch($accountRef)
    {
      case 'a':
      case 'b':
      case 'c':
          sendTweet($tweet->text, $accountRef);
          break;
      case 'e':
      case 'r':
      case 'n':
          mail($mailTo, 'Reminder from text message', $tweet->text);
          writeLog('Email reminder: '.$tweet->text);
          break;
      case 'p':
          include('addPetrol.php');
          writeLog('Petrol Record: '.$tweet->text);
          break;
      default:
          mail($mailTo, 'Unknown twitter request', '['.$accountRef.']: '.$tweet->text);
          writeLog('Unknown request: ['.$accountRef.']: '.$tweet->text);
    }
 
    // remove from the bucket
    $connection->post('statuses/destroy', array('id' => $tweet->id));
  }
}
 
function sendTweet($text, $accountRef)
{
	global $account, $token, $secret;
 
	$sendTweet = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token[$accountRef], $secret[$accountRef]);
	$sendTweet->post('statuses/update', array('status' => $text));
	writeLog('Sent "'.$text.'" to "'.$account[$accountRef].'"');
}
 
function writeLog($msg)
{
  $f = fopen('/var/log/tweets','a');
  fwrite($f, date('r').' '.$msg."\n");
  fclose($f);
}

Syndicate content