166 lines
4.7 KiB
Python
166 lines
4.7 KiB
Python
# Hubspot assessment
|
|
# Author: Paul Halvorsen
|
|
# Email: pmghalvorsen@gmail.com
|
|
|
|
import sys
|
|
import requests
|
|
|
|
# JSON Inbox:
|
|
"""
|
|
{
|
|
"messages": [
|
|
{
|
|
"content": "string",
|
|
"fromUserId": 0000,
|
|
"timestamp": 1529338342000,
|
|
"toUserId": 1111
|
|
},
|
|
...
|
|
]
|
|
"userId": 50210,
|
|
"users": [
|
|
{
|
|
"avatar": "image file",
|
|
"firstName": "",
|
|
"lastName": "",
|
|
"id": 00000 # Doesn't have to match above
|
|
},
|
|
]
|
|
}
|
|
"""
|
|
|
|
# JSON Conversations look like:
|
|
"""
|
|
{
|
|
"conversations": [
|
|
{
|
|
"avatar": "image file",
|
|
"firstName": "",
|
|
"lastName": "",
|
|
"mostRecentMessage": {
|
|
"content": "",
|
|
"timestamp": 1533197225000,
|
|
"userId": 0000
|
|
},
|
|
"totalMessages": 3,
|
|
"userId": 11111
|
|
},
|
|
]
|
|
}
|
|
"""
|
|
|
|
# Get the inbox JSON data from the server
|
|
def get_inbox_data(url):
|
|
inbox_response = requests.get(url)
|
|
|
|
if inbox_response.status_code != 200:
|
|
print(f"Error receiving inbox data, return code: {inbox_response.status_code}")
|
|
sys.exit(1)
|
|
|
|
inbox_json = inbox_response.json()
|
|
|
|
if "messages" not in inbox_json:
|
|
print("Inbox response missing 'messages'")
|
|
sys.exit(1)
|
|
elif "userId" not in inbox_json:
|
|
print("Inbox response missing 'userId'")
|
|
sys.exit(1)
|
|
elif "users" not in inbox_json:
|
|
print("Inbox response missing 'users'")
|
|
sys.exit(1)
|
|
|
|
return inbox_response.json()
|
|
|
|
|
|
# Send the conversation JSON back to interface
|
|
def send_inbox_conversations(url, conversations):
|
|
convo_response = requests.post(url = url, json = conversations)
|
|
|
|
if convo_response.status_code >= 500:
|
|
print(f"Error connecting, contact server admin: Error Code: {convo_response.status_code}")
|
|
sys.exit(1)
|
|
elif convo_response.status_code >= 400:
|
|
print(f"Incorrect response: Code: {convo_response.status_code}: Error Message: {convo_response.text}")
|
|
sys.exit(1)
|
|
elif convo_response.status_code == 200:
|
|
print("Correct response!")
|
|
else:
|
|
print(f"Some other error: Code: {convo_response.status_code}")
|
|
sys.exit(1)
|
|
|
|
# Pull conversation data out of the JSON inbox data
|
|
# The obj keys are the id's of the contacted user
|
|
def build_conversation_data(inbox_data):
|
|
convo_id_data = {}
|
|
|
|
# Build a conversation object for each user
|
|
for user in inbox_data["users"]:
|
|
convo_id_data[user["id"]] = {
|
|
"avatar": user["avatar"],
|
|
"firstName": user["firstName"],
|
|
"lastName": user["lastName"],
|
|
"fromUserId": 0,
|
|
"content": "",
|
|
"timestamp": 0,
|
|
"totalMessages": 0
|
|
}
|
|
|
|
# Get the latest message and number of messages
|
|
for message in inbox_data["messages"]:
|
|
convo_with_id = 0
|
|
if not message["fromUserId"] == inbox_data["userId"]:
|
|
convo_with_id = message["fromUserId"]
|
|
else:
|
|
convo_with_id = message["toUserId"]
|
|
|
|
convo_id_data[convo_with_id]["totalMessages"] += 1
|
|
if convo_id_data[convo_with_id]["timestamp"] < message["timestamp"]:
|
|
convo_id_data[convo_with_id]["fromUserId"] = message["fromUserId"]
|
|
convo_id_data[convo_with_id]["content"] = message["content"]
|
|
convo_id_data[convo_with_id]["timestamp"] = message["timestamp"]
|
|
|
|
return convo_id_data
|
|
|
|
|
|
# Sort and structure the conversations from build_conversation_data
|
|
def get_conversations(convo_id_info):
|
|
conversations = []
|
|
for convo_id, convo_info in convo_id_info.items():
|
|
convo_obj = {
|
|
"avatar": convo_info["avatar"],
|
|
"firstName": convo_info["firstName"],
|
|
"lastName": convo_info["lastName"],
|
|
"mostRecentMessage": {
|
|
"content": convo_info["content"],
|
|
"timestamp": convo_info["timestamp"],
|
|
"userId": convo_info["fromUserId"]
|
|
},
|
|
"totalMessages": convo_info["totalMessages"],
|
|
"userId": convo_id
|
|
}
|
|
|
|
# Determine where this conversation goes in the sorted convos
|
|
insert_index = 0
|
|
for convo in conversations:
|
|
if convo["mostRecentMessage"]["timestamp"] > convo_info["timestamp"]:
|
|
insert_index += 1
|
|
else:
|
|
break
|
|
|
|
conversations.insert(insert_index, convo_obj)
|
|
|
|
return conversations
|
|
|
|
|
|
# Main program
|
|
url_inbox = '<add URL for pulling data>'
|
|
url_convo = '<add URL for pulling data>'
|
|
|
|
inbox_json = get_inbox_data(url_inbox)
|
|
convo_id_info = build_conversation_data(inbox_json)
|
|
conversations = {
|
|
"conversations": get_conversations(convo_id_info)
|
|
}
|
|
send_inbox_conversations(url_convo, conversations)
|
|
|