Skip to content

Commit 21c3a6e

Browse files
author
ted107mk
authored
[POL-1062] Add twilio -> civis connector using parsons library (#15)
* Add twilio -> civis connector using parsons library * Update gitignore, flake8 * update db * Update documentation for container script * Validate env variables * logging * Update steps to use custom credentials * Update docs * Add flake8 config * Add flake8 config * flake8 * Update docs * Add link to parsons github
1 parent 02a7c75 commit 21c3a6e

4 files changed

Lines changed: 99 additions & 8 deletions

File tree

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 100
3+
ignore = E402

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
**/.mypy_cache
3+
venv/

examples/email_reporting_python.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,13 @@ def to_markdown_table(data):
8888
def post_json_run_output(json_value_dict):
8989
client = civis.APIClient()
9090
json_value_object = client.json_values.post(
91-
json.dumps(json_value_dict),
92-
name='email_outputs'
93-
)
91+
json.dumps(json_value_dict),
92+
name='email_outputs')
9493
client.scripts.post_python3_runs_outputs(
95-
os.environ['CIVIS_JOB_ID'],
96-
os.environ['CIVIS_RUN_ID'],
97-
'JSONValue',
98-
json_value_object.id
99-
)
94+
os.environ['CIVIS_JOB_ID'],
95+
os.environ['CIVIS_RUN_ID'],
96+
'JSONValue',
97+
json_value_object.id)
10098

10199

102100
if __name__ == '__main__':
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)