Spaces:
Runtime error
Runtime error
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 whether a project or contact exists in the database and adds if one is 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 examine for existing matches | |
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, request_object): | |
""" Builds the message data for each table and ensures it's logged 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['message']['_vnd']['v1']['chat']['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) | |
message_data = { | |
'contact': contact_data_log.data[0]['id'], # FK | |
'original_message_id': message_data['message']['id'], | |
'text': message_data['message']['text']['body'], | |
'direction': message_data['message']['_vnd']['v1']['direction'], | |
'sender_type': message_data['message']['_vnd']['v1']['author']['type'], | |
'channel_type': "whatsapp / turn.io", | |
'message_inserted_at': message_data['message']['_vnd']['v1']['chat']['inserted_at'], | |
'message_modified_at': message_data['message']['_vnd']['v1']['chat']['updated_at'], | |
'message_sent_at': format_datetime_in_isoformat(datetime.now()), | |
'nlu_response': nlu_response, | |
'request_object': request_object | |
# Autogenerated fields: created_at, modified_at | |
} | |
message_data_log = log_message_data_through_supabase_api('message', message_data) | |