Skip to content

Instantly share code, notes, and snippets.

@chrisoklepke
Created March 8, 2022 13:04
Show Gist options
  • Save chrisoklepke/d92a71af7dbce624dc2db4979c4b35dd to your computer and use it in GitHub Desktop.
Save chrisoklepke/d92a71af7dbce624dc2db4979c4b35dd to your computer and use it in GitHub Desktop.
import os
import requests
import json
from random import randint
ticketCount = 0
def main(event):
# Create a ticket ID for your event
global ticketCount
ticketCount += 1
firstName = event.get("inputFields").get("firstname") # set this as an Input Field
lastName = event.get("inputFields").get("lastname") # set this as an Input Field
random = randint(1000, 9999)
ticketNo = str(ticketCount).zfill(4)
ticketId = f"{firstName}-{lastName}-{random}-{ticketNo}"
# print(ticketId) # for debug
# Create QR code
qrUrl = "https://api.qr-code-generator.com/v1/create?access-token=" + os.getenv("QRAPIKEY")
qrCodePayload = {
"frame_name": "no-frame",
"qr_code_text": ticketId, # You can also link directly to your validation URL with the ticket ID as parameter
"image_format": "PNG",
"qr_code_logo": "no-logo",
"image_width": 300
}
qrCodeHeaders = {
"Content-Type": "application/json"
}
createQrcode = requests.post(qrUrl, headers=qrCodeHeaders, json=qrCodePayload)
# print(createQrcode.content) # for debug
# Send QR code image to HubSpot files
fileApiUrl = "https://api.hubapi.com/files/v3/files?hapikey=" + os.getenv("HAPIKEY")
filename = ticketId + "." + qrCodePayload["image_format"]
fileOptions = {
"access": "PUBLIC_NOT_INDEXABLE",
}
filesData = {
"file": createQrcode.content,
"fileName": filename,
"folderPath": '/tickets', # make sure to create a folder first
"options": (None, json.dumps(fileOptions), "text/strings"),
}
uploadFile = requests.post(fileApiUrl, files=filesData)
# print(uploadFile.json()["url"]) # for debug
return {
"outputFields": {
"ticket_qr_code": uploadFile.json()["url"], # Safe the URL to your output field
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment