Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertainslie/85ed2e058d658a049caf65ce381a21ae to your computer and use it in GitHub Desktop.
Save robertainslie/85ed2e058d658a049caf65ce381a21ae to your computer and use it in GitHub Desktop.
import os
import requests
import hubspot
from pprint import pprint
from hubspot.crm.objects import PublicObjectSearchRequest, ApiException
import json
def main(event):
headers = {'accept': 'application/json', 'content-type': "application/json"}
HAPI = os.getenv("HAPIKEY")
querystring = {"hapikey":HAPI}
client = hubspot.Client.create(api_key=os.getenv("HAPIKEY"))
#input parameters from the Contact record, that wehave passed on to the step
contact_id = event.get('inputFields').get('hs_object_id')
state = event.get('inputFields').get('state')
industry = event.get('inputFields').get('industry').lower()
budget = event.get('inputFields').get('franchise_budget')
formatted_budget = event.get('inputFields').get('formatted_budget')
print("Formatted budget is: " + formatted_budget)
maxBudget = 0
minBudget = 0
shd_asscoiate = "no"
minBudget, maxBudget = calculatebudget(formatted_budget)
zipcode = str(event.get('inputFields').get('zip'))
print("Industry: {}".format(industry))
print("Min: {}".format(minBudget))
print("Max: {}".format(maxBudget))
print("zipcode:")
print(zipcode)
#Search Franchise locations with filters on industry and initiation fee
public_object_search_request = PublicObjectSearchRequest(filter_groups=[{"filters":[{"value":industry,"propertyName":"industry","operator":"EQ"},{"value":minBudget,"highValue":maxBudget,"propertyName":"initiation_fee","operator":"BETWEEN"}]}], properties=["industry","franchise_id","franchise_name"])
#print(public_object_search_request)
try:
api_response = client.crm.objects.search_api.do_search(object_type="franchises", public_object_search_request=public_object_search_request)
print(api_response)
#loop through the franchise locations, and check if zipcode is available or not
for result in api_response.results:
fname = result.properties["franchise_name"]
fid = result.id
#If the user has not entered a zipcode, associate the franchises.
#print(zipcode)
if zipcode == "None":
assocurl = "https://api.hubapi.com/crm/v4/objects/0-1/"+contact_id+"/associations/2-6703516/"+fid
payload = "[{\"associationCategory\":\"USER_DEFINED\",\"associationTypeId\":36}]"
assoc_response = requests.request("PUT", assocurl, data=payload, headers=headers, params=querystring)
else:
#search franchise locations for a match
#List associated locations for the each franchise, and associate. We are sing search API
public_object_search_requesta = PublicObjectSearchRequest(filter_groups=[{"filters":[{"value":zipcode,"propertyName":"zip","operator":"EQ"},{"value":fname,"propertyName":"franchise_name","operator":"EQ"}]}],properties=["franchise_name","franchise_location_id","zip"])
#print(public_object_search_requesta)
try:
locations_api_response = client.crm.objects.search_api.do_search(object_type="2-6703647",public_object_search_request=public_object_search_requesta)
if locations_api_response.total >= 1:
print("Associating with zip search")
print(locations_api_response.total)
assocurl = "https://api.hubapi.com/crm/v4/objects/0-1/"+contact_id+"/associations/2-6703516/"+fid
payload = "[{\"associationCategory\":\"USER_DEFINED\",\"associationTypeId\":36}]"
assoc_response = requests.request("PUT", assocurl, data=payload, headers=headers, params=querystring)
except ApiException as e:
print(e)
except ApiException as e:
print("Exception when calling search or assoc api" % e)
def calculatebudget(f_budget):
if f_budget == "100000":
minBudget = int("0")
maxBudget = int(f_budget)
return minBudget, maxBudget
else:
print("budget: "+ f_budget)
#minBudget: int(f_budget.split('-')[0].replace(",", ""))
#maxBudget: int(f_budget.split('-')[1].replace(",", ""))
minBudget = int(f_budget.split('-')[0])
maxBudget = int(f_budget.split('-')[1])
return minBudget, maxBudget
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment