Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compare contacts script #656

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading