import os import boto3 import ast import traceback import time a = os.environ.get("AWS_ACCESS_KEY_ID") b = os.environ.get("AWS_SECRET_ACCESS_KEY") def dynamo(username, message): try: # Create a DynamoDB client dynamodb = boto3.client('dynamodb',aws_access_key_id=b, aws_secret_access_key=a, region_name='us-east-1' ) # Specify the table name table_name = 'PROD_historial_olivia' # Define the key you want to filter by key = 'usuario' value = username # Create the key condition expression as a string key_condition_expression = f"{key} = :val" # Define the expression attribute values expression_attribute_values = { ":val": {"S": value} # Assuming 'phone' is of string type } # Query the table response = dynamodb.query( TableName=table_name, KeyConditionExpression=key_condition_expression, ExpressionAttributeValues=expression_attribute_values ) # Retrieve the filtered items items = response['Items'] sorted_items = sorted(items, key=lambda x: x['fecha']['S']) mensajes = [] if len(sorted_items) > 0: if message == ast.literal_eval(sorted_items[-1]['interaccion']['S'])['Estudiante']: return ast.literal_eval(sorted_items[-1]['interaccion']['S'])['Olivia'] else: return 'esperar' else: return 'esperar' except Exception as e: print(f"An error occurred: {str(e)}") # Handle the error as needed # You can raise the error again, log it, or return a specific error response traceback_info = traceback.format_exc() print(traceback_info) return {'statusCode': 500, 'body': 'An error occurred during item query.'} def activar_olivia(username, current_message): sns_client = boto3.client('sns', aws_access_key_id=b, aws_secret_access_key=a, region_name='us-east-1' ) topic_arn = os.environ.get("ARN_OLIVIA") attributes = { 'username': {'DataType': 'String', 'StringValue': username}, 'message': {'DataType': 'String', 'StringValue': current_message}} r = sns_client.publish( TopicArn=topic_arn, Message='huggingface', MessageAttributes=attributes ) def lookup_users(username, password): print(username) try: dynamodb = boto3.client('dynamodb',aws_access_key_id=b, aws_secret_access_key=a, region_name='us-east-1' ) # Specify the table name table_name = 'PROD_usuario_olivia' # Query the table response = dynamodb.scan( TableName=table_name ) # Retrieve the filtered items items = response['Items'] if len(items)>0: for i in items: if username == i['usuario']['S'] and password == i['contraseña']['S']: os.environ['user'] = username return True return False except Exception as e: print(f"An error occurred: {str(e)}") return False def greet(name, password): r=lookup_users(name, password) if r == False: mensaje = f"""