In the following example we are using the Net::Twitter module to auto post a tweet along with an image into a twitter account via Oauth.
Please remember to gather the twitter account consumer key, consumer secret and access token details from twitter developer account.
#!/usr/bin/perl
use Net::Twitter;
my $twitter_username = 'Provide here twitter account username'; # twitter account username
my $twitter_password = 'Provide here twitter account password'; # twitter account password
my $nt = Net::Twitter->new(
traits => ['API::RESTv1_1', 'OAuth'],
consumer_key => 'Provide here the twitter developer consumer key',
consumer_secret => 'Provide here the twitter developer consumer secret',
);
my $image_file = '/tmp/flower.png'; # image to be posted in the twitter account
my $message = 'Hello Folks!!!'; # twit to be posted in the twitter account
my $access_token = 'Provide here the twitter app access token';
my $access_token_secret = 'Provide here the twitter app access token secret';
if ($access_token && $access_token_secret) {
$nt->access_token($access_token);
$nt->access_token_secret($access_token_secret);
}
my @filename;
@filename = ($image_file) if($image_file);
my $result;
if (scalar(@filename)) {
$result = $nt->update_with_media({status=>$message,media=>\@filename});
}
else
{
$result = $nt->update({status=>$message});
}
unless ( $result->{id} ) {
#print Dumper($result);
print qq{Twitter posting unsuccessful\n};
exit;
}
print qq{Twitter posting successful\n};
};
if ($@) {
print qq{Twitter posting unsuccessful $@\n};
exit;
}
Output:
-------
On running the Perl script from command line, it will post the specified image and message into the twitter account mentioned in the script.
