Viewing File: /home/ubuntu/codegamaai-test/broker_bot/src/buy_stocks.py

from src.utils import *
from help_apis.intent_class import *
from help_apis.entity_extraction import *
from stock_apis.get_stock_info import *
from similarity_search.similarity_check import *

def buy_stock_scenerio(user_query, user_intent, user_config, key):
        previous_intent = user_config["previous_intent"]
        previous_status = user_config["status"]

        if previous_status == "stock_name_quantity_given":
        # Check if the user has confirmed the purchase
            stock_name = user_config["current_stocks"]
            stock_quantity = user_config["current_quantity"]

            if user_query.lower() == "yes":
                # Redirect to Stock Information API that will Give the Stock Information and Ask for the confirmation based on the information and then proceed to buy the stocks
                response = f"The stocks purchase of {stock_name} with quantity {stock_quantity} has been initiated. The stocks will be added to your portfolio once the purchase is complete."
                user_config = {"previous_intent": None, "current_intent": None, "status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                create_config_json(key,user_config)
                return response
            elif user_query.lower() == "no":
                response = f"The purchase of {stock_name} has been cancelled."
                user_config = {"previous_intent": None, "current_intent": None, "status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                create_config_json(key,user_config)
                return response
            else:
                response = f"The response is not valid. The purchase of {stock_name} has been cancelled."
                user_config = {"previous_intent": None, "current_intent": None,"status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                create_config_json(key,user_config)

                return response
    
        elif previous_status == "stock_name_given":
            # We are expecting the quantity of the stock
            relevent_entities = extract_entities(user_query)
            quantity_list = relevent_entities["quantity"]
            stock_name = user_config["current_stocks"]
            if quantity_list != []:
                stock_quantity = quantity_list[0]
                # Considering the previous query stock name
                stock_name = user_config["current_stocks"]
                get_ticker = get_stock_ticker(stock_name)
                if get_ticker == "No Symbol Found":
                    response = f"The stock name {stock_name} is not valid. The current process has been cancelled. Please retry with a valid stock name."
                    user_config = {"previous_intent": None, "current_intent": None,"status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                    create_config_json(key,user_config)
                    return response
                else:
                    stock_name = get_ticker['name']
                    stock_symbol = get_ticker['symbol']
                    current_stock_information = stock_info(stock_symbol)
                    response = f"Do you want to proceed with the purchase? Reply with 'Yes' or 'No' to confirm."
                    response = current_stock_information + "\n" + response
                    # Update config file
                    user_config["status"] = "stock_name_quantity_given"
                    # user_config["previous_intent"] = user_intent
                    user_config["current_intent"] = user_intent
                    user_config["previous_question"] = user_query
                    user_config["current_quantity"] = stock_quantity
                    user_config["current_stocks"] = stock_name
                    create_config_json(key,user_config)

                    return response
            else:
                response = f"The quantity of the stock is not valid. The purchase of {stock_name} has been cancelled."
                user_config = {"previous_intent": None, "current_intent": None,"status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                create_config_json(key,user_config)
                return response
        
        elif previous_status == "quantity_given":
            # We are expecting the stock name
            relevent_entities = extract_entities(user_query)
            stock_list = relevent_entities["stock"]
            if stock_list != []:
                stock_name = stock_list[0]
                stock_quantity = user_config["current_quantity"]
                # Update config file
                get_ticker = get_stock_ticker(stock_name)
                if get_ticker == "No Symbol Found":
                    response = f"The stock name {stock_name} is not valid. The current process has been cancelled. Please retry with a valid stock name."
                    user_config = {"previous_intent": None, "current_intent": None,"status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                    create_config_json(key,user_config)
                    return response
                else:
                    stock_name = get_ticker['name']
                    stock_symbol = get_ticker['symbol']
                    current_stock_information = stock_info(stock_symbol)
                    response = f"Do you want to proceed with the purchase? Reply with 'Yes' or 'No' to confirm."
                    response = current_stock_information + "\n" + response
                    user_config["status"] = "stock_name_quantity_given"
                    # user_config["previous_intent"] = user_intent
                    user_config["current_intent"] = user_intent
                    user_config["previous_question"] = user_query
                    user_config["current_stocks"] = stock_name
                    create_config_json(key,user_config)

                    return response
            
            else:
                response = f"The stock name is not valid. The purchase of has been cancelled."
                user_config = {"previous_intent": None, "current_intent": None,"status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                create_config_json(key,user_config)
                return response
        
        elif previous_status == "no_entity_given":
            # We are expecting the stock name and quantity
            relevent_entities = extract_entities(user_query)
            stock_list = relevent_entities["stock"]
            quantity_list = relevent_entities["quantity"]
            if stock_list != []:
                stock_name = stock_list[0]
            else:
                stock_name = None
            if quantity_list != []:
                stock_quantity = quantity_list[0]
            else:
                stock_quantity = None
            
            if stock_name == None or stock_quantity == None:
                response = f"The stock name or quantity is not valid. The purchase of has been cancelled."
                user_config = {"previous_intent": None,"current_intent": None, "status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                create_config_json(key,user_config)
                return response
            elif stock_name != None and stock_quantity != None:
                get_ticker = get_stock_ticker(stock_name)
                if get_ticker == "No Symbol Found":
                    response = f"The stock name {stock_name} is not valid. The current process has been cancelled. Please retry with a valid stock name."
                    user_config = {"previous_intent": None, "current_intent": None,"status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                    create_config_json(key,user_config)
                    return response
                else:
                    stock_name = get_ticker['name']
                    stock_symbol = get_ticker['symbol']
                    current_stock_information = stock_info(stock_symbol)
                    response = f"Do you want to proceed with the purchase? Reply with 'Yes' or 'No' to confirm."
                    response = current_stock_information + "\n" + response
                    # Update config file
                    user_config["status"] = "stock_name_quantity_given"
                    # user_config["previous_intent"] = user_intent
                    user_config["current_intent"] = user_intent
                    user_config["previous_question"] = user_query
                    user_config["current_stocks"] = stock_name
                    user_config["current_quantity"] = stock_quantity
                    create_config_json(key,user_config)
                    return response
            else:
                response = f"The stock name or quantity is not valid. The purchase of has been cancelled."
                user_config = {"previous_intent": None, "current_intent": None,"status":0, "previous_question": None,"previous_response": None, "current_stocks": None, "current_quantity": None, "current_stock_value": None, "user_portofolio_info": None}
                create_config_json(key,user_config)
                return response
            
Back to Directory File Manager