Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Openpath outbound webhooks for Camio
#python 3
try:
import base64
except ImportError:
print ("Error, Module base64 is required")
try:
import requests
except ImportError:
print ("Error, Module requests is required")
#get variables
orgid = input ("OrgID:")
username = input ("Username:")
password = input ("Password:")
#get encoded data
authraw = username + ":" + password
authencode = base64.b64encode(authraw.encode("utf-8"))
authencodestr = str(authencode, "utf-8")
authfinal = "Basic <" + authencodestr + ">"
#configs for json
camiobodyunlockconfig = '{ "name": "Send Unlock Event to Camio", "description": "Post to camio when door unlocks", "trigger": { "required": [ "event" ], "properties": { "event": { "enum": [ "entry.unlocked" ] } } }, "variables": [ { "name": "base_url", "value": "https://camio.com/openpath/webhooks" } ], "curlOpts": { "followLocation": false, "maxRedirs": 10, "autoReferer": true, "ssl_verifyPeer": 0, "connectTimeout": 10, "timeout": 10, "httpHeader": [ "Content-type: application/json" ] }, "requests": [ { "post": 1, "postFields": "{{event}}", "url": "{{base_url}}" } ]}'
camiobodyajarstartconfig = '{ "name": "Send Ajar Start Event to Camio", "description": "Post to camio When Ajar begins", "trigger": { "required": [ "event" ], "properties": { "event": { "enum": [ "entry.ajar.started" ] } } }, "variables": [ { "name": "base_url", "value": "https://camio.com/openpath/webhooks" } ], "curlOpts": { "followLocation": false, "maxRedirs": 10, "autoReferer": true, "ssl_verifyPeer": 0, "connectTimeout": 10, "timeout": 10, "httpHeader": [ "Content-type: application/json" ] }, "requests": [ { "curlOpts": { "httpHeader": [ "Content-Type:application/json" ], "post": 1, "postFields": "{{event}}", "url": "{{base_url}}" } } ]}'
camiobodyajarendconfig = '{ "name": "Send Ajar End Event to Camio", "description": "Post to camio When Ajar ends", "trigger": { "required": [ "event" ], "properties": { "event": { "enum": [ "entry.ajar.ended" ] } } }, "variables": [ { "name": "base_url", "value": "https://camio.com/openpath/webhooks" } ], "curlOpts": { "followLocation": false, "maxRedirs": 10, "autoReferer": true, "ssl_verifyPeer": 0, "connectTimeout": 10, "timeout": 10, "httpHeader": [ "Content-type: application/json" ] }, "requests": [ { "curlOpts": { "httpHeader": [ "Content-Type:application/json" ], "post": 1, "postFields": "{{event}}", "url": "{{base_url}}" } } ]}'
camiobodyforcedopenconfig = '{ "name": "Send Forced Open Event to Camio", "description": "Post to camio when forced open", "trigger": { "required": [ "event" ], "properties": { "event": { "enum": [ "entry.forcedOpen" ] } } }, "variables": [ { "name": "base_url", "value": "https://camio.com/openpath/webhooks" } ], "curlOpts": { "followLocation": false, "maxRedirs": 10, "autoReferer": true, "ssl_verifyPeer": 0, "connectTimeout": 10, "timeout": 10, "httpHeader": [ "Content-type: application/json" ] }, "requests": [ { "curlOpts": { "httpHeader": [ "Content-Type:application/json" ], "post": 1, "postFields": "{{event}}", "url": "{{base_url}}" } } ]}'
camiobodylockdownconfig = '{ "name": "Send Lockdown Event to Camio", "description": "Post to camio when Lockdown", "trigger": { "required": [ "event" ], "properties": { "event": { "enum": [ "lockdownPlan.triggered" ] } } }, "variables": [ { "name": "base_url", "value": "https://camio.com/openpath/webhooks" } ], "curlOpts": { "followLocation": false, "maxRedirs": 10, "autoReferer": true, "ssl_verifyPeer": 0, "connectTimeout": 10, "timeout": 10, "httpHeader": [ "Content-type: application/json" ] }, "requests": [ { "curlOpts": { "post": 1, "postFields": "{{event}}", "url": "{{base_url}}" } } ]}'
camiobodylockdownrevertconfig = '{ "name": "Send Lockdown Revert Event to Camio", "description": "Post to camio when lockdown reverts", "trigger": { "required": [ "event" ], "properties": { "event": { "enum": [ "lockdownPlan.reverted" ] } } }, "variables": [ { "name": "base_url", "value": "https://camio.com/openpath/webhooks" } ], "curlOpts": { "followLocation": false, "maxRedirs": 10, "autoReferer": true, "ssl_verifyPeer": 0, "connectTimeout": 10, "timeout": 10, "httpHeader": [ "Content-type: application/json" ] }, "requests": [ { "curlOpts": { "post": 1, "postFields": "{{event}}", "url": "{{base_url}}" } } ]}'
#check credentials
r=requests.get(f"https://api.openpath.com/orgs/{orgid}/entryStates?offset=0&sort=ordinal&order=asc", headers={"Authorization":authfinal})
if r:
print("Working...")
else:
print("Failed, please check credentials")
exit()
#send the webhooks
#print(camiobodyunlock)
requests.post(f"https://api.openpath.com/orgs/{orgid}/hookActions", headers={"Authorization":authfinal}, data={'name':'Send Unlock Event to Camio', 'description':'Post to camio when door unlocks', 'config':f'{camiobodyunlockconfig}'})
requests.post(f"https://api.openpath.com/orgs/{orgid}/hookActions", headers={"Authorization":authfinal}, data={'name':'Send Ajar Start to Camio', 'description':'Post to camio when Ajar Starts', 'config':f'{camiobodyajarstartconfig}'})
requests.post(f"https://api.openpath.com/orgs/{orgid}/hookActions", headers={"Authorization":authfinal}, data={'name':'Send Ajar Ends to Camio', 'description':'Post to camio when Ajar Ends', 'config':f'{camiobodyajarendconfig}'})
requests.post(f"https://api.openpath.com/orgs/{orgid}/hookActions", headers={"Authorization":authfinal}, data={'name':'Send Forced Open Event to Camio', 'description':'Post to camio when door forced open', 'config':f'{camiobodyforcedopenconfig}'})
requests.post(f"https://api.openpath.com/orgs/{orgid}/hookActions", headers={"Authorization":authfinal}, data={'name':'Send Lockdown Event to Camio', 'description':'Post to camio when lockdown occurs', 'config':f'{camiobodylockdownconfig}'})
requests.post(f"https://api.openpath.com/orgs/{orgid}/hookActions", headers={"Authorization":authfinal}, data={'name':'Send Lockdown Revert Event to Camio', 'description':'Post to camio when lockdown reverts', 'config':f'{camiobodylockdownrevertconfig}'})
#get the webhook ids for creating the subscriptions
camiounlockhookaction = requests.get(f"https://api.openpath.com/orgs/{orgid}/hookActions?offset=0&sort=name&order=asc&q=Send%20Unlock%20Event%20to%20Camio", headers={"Authorization":authfinal})
camioajarstarthookaction = requests.get(f"https://api.openpath.com/orgs/{orgid}/hookActions?offset=0&sort=name&order=asc&q=Send%20Ajar%20Start%20to%20Camio", headers={"Authorization":authfinal})
camioajarendhookaction = requests.get(f"https://api.openpath.com/orgs/{orgid}/hookActions?offset=0&sort=name&order=asc&q=Send%20Ajar%20Ends%20to%20Camio", headers={"Authorization":authfinal})
camioforcedopenhookaction = requests.get(f"https://api.openpath.com/orgs/{orgid}/hookActions?offset=0&sort=name&order=asc&q=Send%20Forced%20Open%20Event%20to%20Camio", headers={"Authorization":authfinal})
camiolockdownhookaction = requests.get(f"https://api.openpath.com/orgs/{orgid}/hookActions?offset=0&sort=name&order=asc&q=Send%20Lockdown%20Event%20to%20Camio", headers={"Authorization":authfinal})
camiolockdownreverthookaction = requests.get(f"https://api.openpath.com/orgs/{orgid}/hookActions?offset=0&sort=name&order=asc&q=Send%20Lockdown%20Revert%20Event%20to%20Camio", headers={"Authorization":authfinal})
unlockjson = (camiounlockhookaction.json())
unlockid = (unlockjson['data'][0]['id'])
ajarstartjson = (camioajarstarthookaction.json())
ajarstartid = (ajarstartjson['data'][0]['id'])
ajarendjson = (camioajarendhookaction.json())
ajarendid = (ajarendjson['data'][0]['id'])
forcedopenjson = (camioforcedopenhookaction.json())
forcedopenid = (forcedopenjson['data'][0]['id'])
lockdownjson = (camiolockdownhookaction.json())
lockdownid = (lockdownjson['data'][0]['id'])
lockdownrevertjson = (camiolockdownreverthookaction.json())
lockdownrevertid = (lockdownrevertjson['data'][0]['id'])
#check if subscriptions exist
allsubscriptions = requests.get(f"https://api.openpath.com/orgs/{orgid}/subscriptions?offset=0&sort=id&order=asc", headers={"Authorization":authfinal})
allsubsjsonraw = (allsubscriptions.text)
allsubsjson = (allsubsjsonraw)
#check if subscription is there, if not send the subscriptions
if allsubsjson.find("Send Unlock Event to Camio") > -1:
print("Unlock Subscription already exists")
else:
requests.post(f"https://api.openpath.com/orgs/{orgid}/subscriptions", headers={"Authorization":authfinal}, data={'hookEventId':6, 'hookActionId':f'{unlockid}', 'isBlocking':'false'})
if allsubsjson.find("Send Ajar Start to Camio") > -1:
print("Ajar Start Subscription already exists")
else:
requests.post(f"https://api.openpath.com/orgs/{orgid}/subscriptions", headers={"Authorization":authfinal}, data={'hookEventId':10, 'hookActionId':f'{ajarstartid}', 'isBlocking':'false'})
if allsubsjson.find("Send Ajar Ends to Camio") > -1:
print("Ajar End Subscription already exists")
else:
requests.post(f"https://api.openpath.com/orgs/{orgid}/subscriptions", headers={"Authorization":authfinal}, data={'hookEventId':11, 'hookActionId':f'{ajarendid}', 'isBlocking':'false'})
if allsubsjson.find("Send Forced Open Event to Camio") > -1:
print("Forced Open Subscription already exists")
else:
requests.post(f"https://api.openpath.com/orgs/{orgid}/subscriptions", headers={"Authorization":authfinal}, data={'hookEventId':17, 'hookActionId':f'{forcedopenid}', 'isBlocking':'false'})
if allsubsjson.find("Send Lockdown Event to Camio") > -1:
print("Lockdown Subscription already exists")
else:
requests.post(f"https://api.openpath.com/orgs/{orgid}/subscriptions", headers={"Authorization":authfinal}, data={'hookEventId':19, 'hookActionId':f'{lockdownid}', 'isBlocking':'false'})
if allsubsjson.find("Send Lockdown Revert Event to Camio") > -1:
print("Lockdown Revert Subscription already exists")
else:
requests.post(f"https://api.openpath.com/orgs/{orgid}/subscriptions", headers={"Authorization":authfinal}, data={'hookEventId':20, 'hookActionId':f'{lockdownrevertid}', 'isBlocking':'false'})
print("Finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.