Listing the followers of a KB section with the Zendesk API Follow

Comments

11 comments

  • Avatar
    Valeri

    Really helpful article. I've written the code with Python, but what I need to do next to integrate my API? Thank you!

  • Avatar
    Mike Taylor

    Charles, your excellent article helped me finally create a list of followers to a section of articles. (I cannot believe zendesk doesn't offer this as basic functionality 101, listing section followers, but it is what it is.) 


    I am not a coder but was able to follow your instructions above and get the python file to work. It has done so for several weeks, then today it has run but listed only 3 names, where I expected 20+. 


    I haven't changed anything about the .py file since authoring it, so wonder what could cause this reduction/error. (It is not 20 people unfollowing as I have confirmation they received a recent article posting).


    Thanks for your help.


    Michael

  • Avatar
    Charles Nadeau

    Hi Mike,


    Hard to tell what's going on. I tested the script again to check for any change in the JSON attribute names since I wrote it, but everything still seems to be working for me.


    Is there something in common with the users who are actually listed? For example, are they all agents?

  • Avatar
    Mike Taylor

    Charles,


    The three people being listed are customers who follow a section we have called AO Enhancements (AO is our software). The full list of 20+ that was being reported did include three agents.


    I do need to get this resolved quickly, as we are adding new customers and I need to be able to track software enhancement followers. Should I submit a help request, or can I send you my .py file to run and diagnose?


    Thanks for your support.


    Michael

  • Avatar
    Charles Nadeau

    Hi Mike,


    Can you send me your Python file with the credentials removed? Also, can you send me the Help Center settings for the section in question? Click the section name, then click Edit. I'm particularly interested in the Who Can View? setting.


    Thanks.


     

  • Avatar
    Charles Nadeau

    Hi Mike,


    I think I found the problem. The last 2 lines of your script to print the results are:


    for user in data['users']:
    
    print(user['name'])

    The first line of the snippet should be:


    for user in users:
    
    print(user['name'])

    The users variable is where you cumulatively save the user records if the API returns multiple pages of results.


    The data['users'] variable is different. It contains only the page of results returned by the API. For the subscribers API, each page consists of a maximum of 30 records. If more than 30 people subscribe to a section, then the API returns a second page of results with only the additional people. The contents of data['users'] consists of only those records. And so on when you reach 60 +1 subscribers.


    Your script worked when you had less than 30 subscribers. As soon as you had over 30 subscribers, the value of data['users'] at the end of the script only contained the second page of results -- I think 3 users in in your case. However, all 33 users were stored in the users variable.


    Hope this makes sense.


    Charles


     

  • Avatar
    Mike Taylor

    Charles, sorry for the delay in responding, vacation. Your adjustment to the code worked perfectly. Thank You. Up and running again. 

  • Avatar
    Alex Gillum-Webb

    Hi Charles,

    This is really useful, have been playing around with this today. One thing I was hoping someone could help with, it appears I can only return 'ids' and 'name' when side-loading against user data.

    If I try with 'email' I get:

    KeyError: 'email'

    Are only certain attributes of user data available when side-loading? Thanks for sharing this either way!

     

  • Avatar
    Bryan Flynn

    That's right Alex -- you only get that limited information (user ID, name, and maybe a photo). If you need more user info, you'll have to take the user ID that's returned and call the GET /api/v2/users/{id}.json REST API endpoint.

  • Avatar
    Alex Gillum-Webb

    Hi Brian,

    Thanks for the swift response. I think i'm nearly there - for each user now, under name, I can return the full array of user data - i'm just looking to return the users email address however.

    for user in users:

            print(user['name'])

            newurl = 'https://your_subdomain.zendesk.com/api/v2/users/' + str(user['id'] + '.json'

            response = session.get(newurl)

            data = response.json()

            print (data)

    I'm getting key errors if I try and display anything else in the final print statement. Any ideas? Pretty new to both Python and the Zendesk API - sorry!

     

  • Avatar
    Bryan Flynn

    Hi Alex,

    print(['user']['email']) on that final line should work, assuming the request was successful.

    if response.status_code != 200:
        print('Status:', response.status_code, 'Problem with the request. Exiting.')
    else:
        print('Status:', response.status_code, 'Success!')
        data = response.json()
        print(data['user']['email'])

    This article isn't specific to your use-case but does show how to do a number of things using Python and might be a good reference: Getting large data sets with the Zendesk API and Python

    Also, I'd post subsequent REST API questions in the developer forum, which focuses on this area. You'll get a wider audience versus whoever is just following this article:

    https://develop.zendesk.com/hc/en-us/community/topics/360000019807-REST-APIs

     

Please sign in to leave a comment.

Powered by Zendesk