File size: 3,030 Bytes
17af28e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os

# File to store user preferences
PREFERENCES_FILE = "user_preferences.json"

# Load or create user preferences file
def load_preferences():
    if os.path.exists(PREFERENCES_FILE):
        with open(PREFERENCES_FILE, "r") as file:
            return json.load(file)
    return {}

def save_preferences(preferences):
    with open(PREFERENCES_FILE, "w") as file:
        json.dump(preferences, file, indent=4)

# Initialize preferences
user_preferences = load_preferences()

# Function to get user preferences
def get_user_preferences(user_id):
    return user_preferences.get(user_id, {"name": None, "style": None})

# Function to update user preferences
def update_user_preferences(user_id, name=None, style=None):
    if user_id not in user_preferences:
        user_preferences[user_id] = {}
    if name:
        user_preferences[user_id]["name"] = name
    if style:
        user_preferences[user_id]["style"] = style
    save_preferences(user_preferences)

# Function to provide advice based on tone
def generate_advice(user_style, user_name):
    advice_pool = {
        "gentleness": f"{user_name}, remember that progress takes time. Be kind to yourself as you grow.",
        "tough love": f"Listen, {user_name}, no excuses! You’ve got what it takes—so get to it!",
        "words of affirmation": f"You’re amazing, {user_name}. Believe in yourself; you’re capable of incredible things.",
    }
    return advice_pool.get(user_style, "I'm here to support you in the way that works best for you!")

# Main chatbot response function
def wellness_bot_response(user_id, user_input):
    preferences = get_user_preferences(user_id)
    if not preferences["name"] or not preferences["style"]:
        # Collect user preferences if not set
        if not preferences["name"]:
            preferences["name"] = user_input  # Assume input is the user's name
            update_user_preferences(user_id, name=preferences["name"])
            return f"Hi {preferences['name']}! What motivational style do you prefer? (e.g., gentleness, tough love, words of affirmation)"
        elif not preferences["style"]:
            preferences["style"] = user_input.lower()  # Assume input is the style
            update_user_preferences(user_id, style=preferences["style"])
            return f"Got it, {preferences['name']}! I’ll use a {preferences['style']} approach from now on. How can I support you today?"
    else:
        # Provide advice based on preferences
        advice = generate_advice(preferences["style"], preferences["name"])
        return f"{advice}\nLet me know if there’s anything specific on your mind!"

# Example usage
if __name__ == "__main__":
    user_id = "user123"  # Replace with a unique identifier for each user
    print(wellness_bot_response(user_id, "Alice"))  # First input (name)
    print(wellness_bot_response(user_id, "gentleness"))  # Second input (style)
    print(wellness_bot_response(user_id, "I need help staying motivated."))  # Third input (advice request)