Just an example: Sending tweets with attached images from the cli

I had to search around for a long time to find the solution. Therefore I like to share this code with you. It’s based on the Perl module Net::Twitter Version 3.18004. The code is based on the Examples from the module documentation.

You need to have the twitter api keys: https://dev.twitter.com/apps

Save it as a file, fill in your api keys and do a chmod +x and run it: First parameter: “your text”, second parameter: /path/to/your/image.jpg.

#!/usr/bin/perl -w

use Net::Twitter;
use Scalar::Util 'blessed';

# Edit these keys:
$consumer_key   = "";
$consumer_secret= "";
$token          = "";
$token_secret   = "";

# As of 13-Aug-2010, Twitter requires OAuth for authenticated requests
my $nt = Net::Twitter->new(
        traits   => [qw/OAuth API::REST/],
        consumer_key        => $consumer_key,
        consumer_secret     => $consumer_secret,
        access_token        => $token,
        access_token_secret => $token_secret,
);

$image= [ $ARGV[1] ];

my $result = $nt->update_with_media($ARGV[0], $image);

if ( my $err = $@ ) {
        die $@ unless blessed $err && $err->isa('Net::Twitter::Error');

        warn "HTTP Response Code: ", $err->code, "\n",
        "HTTP Message......: ", $err->message, "\n",
        "Twitter error.....: ", $err->error, "\n";
}
Tags: ,