From 4f7ef26ce48401d6fd055ad8e684828ebb118be8 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Thu, 2 Feb 2017 02:42:15 -0800 Subject: [PATCH] New twitter plugin --- plugins/twitter.pm | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 plugins/twitter.pm diff --git a/plugins/twitter.pm b/plugins/twitter.pm new file mode 100644 index 0000000..7fed4b6 --- /dev/null +++ b/plugins/twitter.pm @@ -0,0 +1,51 @@ +no warnings 'void'; + +use Twitter::API; +use Data::Dumper; +use strict; + +open(my $fh, "<", "etc/twitterkeys") or die $!; +my ($consumer_key, $consumer_secret) = <$fh>; +close($fh); + +chomp $consumer_key; +chomp $consumer_secret; + +#die Dumper($consumer_key, $consumer_secret); + +my $client = Twitter::API->new_with_traits( + traits => [qw/ApiMethods AppAuth/], + consumer_key => $consumer_key, + consumer_secret => $consumer_secret, + ); + +# get the appauth token and set it up +my $_r = $client->oauth2_token; +$client->access_token($_r); + +sub { + my( $said ) = @_; + +# TODO make this also support getting more than one tweet. + + my ($userid) = $said->{body} =~ /^\s*(\S+)/g; + + my $timeline=$client->user_timeline($userid); + + my $tweet = $timeline->[0]; + + if ($tweet) { + my ($time, $text, $id) = @{$tweet}{qw/created_at text id/}; + my $source = $tweet->{user}{name}; + my $url = "https://twitter.com/link/status/$id"; + + print STDERR Dumper($timeline); + print "<$source> $text $url"; + } else { + print "No tweets found"; + } + + return ('handled', 'handled'); +}; +__DATA__ +Get the latest tweet from an account