Previously, I wrote a post showing the basics on how to send a tweet using Perl and Net::Twitter. I showed the easiest you can do it.
Below is the code that I use most often. It is a command-line tool, where it's used something along the lines of
Below is the code that I use most often. It is a command-line tool, where it's used something along the lines of
named_twitter.pl jacobydave text of my tweet
. Except, that's not how I type it, thanks to my .alias
file. I have twitter
aliased to '~/bin/named_twitter jacobydave '
and normally tweet like twitter this is how I tweet
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# largely taken verbatim from | |
# http://search.cpan.org/dist/Net-Twitter/lib/Net/Twitter/Role/OAuth.pm | |
# Next step is to get the keys and secrets to a config. | |
use feature qw{ say } ; | |
use strict ; | |
use Carp ; | |
use Data::Dumper ; | |
use Encode 'decode' ; | |
use IO::Interactive qw{ interactive } ; | |
use Net::Twitter ; | |
use WWW::Shorten 'TinyURL' ; | |
use YAML::XS qw{ DumpFile LoadFile } ; | |
use utf8 ; | |
binmode STDOUT, ':utf8' ; | |
# next step, add Getopt::Long into the mix | |
my $config_file = $ENV{ HOME } . '/.twitter.cnf' ; | |
my $config = LoadFile( $config_file ) ; | |
my ( $user, @status ) = map { decode('UTF-8', $_) } @ARGV ; | |
@status = map { | |
my $s = $_ ; | |
if ( $s =~ m{^https?://}i ) { | |
$s = makeashorterlink( $s ) ; | |
} | |
$s ; | |
} @status ; | |
my $status = join ' ', @status ; | |
if ( length $status < 1 ) { | |
$status = <STDIN> ; | |
my @status = split /\s/, $status ; | |
$user = shift @status ; | |
@status = map { | |
my $s = $_ ; | |
if ( $s =~ m{^http://}i ) { | |
$s = makeashorterlink( $s ) ; | |
} | |
$s ; | |
} @status ; | |
$status = join ' ', @status ; | |
} | |
if ( length $status > 140 ) { | |
say { interactive } 'Too long' ; | |
say { interactive } length $status ; | |
say { interactive } $status ; | |
exit ; | |
} | |
if ( length $status < 1 ) { | |
say { interactive } 'No content' ; | |
say { interactive } length $status ; | |
say { interactive } $status ; | |
exit ; | |
} | |
# GET key and secret from http://twitter.com/apps | |
my $twit = Net::Twitter->new( | |
traits => [qw/API::RESTv1_1/], | |
consumer_key => $config->{ consumer_key }, | |
consumer_secret => $config->{ consumer_secret }, | |
ssl => 1 , | |
) ; | |
# You'll save the token and secret in cookie, config file or session database | |
my ( $access_token, $access_token_secret ) ; | |
( $access_token, $access_token_secret ) = restore_tokens( $user ) ; | |
if ( $access_token && $access_token_secret ) { | |
$twit->access_token( $access_token ) ; | |
$twit->access_token_secret( $access_token_secret ) ; | |
} | |
unless ( $twit->authorized ) { | |
# You have no auth token | |
# go to the auth website. | |
# they'll ask you if you wanna do this, then give you a PIN | |
# input it here and it'll register you. | |
# then save your token vals. | |
say "Authorize this app at ", $twit->get_authorization_url, | |
' and enter the PIN#' ; | |
my $pin = <STDIN> ; # wait for input | |
chomp $pin ; | |
my ( $access_token, $access_token_secret, $user_id, $screen_name ) = | |
$twit->request_access_token( verifier => $pin ) ; | |
save_tokens( $user, $access_token, $access_token_secret ) ; | |
} | |
if ( $twit->update( $status ) ) { | |
say { interactive } $status ; | |
} | |
else { | |
say { interactive } 'FAIL' ; | |
} | |
#========= ========= ========= ========= ========= ========= ========= | |
sub restore_tokens { | |
my ( $user ) = @_ ; | |
my ( $access_token, $access_token_secret ) ; | |
if ( $config->{ tokens }{ $user } ) { | |
$access_token = $config->{ tokens }{ $user }{ access_token } ; | |
$access_token_secret = | |
$config->{ tokens }{ $user }{ access_token_secret } ; | |
} | |
return $access_token, $access_token_secret ; | |
} | |
sub save_tokens { | |
my ( $user, $access_token, $access_token_secret ) = @_ ; | |
$config->{ tokens }{ $user }{ access_token } = $access_token ; | |
$config->{ tokens }{ $user }{ access_token_secret } = $access_token_secret ; | |
DumpFile( $config_file, $config ) ; | |
return 1 ; | |
} | |
__DATA__ | |
This program uses a YAML file to hold the configuration data, and | |
it looks like this | |
--- | |
consumer_key: ckckckckckckckckckckck | |
consumer_secret: cscscscscscscscscscscscscscscscscscscscs | |
tokens: | |
your_screen_name: | |
access_token: 1111111111-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
access_token_secret: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz |
This isn't to say I never automate tweets; I certainly do. But it is a rare part of what I do with the Twitter API and Net::Twitter. I will dive deeper into issues with tweeting and direct messages, both in a technical and social matter, in a later post.
But I said that you have the consumer key and secret, which identify you as a service, and the access token and secret, which identify you as a user of the service. In the above code sample, I use YAML to store this data, but you could use JSON, Sqlite or anything else to store it. Among other benefits, you can put your code into GitHub, as I did above, without exposing anything.
As I proceed, I will assume that tokens are handled somehow and proceed directly to the cool part.
Again, you get the consumer key by going to apps.twitter.com and creating a new app.
But I said that you have the consumer key and secret, which identify you as a service, and the access token and secret, which identify you as a user of the service. In the above code sample, I use YAML to store this data, but you could use JSON, Sqlite or anything else to store it. Among other benefits, you can put your code into GitHub, as I did above, without exposing anything.
As I proceed, I will assume that tokens are handled somehow and proceed directly to the cool part.
Again, you get the consumer key by going to apps.twitter.com and creating a new app.
You log into apps.twitter.com with your Twitter account, click "Create New App" and fill in the details. When I created my three, you had to specifically choose "can send DMs" specifically, so if you're creating apps to follow along, I do suggest you allow yourself that option.
No comments:
Post a Comment