diff --git a/scripts/migrate_to_turn/README.md b/scripts/migrate_to_turn/README.md index 41262172..9110cd4f 100644 --- a/scripts/migrate_to_turn/README.md +++ b/scripts/migrate_to_turn/README.md @@ -21,7 +21,7 @@ It will also output the latest modified on date in the batch, this can then be u ### update_turn_contacts.py -Update using the turn contacts api asynchronously. +Update Turn contacts using the Turn contacts api asynchronously. This script takes a filename of a file generated by the `fetch_rapidpro_contacts.py` script as a parameter and updates all the contact in the file on Turn. @@ -34,7 +34,7 @@ The output is sent to a json file, which can be used to retry failed requests. ### update_turn_contacts_queue.py -Update using the turn contacts api asynchronously but using a queue and workers. It will sleep if it gets rate limited by turn. +Update Turn contacts using the Turn contacts api asynchronously but using a queue and workers. It will sleep if it gets rate limited by Turn. This script takes a filename of a file generated by the `fetch_rapidpro_contacts.py` script as a parameter and updates all the contact in the file on Turn. @@ -45,6 +45,15 @@ Command to run: The output is sent to a json file, which can be used to retry failed requests. +### update_turn_contacts_bulk.py + +Update Turn contacts using the Turn bulk update contacts API. The API is currently limited to allow files of a maximum of 1 Megabyte in size. + +This script takes the csv provided and sends it directly yto the Turn API. It will output the results to a new csv file. + +Command to run: +`python scripts/migrate_to_turn/update_turn_contacts_bulk.py contacts-2024-01-01-2025-01-07.csv` + ## FIELD_MAPPING This is a dictionary the script uses to figure out where to get the data, how to process it and where it should go. diff --git a/scripts/migrate_to_turn/update_turn_contacts_bulk.py b/scripts/migrate_to_turn/update_turn_contacts_bulk.py new file mode 100644 index 00000000..e8793144 --- /dev/null +++ b/scripts/migrate_to_turn/update_turn_contacts_bulk.py @@ -0,0 +1,28 @@ +import os +import sys +from urllib.parse import urljoin + +import requests + +TURN_URL = "https://whatsapp-praekelt-cloud.turn.io" + + +def bulk_update_turn_contacts(filename): + data = open(filename, "rb") + url = urljoin(TURN_URL, "v1/contacts") + headers = { + "Authorization": f"Bearer {os.environ['TURN_TOKEN']}", + "content-type": "text/csv", + } + response = requests.post(url, data=data, headers=headers) + + print(response.status_code) + + f = open(f"result_{filename}", "wb") + f.write(response.content) + f.close() + + +if __name__ == "__main__": + filename = sys.argv[1] + bulk_update_turn_contacts(filename)