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.

#!/usr/bin/perl

# usage: echo big long string of text | facebook.pl

# returns "big long string of text"

use 5.010 ;
use strict ;
use warnings ;
use Carp ;
use Data::Dumper ;
use Net::SMTP::TLS ;
use Getopt::Long ;
use IO::Interactive qw{ interactive } ;
use subs qw{ send_mail get_credentials } ;

my $status = join ' ', @ARGV ;
if ( length $status < 1 ) {
    while (  ) {
        $status .= $_ ;
        }
    chomp $status ;
    }

my $identity = 'MY_IDENTITY_STRING' ;
my @to ;
push @to, 'GETYOUROWNSPECIALEMAILFROM@m.facebook.com' ;
my $body = $status ;

my %msg ;
$msg{ body }    = $body ;
$msg{ to }      = \@to ;
$msg{ cc }      = \@cc ;

say send_mail %msg ;

exit ;

sub get_credentials {

    #this is the point where my setup sucks
    my %creds ;
    my $config = '/home/jacoby/.smtp_identities' ;
    if ( open my $fh, '<', $config ) {
        while ( <$fh> ) {
            my $line = $_ ;
            chomp $line ;
            $line = ( split m{\#}mx )[ 0 ] ;
            next if length $line < 1 ;
            $line =~ s{\s}{}gmx ;
            my @line = split m{,}mx, $line ;
            next if $line[ 0 ] ne $identity ;
            $creds{ from }     = $line[ 1 ] ;
            $creds{ server }   = $line[ 2 ] ;
            $creds{ username } = $line[ 3 ] ;
            $creds{ password } = $line[ 4 ] ;
            }
        close $fh ;
        }
    if ( defined $creds{ username } ) {
        return %creds ;
        }
    say 'No identity chosen' ;
    exit 1 ;

    }

sub send_mail {
    my %msg     = @_ ;
    my %creds   = get_credentials ;
    my $body    = $msg{ body } ;
    my $to      = $msg{ to } ;
    my @to      = @$to ;
    my $mailer = new Net::SMTP::TLS( $creds{ server },
                                     Hello    => $creds{ server },
                                     Port     => 587,
                                     User     => $creds{ username },
                                     Password => $creds{ password }, ) or croak $!;
    $mailer->mail( $creds{ from } ) ;
    $mailer->to( @to ) ;
    $mailer->data ;
    $mailer->datasend( 'Subject: ' . $body . "\n" ) ;
    $mailer->datasend( 'From: ' . $creds{ from } . "\n" ) ;
    $mailer->datasend( 'To: ' . ( join ',', @to ) . "\n" ) ;
    $mailer->datasend( "\n" ) ;
    $mailer->datasend( '' ) ;
    $mailer->dataend ;
    $mailer->quit ;
    return $msg{ body } ;
    }

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.
Post a Comment