Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2010/08/30

My Facebook Status Solution

You might remember me posting how to post status updates to Buzz, albeit via Perl. I mentioned how I wanted to do the same or similar with Facebook. I got nice suggestions of how to handle it via WWW::Facebook::API, and I tried to wrestle with Facebook's OAuth or similar implementation to get the access I need to do what I want. And then I figured out another way.

  1. #!/usr/bin/perl  
  2.   
  3. # usage: echo big long string of text | facebook.pl  
  4.   
  5. # returns "big long string of text"  
  6.   
  7. use 5.010 ;  
  8. use strict ;  
  9. use warnings ;  
  10. use Carp ;  
  11. use Data::Dumper ;  
  12. use Net::SMTP::TLS ;  
  13. use Getopt::Long ;  
  14. use IO::Interactive qw{ interactive } ;  
  15. use subs qw{ send_mail get_credentials } ;  
  16.   
  17. my $status = join ' '@ARGV ;  
  18. if ( length $status < 1 ) {  
  19.     while ( <stdin> ) {  
  20.         $status .= $_ ;  
  21.         }  
  22.     chomp $status ;  
  23.     }  
  24.   
  25. my $identity = 'MY_IDENTITY_STRING' ;  
  26. my @to ;  
  27. push @to'GETYOUROWNSPECIALEMAILFROM@m.facebook.com' ;  
  28. my $body = $status ;  
  29.   
  30. my %msg ;  
  31. $msg{ body }    = $body ;  
  32. $msg{ to }      = \@to ;  
  33. $msg{ cc }      = \@cc ;  
  34.   
  35. say send_mail %msg ;  
  36.   
  37. exit ;  
  38.   
  39. sub get_credentials {  
  40.   
  41.     #this is the point where my setup sucks  
  42.     my %creds ;  
  43.     my $config = '/home/jacoby/.smtp_identities' ;  
  44.     if ( open my $fh'<'$config ) {  
  45.         while ( <$fh> ) {  
  46.             my $line = $_ ;  
  47.             chomp $line ;  
  48.             $line = ( split m{\#}mx )[ 0 ] ;  
  49.             next if length $line < 1 ;  
  50.             $line =~ s{\s}{}gmx ;  
  51.             my @line = split m{,}mx, $line ;  
  52.             next if $line[ 0 ] ne $identity ;  
  53.             $creds{ from }     = $line[ 1 ] ;  
  54.             $creds{ server }   = $line[ 2 ] ;  
  55.             $creds{ username } = $line[ 3 ] ;  
  56.             $creds{ password } = $line[ 4 ] ;  
  57.             }  
  58.         close $fh ;  
  59.         }  
  60.     if ( defined $creds{ username } ) {  
  61.         return %creds ;  
  62.         }  
  63.     say 'No identity chosen' ;  
  64.     exit 1 ;  
  65.   
  66.     }  
  67.   
  68. sub send_mail {  
  69.     my %msg     = @_ ;  
  70.     my %creds   = get_credentials ;  
  71.     my $body    = $msg{ body } ;  
  72.     my $to      = $msg{ to } ;  
  73.     my @to      = @$to ;  
  74.     my $mailer = new Net::SMTP::TLS( $creds{ server },  
  75.                                      Hello    => $creds{ server },  
  76.                                      Port     => 587,  
  77.                                      User     => $creds{ username },  
  78.                                      Password => $creds{ password }, ) or croak $!;  
  79.     $mailer->mail( $creds{ from } ) ;  
  80.     $mailer->to( @to ) ;  
  81.     $mailer->data ;  
  82.     $mailer->datasend( 'Subject: ' . $body . "\n" ) ;  
  83.     $mailer->datasend( 'From: ' . $creds{ from } . "\n" ) ;  
  84.     $mailer->datasend( 'To: ' . ( join ','@to ) . "\n" ) ;  
  85.     $mailer->datasend( "\n" ) ;  
  86.     $mailer->datasend( '' ) ;  
  87.     $mailer->dataend ;  
  88.     $mailer->quit ;  
  89.     return $msg{ body } ;  
  90.     }  
  91. </stdin>  

I already had it, so I just had to modify it slightly, but it came to me. You can set status and upload images via email. I just hand to double-check the right way — statuses in subject, not body — and came right up.

get_credentials is my method for not hard-coding information into the code, and here I do exactly that for the $to email and the $identity string.

I could see using the big and scary Facebook API if I wanted to code up the next Farmville. I know my wife wants me to do that. But for this small purpose, there's little need to go through all that.

No comments:

Post a Comment