|
| 1 | +# This script is an example of using the Parsons library to import data from |
| 2 | +# Twilio using the Parsons Twilio connector, and then upload the resulting |
| 3 | +# table to Platform using the Parsons Civis connector. |
| 4 | +# Parsons library source: https://github.com/move-coop/parsons |
| 5 | +# Below are instructions to be able to customize and run this file in Platform. |
| 6 | + |
| 7 | +# Step 1: Copy this file to a Github repository that is connected to Platform. |
| 8 | +# For docs on how to connect Platform to Github, follow the steps in |
| 9 | +# the first two paragraphs under "Connecting Platform to Github/Bitbucket": |
| 10 | +# https://civis.zendesk.com/hc/en-us/articles/115003734992-Version-Control |
| 11 | + |
| 12 | +# Step 2: Set CIVIS_DATABASE and CIVIS_TABLE global variables below |
| 13 | +# List of databases: platform.civisanalytics.com/spa/remote_hosts |
| 14 | +# CIVIS_TABLE should be in "schema.table" form |
| 15 | + |
| 16 | +CIVIS_DATABASE = 99 |
| 17 | +CIVIS_TABLE = "twilio_test.account_usage" |
| 18 | + |
| 19 | +# Step 3: Create a platform custom credential with the following fields: |
| 20 | +# Name: Twilio Account |
| 21 | +# Credential Type: Custom |
| 22 | +# Username: <Twilio Account SID> |
| 23 | +# Password: <Twilio Auth Token> |
| 24 | +# |
| 25 | +# Docs on creating platform credentials: platform.civisanalytics.com/spa/credentials/new |
| 26 | +# Twilio Account SID & Auth Token can be found at www.twilio.com/console |
| 27 | + |
| 28 | +# Step 4: Create a Platform container script (Code -> Container) with the following fields: |
| 29 | +# GITHUB REPOSITORY URL: <Github repository from Step 1> |
| 30 | +# GITHUB REPOSITORY REFERENCE: <Github Branch that contains the copy of this file> |
| 31 | +# COMMAND: pip install pandas |
| 32 | +# python /app/examples/parsons_civis_twilio_example.py |
| 33 | +# DOCKER IMAGE NAME: movementcooperative/parsons |
| 34 | +# DOCKER IMAGE TAG: v0.16.0 |
| 35 | +# |
| 36 | +# Container Scripts docs: |
| 37 | +# https://civis.zendesk.com/hc/en-us/articles/218200643-Container-Scripts |
| 38 | + |
| 39 | +# Step 5: Add a parameter to the container (Set Parameters) with the following fields: |
| 40 | +# TYPE: Custom Credential |
| 41 | +# REQUIRED: Yes |
| 42 | +# PARAMETER NAME: TWILIO_ACCOUNT |
| 43 | +# INPUT DISPLAY NAME (OPTIONAL): TWILIO ACCOUNT CREDENTIAL |
| 44 | + |
| 45 | +# Step 6: Set the TWILIO ACCOUNT CREDENTIAL parameter from Step 5 to be the |
| 46 | +# credential created in Step 3 |
| 47 | + |
| 48 | +# Step 7: Run the container script! |
| 49 | + |
| 50 | +# For complete documentation on Parsons Twilio & Civis connectors, see: |
| 51 | +# Civis: github.com/move-coop/parsons/blob/master/parsons/civis/civisclient.py |
| 52 | +# Twilio: github.com/move-coop/parsons/blob/master/parsons/twilio/twilio.py |
| 53 | + |
| 54 | +from parsons.civis.civisclient import CivisClient |
| 55 | +from parsons.twilio.twilio import Twilio |
| 56 | +import os |
| 57 | + |
| 58 | + |
| 59 | +class CivisTwilioConnector: |
| 60 | + |
| 61 | + def __init__(self): |
| 62 | + self.validate_environment() |
| 63 | + self.civis_client = CivisClient(db=CIVIS_DATABASE) |
| 64 | + self.twilio_client = Twilio( |
| 65 | + account_sid=os.environ.get('TWILIO_ACCOUNT_USERNAME'), |
| 66 | + auth_token=os.environ.get('TWILIO_ACCOUNT_PASSWORD')) |
| 67 | + |
| 68 | + def twilio_to_civis(self): |
| 69 | + table = self.twilio_client.get_account_usage() |
| 70 | + self.civis_client.table_import(table, CIVIS_TABLE, existing_table_rows='drop') |
| 71 | + |
| 72 | + def read_from_civis(self): |
| 73 | + query = f"SELECT * from {CIVIS_TABLE}" |
| 74 | + table = self.civis_client.query(query) |
| 75 | + print(table) |
| 76 | + |
| 77 | + def validate_environment(self): |
| 78 | + if not os.environ.get('TWILIO_ACCOUNT_USERNAME'): |
| 79 | + raise ValueError( |
| 80 | + "No credential named 'Twilio Account' found, see Step 4") |
| 81 | + if not os.environ.get('TWILIO_ACCOUNT_PASSWORD'): |
| 82 | + raise ValueError( |
| 83 | + "'Twilio Account' credential must be a custom credential, see Step 4") |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + connector = CivisTwilioConnector() |
| 88 | + connector.twilio_to_civis() |
| 89 | + connector.read_from_civis() |
0 commit comments