Skip to content

Instantly share code, notes, and snippets.

@chrisoklepke
Created October 17, 2023 15:01
Show Gist options
  • Save chrisoklepke/ebc560f776338d06c6efccd3598d6907 to your computer and use it in GitHub Desktop.
Save chrisoklepke/ebc560f776338d06c6efccd3598d6907 to your computer and use it in GitHub Desktop.
import os
import time
from hubspot import HubSpot
from hubspot.crm.products import (
PublicObjectSearchRequest,
ApiException as productApiException,
)
from hubspot.crm.line_items import (
BatchInputSimplePublicObjectInput,
ApiException as lineItemApiException,
)
from dotenv import load_dotenv # Delete Line or Comment when Implementing
load_dotenv() # Delete Line or Comment when Implementing
def main(event):
access_token = os.getenv("WFTOKEN")
hubspot = HubSpot(access_token=access_token)
deal_id = event.get("object").get("objectId")
modell = event.get("inputFields").get("modell")
bafa_antrag = event.get("inputFields").get("bafa_antrag")
zulassungsservice = event.get("inputFields").get("zulassungsservice")
bafa_antrag_product_id = "761123043"
zulassungsservice_product_id = "761155781"
current_time = int(time.time()) * 1000
if modell == "ELARIS BEO":
car_color = event.get("inputFields").get("farbe_beo")
transfer_product_id = "727213290"
discount_product_id = "863063778"
discount_price = 2500
elif modell == "ELARIS FINN":
car_color = event.get("inputFields").get("farbe_finn")
transfer_product_id = "727202257"
discount_product_id = "863065574"
discount_price = 3000
elif modell == "ELARIS PIO":
car_color = event.get("inputFields").get("farbe_pio")
transfer_product_id = "701178863"
discount_product_id = "863055318"
discount_price = 3000
else:
print("Error: Modell nicht korrekt gesetzt")
# Get Product ID
public_object_search_request = PublicObjectSearchRequest(
filter_groups=[
{
"filters": [
{"value": modell, "propertyName": "modell", "operator": "EQ"},
{"value": car_color, "propertyName": "farbe", "operator": "EQ"},
{
"value": current_time,
"propertyName": "valid_from",
"operator": "LTE",
},
]
}
],
sorts=[{"propertyName": "valid_from", "direction": "DESCENDING"}],
properties=[
"name",
"modell",
"innenausstattung",
"farbe",
"holzapplikation",
"sitzfarbe",
"hs_sku",
"valid_from",
],
limit=10,
)
try:
get_product = hubspot.crm.products.search_api.do_search(
public_object_search_request=public_object_search_request
)
print(get_product)
except productApiException as exception:
print(exception)
vehicle_product_id = get_product.results[0].id
print(vehicle_product_id)
line_item_properties = [
{
"properties": {
"hs_product_id": vehicle_product_id,
"quantity": "1",
}
},
{
"properties": {
"hs_product_id": transfer_product_id,
"quantity": "1",
}
},
{
"properties": {
"hs_product_id": discount_product_id,
"quantity": "1",
"discount": discount_price,
}
},
]
if bafa_antrag == "true":
line_item_properties.append(
{
"properties": {
"hs_product_id": bafa_antrag_product_id,
"quantity": "1",
}
}
)
if zulassungsservice == "true":
line_item_properties.append(
{
"properties": {
"hs_product_id": zulassungsservice_product_id,
"quantity": "1",
}
}
)
line_items = BatchInputSimplePublicObjectInput(inputs=line_item_properties)
try:
create_line_items = hubspot.crm.line_items.batch_api.create(
batch_input_simple_public_object_input=line_items
)
new_line_items = create_line_items.results
except lineItemApiException as exception:
print(exception)
for item in new_line_items:
try:
assoc_line_item_to_deal = hubspot.crm.line_items.associations_api.create(
line_item_id=item.id,
to_object_type="deal",
to_object_id=deal_id,
association_type="20",
)
# print(assoc_line_item_to_deal)
except lineItemApiException as exception:
print(exception)
return
# Event Payload
payload = {
"origin": {
# Your portal ID
"portalId": 1,
# Your custom action definition ID
"actionDefinitionId": 2,
},
"object": {
# The type of CRM object that is enrolled in the workflow
"objectType": "DEAL",
# The ID of the CRM object that is enrolled in the workflow
"objectId": 5067583162,
},
"inputFields": {
# The property name for defined inputs
"modell": "ELARIS BEO",
"farbe_beo": "Jet Black",
"farbe_finn": "",
"farbe_pio": "",
"bafa_antrag": True,
"zulassungsservice": True,
},
# A unique ID for this execution
"callbackId": "ap-123-456-7-8",
}
main(payload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment