diff options
| author | Teddy Wing | 2016-09-11 01:02:17 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-09-11 01:02:17 -0400 |
| commit | 6f070871083699174e5f7ed6ce8f42e56ecf4f8e (patch) | |
| tree | dae85e0377d169bde56574bfbd01dc27435400bc | |
| parent | 99c105d4460813f0ae7e0f54f68a067cbf14bab4 (diff) | |
| download | irssi-slack-profile-6f070871083699174e5f7ed6ce8f42e56ecf4f8e.tar.bz2 | |
If the users list is not available, fetch it from the Slack API
In `find_user`, we check whether the Storable file is there (and
non-zero). If it isn't, we fetch the users list from the Slack API.
Renamed the subroutine containing the HTTP code to `fetch_users_list`
from `users_list` to better convey the fact that it has to reach out to
the web in order to get it.
Store the Storable object in the Irssi scripts directory (instead of our
local development directory as it was previously).
Use the correct Slack API URL in our HTTP request, and also include our
Slack API token from the Irssi settings.
Add better error handling and messaging when the API request fails for
whatever reason. Die on error so we don't end up executing the
`retrieve` line (otherwise this line will cause an error complaining
that the `users.list.plstore` file doesn't exist if it hasn't yet been
created).
If the API call succeeds and Slack gives us an 'ok' response, then
`store` the JSON-decoded users list array in our serialized cache file
and assign it to the `@users_list` global variable so we additionally
have it in memory..
| -rw-r--r-- | slack-profile.pl | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/slack-profile.pl b/slack-profile.pl index 79c2fe2..89f2a8a 100644 --- a/slack-profile.pl +++ b/slack-profile.pl @@ -9,6 +9,7 @@ use LWP::UserAgent; use HTTP::Request::Common; use Mozilla::CA; use Storable; +use URI; use vars qw($VERSION %IRSSI); use Irssi; @@ -24,25 +25,39 @@ $VERSION = '1.00'; my @users_list; -sub users_list { +sub users_list_cache { + Irssi::get_irssi_dir() . '/scripts/users.list.plstore'; +} + +sub fetch_users_list { my $ua = LWP::UserAgent->new; $ua->agent('Mozilla/5.0'); - $token = Irssi::settings_get_str('slack_profile_token'); + my $token = Irssi::settings_get_str('slack_profile_token'); + my $url = URI->new('https://slack.com/api/users.list'); + $url->query_form(token => $token); - my $req = HTTP::Request->new(GET => 'http://ip.jsontest.com/'); + my $req = HTTP::Request->new(GET => $url); $req->header('content-type' => 'application/json'); my $resp = $ua->request($req); if ($resp->is_success) { - my $message = $resp->decoded_content; - print $message; + my $payload = decode_json($resp->decoded_content); + + if ($payload->{'ok'}) { + @users_list = @{$payload->{'members'}}; + store \@users_list, users_list_cache; + } + else { + Irssi::print("Error from the Slack API: $payload->{'error'}"); + die 'Unable to retrieve users from the Slack API'; + } } else { - print $resp->code . "\n"; - print $resp->message . "\n"; + Irssi::print("Error calling the Slack API: ($resp->code) $resp->message"); + die 'Unable to communicate with the Slack API'; } } @@ -50,8 +65,11 @@ sub find_user { my ($username) = @_; if (!@users_list) { - my $file = 'users.list.plstore'; - @users_list = retrieve($file); + if (!-s users_list_cache) { + fetch_users_list(); + } + + @users_list = retrieve(users_list_cache); } for my $user (@{@users_list[0]}) { |
