-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |