Skip to content

Commit

Permalink
Merge pull request #656 from praekeltfoundation/migrate-to-turn-scripts
Browse files Browse the repository at this point in the history
Add compare contacts script
  • Loading branch information
erikh360 authored Jan 21, 2025
2 parents 9f9b41d + 4aa7a51 commit df84c58
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
11 changes: 11 additions & 0 deletions scripts/migrate_to_turn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ This script takes the csv provided and sends it directly yto the Turn API. It wi
Command to run:
`python scripts/migrate_to_turn/update_turn_contacts_bulk.py contacts-2024-01-01-2025-01-07.csv`

### compare_contacts.py

This script can be used to compare specific contacts.

Add the WhatsApp IDs of the contacts you want to compare to the WA_IDS list in the script.

The script will get their Rapidpro and Turn contact details and output everything to a `compare.csv` file.

Command to run:
`python scripts/migrate_to_turn/compare_contacts.py`

## 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
70 changes: 70 additions & 0 deletions scripts/migrate_to_turn/compare_contacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import csv
import os
from urllib.parse import urljoin

import requests
from fetch_rapidpro_contacts import FIELD_MAPPING
from temba_client.v2 import TembaClient

RAPIDPRO_URL = "https://rapidpro.qa.momconnect.co.za"
TURN_URL = "https://whatsapp-praekelt-cloud.turn.io"

WA_IDS = ["27836378531"]

rapidpro_client = TembaClient(RAPIDPRO_URL, os.environ["RAPIDPRO_TOKEN"])


def get_rapidpro_contact(wa_id):
urn = f"whatsapp:{wa_id}"
contact = rapidpro_client.get_contacts(urn=urn).first(retry_on_rate_exceed=True)
data = {}

for rapidpro_field, turn_details in FIELD_MAPPING.items():
if turn_details["type"] == "default":
data[rapidpro_field] = getattr(contact, rapidpro_field)
else:
data[rapidpro_field] = contact.fields[rapidpro_field]

return data


def get_turn_contact(wa_id):
headers = {
"Authorization": "Bearer {}".format(os.environ["TURN_TOKEN"]),
"content-type": "application/json",
"Accept": "application/vnd.v1+json",
}
response = requests.get(
urljoin(TURN_URL, "/v1/contacts/{}/profile".format(wa_id)),
headers=headers,
)
contact = response.json()["fields"]

data = {}
for rapidpro_field, turn_details in FIELD_MAPPING.items():
data[rapidpro_field] = contact[turn_details["turn_name"]]
return data


def compare_contacts():
rows = []
for wa_id in WA_IDS:
rapidpro_data = get_rapidpro_contact(wa_id)
turn_data = get_turn_contact(wa_id)

row = {"wa_id": wa_id}
for rapidpro_field in FIELD_MAPPING.keys():
row[f"RP {rapidpro_field}"] = rapidpro_data[rapidpro_field]
row[f"TURN {rapidpro_field}"] = turn_data[rapidpro_field]

rows.append(row)

with open("compare.csv", "w", encoding="utf-8") as f:
fieldnames = rows[0].keys()
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)


if __name__ == "__main__":
compare_contacts()

0 comments on commit df84c58

Please sign in to comment.