Skip to content

Commit

Permalink
code to trigger Studio API example
Browse files Browse the repository at this point in the history
  • Loading branch information
burutofoz committed Nov 12, 2024
1 parent ece3666 commit 03732d8
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
Binary file added __pycache__/csv_util.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/twilio_studio_trigger.cpython-310.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions content.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
to,from,body
+6595xx,+1626xxx,Hello, this is a test message.
+6595xx,+1626xxx,Another test message.
+6595xx,+1626xxx,Yet another test message.
9 changes: 9 additions & 0 deletions csv_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import csv

class CSVUtil:
@staticmethod
def read_csv(file_name):
with open(file_name, mode='r') as file:
csv_reader = csv.DictReader(file)
data = [row for row in csv_reader]
return data
26 changes: 26 additions & 0 deletions studioRestAPITrigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
import os
from csv_util import CSVUtil
from twilio_studio_trigger import TwilioStudioTrigger

def main():
if len(sys.argv) != 3:
print("Usage: python studioRestAPITrigger.py <csv_file> <flow_sid>")
sys.exit(1)

csv_file = sys.argv[1]
flow_sid = sys.argv[2]

account_sid = os.getenv('TQ_TWILIO_ACCOUNT_SID')
auth_token = os.getenv('TQ_TWILIO_AUTH_TOKEN')

if not account_sid or not auth_token or not flow_sid:
print("Please set the TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN environment variables and provide the FLOW_SID as an argument.")
sys.exit(1)

twilio_trigger = TwilioStudioTrigger(account_sid, auth_token, flow_sid)
csv_util = CSVUtil()
twilio_trigger.process_csv_and_trigger_flows(csv_file, csv_util)

if __name__ == "__main__":
main()
25 changes: 25 additions & 0 deletions twilio_studio_trigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from twilio.rest import Client

class TwilioStudioTrigger:
def __init__(self, account_sid, auth_token, flow_sid):
self.client = Client(account_sid, auth_token)
self.flow_sid = flow_sid

def trigger_flow(self, parameters):
execution = self.client.studio.v1.flows(self.flow_sid).executions.create(
to=parameters['to'],
from_=parameters['from'],
parameters=parameters
)
return execution.sid

def process_csv_and_trigger_flows(self, csv_file, csv_util):
data = csv_util.read_csv(csv_file)
for row in data:
parameters = {
'to': row['to'],
'from': row['from'],
'body': row['body']
}
execution_sid = self.trigger_flow(parameters)
print(f"Triggered flow with execution SID: {execution_sid}")

0 comments on commit 03732d8

Please sign in to comment.