Skip to content

Instantly share code, notes, and snippets.

@gonzaleztroyano
Last active August 22, 2023 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gonzaleztroyano/06d75585a7dc3d02519b6b94f4e20c75 to your computer and use it in GitHub Desktop.
Save gonzaleztroyano/06d75585a7dc3d02519b6b94f4e20c75 to your computer and use it in GitHub Desktop.
This script, executed as a cron job, enables the generation of scheduled tasks in HubSpot CRM (even using the free version, on which it is not natively supported). Essentially, the script uses the HubSpot HTTP API to perform task creation requests (engagements).
# This script, executed as a cron job, enables the generation of scheduled tasks in HubSpot CRM (even using the free version, on which it is not natively supported).
#
# All the variables that need to be changed are indicated as "XXXXXXXXXX" in the code below. Essentially, the script uses the HubSpot HTTP API to perform task creation requests (engagements).
# All the API documentation is available here: https://developers.hubspot.com/docs/api/overview
# For in-deep task creation via API documentation, check: https://developers.hubspot.com/docs/api/crm/tasks
#
# The HTTP request is sent using “requests” via POST, combining the details of the tasks and the authorization and content-type headers.
#
# The script has saved into the ‘task’ object different variables for each configured task. For instance, task1 has a title starting with ‘informe’ and an association with another object (a person, in this case). task2 has no association configured.
# To add more tasks, just edit the “task” object, with a name and a object value for that entry following the structure shown as example.
#
# Depending on whether or not it has an association, the payload varies. The script uses the first param when called to decide which task needs to be created.
# If it is the task "task" that has to be created:
# >> hubspot-task-scheduler.py task2 <<
# Or add the following entry on the crontab, changing the schedule:
# >> 0 8 * * THU /usr/bin/python /home/hubspot-api/hubspot-task-scheduler.py task2 > /var/log/hs-scheduler.log <<
import requests, time, sys
url = "https://api.hubapi.com/crm/v3/objects/tasks"
task = {"task1" : {"task_title":"Informe XXXXXXXXXX - ","task_body":"", "association":"XXXXXXXXXX"}, "task2":{"task_title":"Revisar tickets y deals - ","task_body":"", "association":""}}
objetivo = str(sys.argv[1])
fecha_bonita = time.asctime()[4:10] + time.asctime()[19:]
if task.get(objetivo)["association"] == "":
payload = '{"properties": {"hs_timestamp": '+ str(int((time.time()* 1000))) +',"hs_task_body": "'+ task.get(objetivo)["task_body"]+'", "hubspot_owner_id": "XXXXXXXXXX","hs_task_subject": "'+ task.get(objetivo)["task_title"]+ fecha_bonita +'", "hs_task_status": "WAITING"},"associations": []}'
else:
payload = '{"properties": {"hs_timestamp": '+ str(int((time.time()* 1000))) +',"hs_task_body": "'+ task.get(objetivo)["task_body"]+'", "hubspot_owner_id": "XXXXXXXXXX","hs_task_subject": "'+ task.get(objetivo)["task_title"]+ fecha_bonita +'", "hs_task_status": "WAITING"},"associations": [{ "to": {"id": "'+ task.get(objetivo)["association"]+'"},"types": [{"associationCategory": "HUBSPOT_DEFINED","associationTypeId": 10}]}]}'
headers = {"authorization": "Bearer pat-XXXXXXXXXX", "content-type":"application/json"}
res = requests.post(url, data=payload, headers=headers)
print(res.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment