Skip to content

Instantly share code, notes, and snippets.

@chucknado
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chucknado/283b4b992d6d2adb36e3 to your computer and use it in GitHub Desktop.
Save chucknado/283b4b992d6d2adb36e3 to your computer and use it in GitHub Desktop.
A Perl script for "Zendesk API tutorial: Listing the followers of a KB section" at https://support.zendesk.com/hc/en-us/articles/206340488
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use MIME::Base64;
my $num_args = $#ARGV + 1;
if ($num_args != 1) {
print "Usage error: list_followers.pl hc_id\n";
exit;
}
my $zendesk = 'https://your_subdomain.zendesk.com';
my $endpoint = "/api/v2/help_center/sections/$ARGV[0]/subscriptions.json?include=users";
my $url = $zendesk . $endpoint;
my $credentials = encode_base64('your_zd_email/token:your_api_token');
my $ua = LWP::UserAgent->new(ssl_opts =>{ verify_hostname => 0 });
my @users;
while ($url) {
my $response = $ua->get($url, 'Authorization' => "Basic $credentials");
die 'http status: ' . $response->code . ' ' . $response->message
unless ($response->is_success);
my $data = decode_json($response->content);
push @users, @{ $data->{'users'} };
if (defined $data->{'next_page'}) {
$url = $data->{'next_page'};
} else {
$url = '';
}
}
foreach my $user ( @users ) {
print $user->{"name"} . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment