From faec5dcd5dbfe4f92955aadf906faa5ddbd3fdf0 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Fri, 24 Mar 2017 15:33:55 -0400 Subject: [PATCH] Add a some new features to the twitter plugin. get multiple tweets from a user, and search for hashtags --- .gitmodules | 3 +++ plugins/twitter.pm | 39 ++++++++++++++++++++++++++++----------- wiki | 1 + 3 files changed, 32 insertions(+), 11 deletions(-) create mode 160000 wiki diff --git a/.gitmodules b/.gitmodules index 7c7a0b8..1843086 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "jail"] path = jail url = https://github.com/simcop2387/perlbot-jail +[submodule "wiki"] + path = wiki + url = https://github.com/perlbot/perlbuut.wiki.git diff --git a/plugins/twitter.pm b/plugins/twitter.pm index 7fed4b6..93e54bc 100644 --- a/plugins/twitter.pm +++ b/plugins/twitter.pm @@ -23,27 +23,44 @@ my $client = Twitter::API->new_with_traits( 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]; +sub display_tweet { + my $tweet = shift; 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"; } +} + +sub { + my( $said ) = @_; + +# TODO make this also support getting more than one tweet. + + if ($said->{body} =~ /^\s*(#\S+)/) { + # hash tags. omg. + my $search = $client->search($1); + + open (my $fh, ">", "/tmp/twitter"); + print $fh Dumper($search); + + my $tweets = $search->{statuses}; + my $tweet = $tweets->@[rand() * $tweets->@*]; + + display_tweet $tweet; + } else { + my ($userid, $count) = $said->{body} =~ /^\s*(\S+)(?:\s+(\d+))?/g; + + my $timeline=$client->user_timeline($userid); + my $tweet = $timeline->[$count//0]; + + display_tweet $tweet; + } return ('handled', 'handled'); }; diff --git a/wiki b/wiki new file mode 160000 index 0000000..b31b40f --- /dev/null +++ b/wiki @@ -0,0 +1 @@ +Subproject commit b31b40fd425f599acf48bb5d16ce26fa9f61fb46