Skip to content

Instantly share code, notes, and snippets.

@ninajlu
Created March 28, 2019 13:33
Show Gist options
  • Save ninajlu/d0b939ee34257fe2b21ae935321895d3 to your computer and use it in GitHub Desktop.
Save ninajlu/d0b939ee34257fe2b21ae935321895d3 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import json
import boto3
import time
import urllib
import csv
from botocore.vendored import requests
# Get the service resource
s3 = boto3.client('s3')
tests3 = boto3.resource(u's3')
# insert_data function for creating a Zendesk ticket from the data
def insert_data(recList):
for i in range(len(recList)):
record = recList[i]
tags = list()
#Optionally add any tags you would like to add to the ticket
tags.append('[TAG]')
#Set the Body of your Ticket
html_body = """[BODY OF TICKET IN HTML]"""
#Set the Subject / Title of your Ticket
subject = """[SUBJECT OF TICKET]"""
#is_public determines whether or not the comment will be internal to your organization or sent to a user.
is_public = False
#set the Zendesk ID of the requester (customer's Zendesk ID)
requester_id = "ID OF REQUESTER"
#set the Zendesk ID of the submitter of the ticket (yours or a Zendesk Bot's id)
submitter_id = "ID OF SUBMITTER"
data = {'ticket': {'subject': subject, 'tags': tags, 'is_public': is_public, 'comment': {'public': is_public, 'html_body': html_body}, 'requester_id': requester_id , 'submitter_id': submitter_id }}
# Create a JSON payload
payload = json.dumps(data)
# Set the request parameters
url = 'https://[SUBDOMAIN].zendesk.com/api/v2/tickets.json'
user = '[EMAIL]'
pwd = '[PASSWORD]'
headers = {'content-type': 'application/json'}
# Do the HTTP post request
response = requests.post(url, data=payload, auth=(user, pwd), headers=headers)
# Check for HTTP codes other than 201 (Created)
if response.status_code != 201:
print('Status:', response.status_code, 'Problem with the request. Exiting.')
exit()
# Report success
print('Successfully created the ticket.')
# lambda_handler is the main function in lambda function
def lambda_handler(event,context):
source_bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
copy_source = {'Bucket':source_bucket , 'Key':key}
print(event)
try:
bucket = tests3.Bucket(source_bucket)
obj = bucket.Object(key=key)
response = obj.get()
print("response from file object")
print(response)
lines = response['Body'].read().decode('utf-8').splitlines(True)
recList = list()
# creating a list of dictionaries from the csv file records
for row in csv.DictReader(lines):
recList.append(row)
insert_data(recList)
s3.delete_object(Bucket=source_bucket, Key=key)
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, source_bucket))
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment