Skip to content

Instantly share code, notes, and snippets.

@chucknado
Last active May 27, 2021 13:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chucknado/b6a34dfd1a32c9352156 to your computer and use it in GitHub Desktop.
Save chucknado/b6a34dfd1a32c9352156 to your computer and use it in GitHub Desktop.
A Python script for "Zendesk API tutorial: Building a custom ticket form" at https://support.zendesk.com/hc/en-us/articles/209003547
import json
import requests
from bottle import route, template, run, static_file, request, response
@route('/create_ticket', method=['GET', 'POST'])
def handle_form():
if 'verified_email' in request.cookies:
ask_email = False
else:
ask_email = True
status = ''
if request.POST:
# Get the form data
subject = request.forms.get('subject')
description = request.forms.get('description')
if 'verified_email' in request.cookies:
email = request.get_cookie('verified_email')
else:
email = request.forms.get('email')
# Package the data for the API
data = {'request': {'subject': subject, 'comment': {'body': description}}}
ticket = json.dumps(data)
# Make the API request
user = email + '/token'
api_token = 'your_api_token'
url = 'https://your_subdomain.zendesk.com/api/v2/requests.json'
headers = {'content-type': 'application/json'}
r = requests.post(url, data=ticket, auth=(user, api_token), headers=headers)
if r.status_code != 201:
if r.status_code == 401 or 422:
status = 'Could not authenticate you. Check your email address or register.'
ask_email = True
else:
status = 'Problem with the request. Status ' + str(r.status_code)
else:
status = 'Ticket was created. Look for an email notification.'
if 'verified_email' not in request.cookies:
response.set_cookie('verified_email', email, max_age=364*24*3600)
ask_email = False
return template('ticket_form', feedback=status, no_email=ask_email)
@route('/css/<filename>')
def send_css(filename):
return static_file(filename, root='static/css')
run(host='localhost', port=8080, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment