Skip to content

Commit

Permalink
Merge pull request #654 from praekeltfoundation/migrate-to-turn-scripts
Browse files Browse the repository at this point in the history
Add bulk update turn contacts script
  • Loading branch information
erikh360 authored Jan 16, 2025
2 parents 4006597 + f32cbf0 commit 85c2c5b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
13 changes: 11 additions & 2 deletions scripts/migrate_to_turn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Expand All @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions scripts/migrate_to_turn/update_turn_contacts_bulk.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 85c2c5b

Please sign in to comment.