Insipration

Block Heap

Web Developer's Blog

Create a custom python post action in Zapier

1) Create or open an existing “Zap”

2) Add a new “Step” to your Zap

3) Add an “Action” to your Step from the sidebar menu

4) Specify “Code” as your action from the list of possible built-in apps

5) Set Python as the programming language

6) Designate input data fields*

As you can see, some of our fields are static and some are pulled in from Facebook

*Your fields may vary. These are specific to the campaign we were running. We also had to define these fields with the Slate Dashboard under Web Services and Hooks.

We like tested using a RequestBin on Pipedream to ensure data is sent successfully and to examine the raw data and transmission details. You may want to match up the XML schema from Slate to make sure everything aligns properly.

Setting up a test endpoint with Pipedream:

i. Create endpoint

ii. Copy endpoint for use as one of the data fields in step 6 (for testing only)

7. Insert code

The default code area looks like this:

We proceed by inserting our own code, which you can download here.


from requests.auth import HTTPBasicAuth
import json

endpoint = input_data.get('endpoint', '')

person = [{
            'vendor': input_data.get('vendor', ''),
            'campaign': input_data.get('campaign', ''),
            'academic_interest': input_data.get('academic_interest', ''),
            'firstname': input_data.get('firstname', ''),
            'lastname': input_data.get('lastname', ''),
            'phone': input_data.get('phone', ''),
            'salesstate': input_data.get('state', ''),
            'emailaddress': input_data.get('emailaddress', '')
        }]

payload = {'person': person}

try:
    r = requests.post(endpoint, data=json.dumps(payload),
                      auth=HTTPBasicAuth('myusername',
                                         r'mypassword'))
except requests.exceptions.Timeout:
    return {'success': False, 'error': 'Timeout.'}
except requests.exceptions.TooManyRedirects:
    return {'success': False, 'error': 'Endpoint error.'}
except requests.exceptions.RequestException as e:
    return {'success': False, 'error': 'catastrophic error.'}
finally:
    return {
        'success': True,
        'person': person
    }

return []

Additional Notes:

You’ll need to adjust the person object in the Python code to reflect your own input values. Zapier makes an object available named input_data that contains the data from step 6.

Our example uses HTTP Basic Authentication but you may need to strip out those lines for your situation. It made sense for us to hard code the username and password but you may want to look into how Zapier handles and stores this data, as it’s generally seen as a bad security practice. Zapier CLI looks promising for setting environment variables from the terminal or something similar.

You may also want to adjust the error handling portion to fit your needs.

Leave a Reply

Your email address will not be published.

SIDEBAR HERE