Last active
August 29, 2015 14:24
-
-
Save chucknado/e2c472ac7efc23fe9f26 to your computer and use it in GitHub Desktop.
A Python script for the article "Importing users with the Zendesk API" at https://support.zendesk.com/hc/en-us/articles/206155718
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import xlrd | |
import requests | |
session = requests.Session() | |
session.headers = {'Content-Type': 'application/json'} | |
session.auth = 'your_zd_email', 'your_zd_password' | |
url = 'https://your_subdomain.zendesk.com/api/v2/users/create_many.json' | |
payloads = [] | |
users_dict = {'users': []} | |
book = xlrd.open_workbook('users_list.xlsx') | |
sheet = book.sheet_by_name('Sheet1') | |
for row in range(1, sheet.nrows): | |
if sheet.row_values(row)[2]: | |
users_dict['users'].append( | |
{ | |
'name': sheet.row_values(row)[2], | |
'email': sheet.row_values(row)[3], | |
'user_fields': {'member_level': sheet.row_values(row)[7]} | |
} | |
) | |
if len(users_dict['users']) == 100: | |
payloads.append(json.dumps(users_dict)) | |
users_dict = {'users': []} | |
if users_dict['users']: | |
payloads.append(json.dumps(users_dict)) | |
for payload in payloads: | |
response = session.post(url, data=payload) | |
if response.status_code != 200: | |
print('Import failed with status {}'.format(response.status_code)) | |
exit() | |
print('Successfully imported a batch of users') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment