Skip to content

Commit 85c2c5b

Browse files
authored
Merge pull request #654 from praekeltfoundation/migrate-to-turn-scripts
Add bulk update turn contacts script
2 parents 4006597 + f32cbf0 commit 85c2c5b

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

scripts/migrate_to_turn/README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ It will also output the latest modified on date in the batch, this can then be u
2121

2222
### update_turn_contacts.py
2323

24-
Update using the turn contacts api asynchronously.
24+
Update Turn contacts using the Turn contacts api asynchronously.
2525

2626
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.
2727

@@ -34,7 +34,7 @@ The output is sent to a json file, which can be used to retry failed requests.
3434

3535
### update_turn_contacts_queue.py
3636

37-
Update using the turn contacts api asynchronously but using a queue and workers. It will sleep if it gets rate limited by turn.
37+
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.
3838

3939
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.
4040

@@ -45,6 +45,15 @@ Command to run:
4545

4646
The output is sent to a json file, which can be used to retry failed requests.
4747

48+
### update_turn_contacts_bulk.py
49+
50+
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.
51+
52+
This script takes the csv provided and sends it directly yto the Turn API. It will output the results to a new csv file.
53+
54+
Command to run:
55+
`python scripts/migrate_to_turn/update_turn_contacts_bulk.py contacts-2024-01-01-2025-01-07.csv`
56+
4857
## FIELD_MAPPING
4958

5059
This is a dictionary the script uses to figure out where to get the data, how to process it and where it should go.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
import sys
3+
from urllib.parse import urljoin
4+
5+
import requests
6+
7+
TURN_URL = "https://whatsapp-praekelt-cloud.turn.io"
8+
9+
10+
def bulk_update_turn_contacts(filename):
11+
data = open(filename, "rb")
12+
url = urljoin(TURN_URL, "v1/contacts")
13+
headers = {
14+
"Authorization": f"Bearer {os.environ['TURN_TOKEN']}",
15+
"content-type": "text/csv",
16+
}
17+
response = requests.post(url, data=data, headers=headers)
18+
19+
print(response.status_code)
20+
21+
f = open(f"result_{filename}", "wb")
22+
f.write(response.content)
23+
f.close()
24+
25+
26+
if __name__ == "__main__":
27+
filename = sys.argv[1]
28+
bulk_update_turn_contacts(filename)

0 commit comments

Comments
 (0)