def delete_recording(sid): client = Client(account_sid, auth_token) client.recordings(sid).delete() files = glob.glob('static/recordings/*.wav') for f in files: try: if sid in f: os.remove(f) print(f, ' file deleted') except OSError as e: print("Error: %s : %s" % (f, e.strerror))
def recording_by_sid(sid): if request.method == 'GET': client = Client(account_sid, auth_token) recording = client.recordings(sid).fetch() print('recording found', recording.date_created) recording_url = "https://api.twilio.com{}".format(recording.uri.replace('.json', '')) decrypted_recording_name = recording.sid + '_decrypted.wav' if not decrypted_recording_exists(decrypted_recording_name): decrypt_recording(recording.encryption_details, recording_url, recording.sid) return render_template( 'recording.html', title="Voicemail {} page".format(recording.sid), recording=decrypted_recording_name, sid=recording.sid, path="/recording/{}".format(recording.sid) ) else: print('delete file') delete_recording(sid) return redirect('/')
import logging #write requests & responses from Twilio to log file, useful, IMHO, for debugging: logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s', filename= '/usr/local/twilio/python3/sdkv6x/recordings/logs/twilio_recordings.log', filemode='a') # Your Account Sid and Auth Token from twilio.com/console & stored in Mac OS ~/.bash_profile in this example account_sid = os.environ.get('$TWILIO_ACCOUNT_SID') auth_token = os.environ.get('$TWILIO_AUTH_TOKEN') client = Client(account_sid, auth_token) # A list of recording parameters & their permissable values, comment out (#) those lines not required recording = client.recordings('RExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx').fetch() #print list of all recording properties to console, useful for learning info available you can work with? print(recording.account_sid) print(recording.api_version) print(recording.call_sid) print(recording.channels) print(recording.conference_sid) print(recording.date_created) print(recording.date_updated) print(recording.duration) print(recording.encryption_details) print(recording.error_code) print(recording.price) print(recording.price_unit) print(recording.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) recording = client.recordings('RE557ce644e5ab84fa21cc21112e22c485').fetch() print(recording.call_sid)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) client.recordings("RE557ce644e5ab84fa21cc21112e22c485") \ .delete()
def cleanupRequests(n=10): """ Finds the last n records in the Requests table where "Status" = "Request Complete" that were last modified at least 30 days ago, erases the "Message" and "First Name" fields and deletes any recordings associated with the call. """ # formula for filtering data from airtable formula = 'AND(DATETIME_DIFF(NOW(), {Last Modified}, "days") > 30, Status = "Request Complete")' # airtable query headers = { "Authorization": "Bearer {}".format(os.environ['AIRTABLE_AUTH_TOKEN']) } params = params = { 'maxRecords': 10, 'view': 'All Requests + Data', 'sortField': 'Last Modified', 'sortDirection': 'asc', 'filterByFormula': formula } r = requests.get(os.environ['PROD_URL'], headers=headers, params=params) # if status code is good ... if r.status_code == 200: # instantiate twilio client client = Client(os.environ['ACCOUNT_SID'], os.environ['TWILIO_AUTH_TOKEN']) # iterate through records for record in r.json()['records']: data = {'fields': {'Message': "", 'First Name': ""}} # patch the requisite fields r = requests.patch(os.environ['PROD_URL'] + record['id'], headers=headers, json=data) # erase the recordings associated with the call SID call_sid = record['fields']['Twilio Call Sid'] call = client.calls(call_sid).fetch() for recording_sid in call.recordings.list(): client.recordings(recording_sid).delete() # confirm deletion successfully_deleted = 0 r = requests.get(os.environ['PROD_URL'] + record['id'], headers=headers) call = client.calls(call_sid).fetch() if all([ r.status_code == 200, 'Message' not in r.json().keys(), 'First Name' not in r.json().keys(), len(call.recordings.list()) == 0 ]): print('succesfully deleted') successfully_deleted += 1 else: print('error') return str(successfully_deleted)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) payload = client.recordings('REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .add_on_results('XRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .payloads('XHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .fetch() print(payload.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) client.recordings('REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX').delete()
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) recording = client.recordings("REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").fetch() print(recording.call_sid)
def main( delete: "Delete recordings" = False, archive: "Path to local directory" = None, after: "yyyy-mm-dd" = None, before: "yyyy-mm-dd" = None, summary: "Count recordings" = True, verbose: "List recording details" = False, confirm: "Confirm deletions" = True, account: "Account SID; if not given, value of environment variable TWILIO_ACCOUNT_SID" = None, password: "******" = None, subaccount: "If present, subaccount to use" = None): """ Lists and/or deletes Twilio recording files, optionally archiving them. """ # Validate command line arguments. if account is None: try: account = os.environ['TWILIO_ACCOUNT_SID'] except KeyError: sys.exit( "Error: no account, nor environment variable TWILIO_ACCOUNT_SID" ) if password is None: try: password = os.environ['TWILIO_AUTH_TOKEN'] except KeyError: sys.exit( "Error: no password, nor environment variable TWILIO_AUTH_TOKEN" ) if after: try: after = datetime.datetime.strptime(after, '%Y-%m-%d') except ValueError: sys.exit("Error: invalid after date") if before: try: before = datetime.datetime.strptime(before, '%Y-%m-%d') except ValueError: sys.exit("Error: invalid before date") if after and before: if after >= before: sys.exit("Error: after date is not before the before date") # If archiving, and archive directory does not exist, create it. if archive: try: if not os.path.exists(archive): os.mkdir(archive) else: if os.path.isfile(archive): sys.exit("Error: {0} is not a directory".format(archive)) except OSError: sys.exit("Error: unable to create directory {0}".format(archive)) # Construct base URL for downloading. base_url = "https://api.twilio.com/2010-04-01/Accounts/" + account + "/Recordings/" # Get the account name; this is used for the confirmation, and is a useful # check for correct username and password. client = Client(account, password, subaccount) try: account_struct = client.api.accounts( subaccount if subaccount else account).fetch() account_name = account_struct.friendly_name except TwilioException: sys.exit("Error: invalid account SID or auth token") # Ask for confirmation if delete has been selected and confirmation is required. if delete and confirm: prompt = ( "Delete all recordings for account {0} ".format(account_name) + ("created " if before or after else "") + ("after {0} ".format(after) if after else "") + ("and " if before and after else "") + ("before {0} ".format(before) if before else "") + "(y/n)? ") while True: response = input(prompt).strip().lower() if response in {'n', 'no'}: sys.exit("Aborted") if response in {'y', "yes"}: break # Get recordings list. recordings = client.recordings.list(date_created_before=before, date_created_after=after) if verbose: print( "Date/Time Created,Recording SID,Account SID,URI,Duration,Action") count = 0 downloaded = 0 skipped = 0 # Loop through the list, downloading and deleting recordings as required. for recording in recordings: count += 1 action = "" if archive: filename = os.path.join(archive, recording.sid + ".mp3") # If the file was downloaded previously, don't do it again. if os.path.exists(filename): skipped += 1 else: url = base_url + recording.sid + ".mp3" download(url, filename) action = "downloaded" downloaded += 1 if delete: client.recordings(recording.sid).delete() action = "deleted" if action == "" else (action + "+deleted") if verbose: print("{0},{1},{2},{3},{4}".format(recording.date_created, recording.sid, recording.call_sid, recording.duration, action)) # Finally, print summary. if summary: print("{0} recordings".format(count) + (" deleted" if delete else "") + (", {0} downloaded, {1} skipped".format(downloaded, skipped)) if archive else "")
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) add_on_results = client.recordings('REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .add_on_results \ .list(limit=20) for record in add_on_results: print(record.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) client.recordings("REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").delete()
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) payloads = client.recordings("REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .add_on_results("XRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .payloads \ .list() for record in payloads: print(record.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) recording = client.recordings('REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX').fetch() print(recording.call_sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) client.recordings('RE557ce644e5ab84fa21cc21112e22c485').delete()
#write requests & responses from Twilio to log file, useful, IMHO, for debugging: logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s', filename= '/usr/local/twilio/python3/sdkv6x/recordings/logs/twilio_recordings.log', filemode='a') # Your Account Sid and Auth Token from twilio.com/console & stored in Mac OS ~/.bash_profile in this example account_sid = os.environ.get('$TWILIO_ACCOUNT_SID') auth_token = os.environ.get('$TWILIO_AUTH_TOKEN') client = Client(account_sid, auth_token) # A list of recording parameters & their permissable values, comment out (#) those lines not required client.recordings('RExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx').delete() # print list of all recording properties to console, useful for learning info available you can work with? #print(recording.account_sid) #print(recording.api_version) #print(recording.call_sid) #print(recording.channels) #print(recording.conference_sid) #print(recording.date_created) #print(recording.date_updated) #print(recording.duration) #print(recording.encryption_details) #print(recording.error_code) #print(recording.price) #print(recording.price_unit) #print(recording.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) add_on_results = client.recordings("REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .add_on_results \ .list() for record in add_on_results: print(record.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) add_on_result = client.recordings('REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .add_on_results('XRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .fetch() print(add_on_result.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) payloads = client.recordings('REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .add_on_results('XRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .payloads \ .list(limit=20) for record in payloads: print(record.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) payload = client.recordings("REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .add_on_results("XRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .payloads("XHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .fetch() print(payload.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) client.recordings("REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .add_on_results("XRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .delete()
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) client.recordings('REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .add_on_results('XRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \ .delete()
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) add_on_result = client.recordings("REXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .add_on_results("XRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \ .fetch() print(add_on_result.sid)