Skip to content

Instantly share code, notes, and snippets.

@reicolina
Last active October 13, 2017 21:58
Show Gist options
  • Save reicolina/c47b9a57bdda2bfa96da65f359841328 to your computer and use it in GitHub Desktop.
Save reicolina/c47b9a57bdda2bfa96da65f359841328 to your computer and use it in GitHub Desktop.
Hootsuite Amplify for Selling Contact Import Script
# ___ ,_, ___
# (o,o) (o,o) ,,,(o,o),,,
# {`"'} {`"'} ';:`-':;'
# -"-"- -"-"- -"-"-
#
# AMPLIFY FOR SELLING: CONTACT IMPORT SCRIPT
# ==========================================
# 1.- Obtain CSV file(s) from your data source.
# 2.- Save the CSV file in the same folder as this script.
# 3.- Replace the HOOTSUITE_MEMBER_ID, AMPLIFY_API_KEY, CRM_MAPPING_URL
# and DATA_SOURCE_NAME constants with the REAL values.
# 4.- Run this script.
# NOTE: The CSV file data *MUST* match have the following field types,
# in the exact same order:
# - user_id - contact's ID from the data source
# - email
# - name - contact's full name
# - phone
# - tw_id - twitter ID if any
# - tw_username - array of twitter username if any
# - website - The person's personal website, or their company's website if any
# - job_title
# - company_name
# - location
#
# EXAMPLE CSV FILE:
# =================
# 0001, [email protected], Bob Smith, 555-555-5551, , @u1, , engineer, company1, vancouver
# 0002, [email protected], Allen Fox, 555-555-5552, , @u2, , product manager, company2, new york
# 0003, [email protected], Maria Jackson, 555-555-5553, , @u3, , designer, company3, bucharest
import csv
import os
import requests
# Define constants
# The ID of the Hootsuite Member ID that will own the Contact.
HOOTSUITE_MEMBER_ID = '123456'
# API Key provided by Hootsuite Amplify
AMPLIFY_API_KEY = 'jshkdjahdkjhskadj-djhskjdhajsd-hjkahjdshkjah'
# Amplify Import Endpoint
CRM_MAPPING_URL = 'https://postman-echo.com/post'
# Data Source name
DATA_SOURCE_NAME = 'my-data-source'
# Define variables.
processed_contacts = 0
# Load CSV File
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for file_name in files:
if file_name.endswith('.csv'):
print 'Loading CSV File: ' + file_name
with open(file_name, 'rb') as csv_file:
csv_reader = csv.reader(csv_file)
# Loop through each contact in the list
for row in csv_reader:
print 'Processing contact data: ' + ', '.join(row)
# Generate request payload
payload = {
"user_id": row[0].strip(),
"emails": [row[1].strip()],
"crm_type": DATA_SOURCE_NAME,
"name": row[2].strip(),
"phones": [row[3].strip()],
"tw_ids": [row[4].strip()],
"tw_usernames": [row[5].strip()],
"data": {
"website": row[6].strip(),
"job_title": row[7].strip(),
"company_name": row[8].strip(),
"location": row[9].strip(),
}
}
headers = {
"x-facade-app-api-key": AMPLIFY_API_KEY,
'content-type': "application/json"
}
# Send request to Amplify's CRM Mapping API
try:
print " Sending contact to Amplify API..."
response = requests.request("POST", CRM_MAPPING_URL,
data=payload, headers=headers)
response.raise_for_status()
print " Reponse Code: " + str(response.status_code)
processed_contacts = processed_contacts + 1
except requests.exceptions.HTTPError as err:
print "Failed!! - " + str(err)
print 'DONE! Processed ' + str(processed_contacts) + " contacts."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment