Spaces:
Runtime error
Runtime error
File size: 3,207 Bytes
185d33a 12058fc ff85563 12058fc 185d33a 12058fc 185d33a 12058fc 185d33a 12058fc ff85563 12058fc ff85563 12058fc 185d33a 12058fc ff85563 12058fc 185d33a 12058fc ff85563 12058fc 185d33a 12058fc 185d33a ff85563 185d33a 12058fc ff85563 12058fc 185d33a 12058fc ff85563 12058fc ff85563 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import os
from datetime import datetime
from dotenv import load_dotenv
from supabase import create_client
load_dotenv()
SUPA = create_client(
os.environ.get('SUPABASE_URL'),
os.environ.get('SUPABASE_KEY')
)
def log_message_data_through_supabase_api(table_name, log_data):
return SUPA.table(table_name).insert(log_data).execute()
def format_datetime_in_isoformat(dt):
return getattr(dt.now(), 'isoformat', lambda x: None)()
def get_or_create_supabase_entry(table_name, insert_data, check_variable=None):
""" Checks if project or contact exists and adds entry if not found
Input:
- table_name: str- the name of the table in Supabase that is being examined
- insert_data: json - the data to insert
- check_variable: str/None - the specific field to check for existing match
Result
- logged_data - an object with the Supabase data
"""
if table_name == 'contact':
resp = SUPA.table('contact').select("*").eq(
"original_contact_id",
insert_data['original_contact_id']
).eq(
"project",
insert_data['project']
).execute()
else:
resp = SUPA.table(table_name).select("*").eq(
check_variable,
insert_data[check_variable]
).execute()
if len(resp.data) == 0:
logged_data = log_message_data_through_supabase_api(
table_name,
insert_data
)
else:
logged_data = resp
return logged_data
def prepare_message_data_for_logging(message_data, nlu_response):
""" Builds objects for each table and logs them to the database
Input:
- message_data: an object with the full message data from Turn.io/Whatsapp
"""
project_data = {
'name': "Rori",
# Autogenerated fields: id, created_at, modified_at
}
project_data_log = get_or_create_supabase_entry(
'project',
project_data,
'name'
)
contact_data = {
'project': project_data_log.data[0]['id'], # FK
'original_contact_id': message_data['contact_uuid'],
'urn': "",
'language_code': "en",
'contact_inserted_at': format_datetime_in_isoformat(datetime.now())
# Autogenerated fields: id, created_at, modified_at
}
contact_data_log = get_or_create_supabase_entry('contact', contact_data)
del message_data['author_id']
message_data = {
'contact': contact_data_log.data[0]['id'], # FK
'original_message_id': message_data['message_id'],
'text': message_data['message_body'],
'direction': message_data['message_direction'],
'sender_type': message_data['author_type'],
'channel_type': "whatsapp / turn.io",
'message_inserted_at': message_data['message_inserted_at'],
'message_modified_at': message_data['message_updated_at'],
'message_sent_at': format_datetime_in_isoformat(datetime.now()),
'nlu_response': nlu_response,
'request_object': message_data
# Autogenerated fields: created_at, modified_at
}
message_data_log = log_message_data_through_supabase_api(
'message',
message_data
)
|