File size: 8,050 Bytes
ac0f906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e83f5e9
 
 
 
ac0f906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e83f5e9
ac0f906
 
 
 
 
 
 
 
 
 
 
 
e83f5e9
 
 
 
 
 
 
ac0f906
 
 
 
 
 
 
 
 
 
 
 
 
 
e83f5e9
 
ac0f906
 
 
 
 
 
e83f5e9
 
ac0f906
e83f5e9
ac0f906
 
e83f5e9
 
 
 
 
 
 
 
c8b8c9b
 
e83f5e9
ac0f906
c8b8c9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e83f5e9
 
 
ac0f906
e83f5e9
 
 
 
 
 
 
ac0f906
c8b8c9b
 
 
 
e83f5e9
 
 
 
 
 
ac0f906
e83f5e9
ac0f906
 
 
 
 
c8b8c9b
 
ac0f906
 
 
c8b8c9b
 
 
ac0f906
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import os
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError
from dotenv import load_dotenv
from datetime import datetime, timedelta
import pytz
import logging

# Configure logging
logger = logging.getLogger(__name__)

# Load environment variables
load_dotenv()

# MongoDB connection string from .env
MONGODB_URL = os.getenv("MONGODB_URL")
DB_NAME = os.getenv("DB_NAME", "Telegram")
COLLECTION_NAME = os.getenv("COLLECTION_NAME", "session_chat")

# Set timeout for MongoDB connection
MONGODB_TIMEOUT = int(os.getenv("MONGODB_TIMEOUT", "5000"))  # 5 seconds by default

# Legacy cache settings - now only used for configuration purposes
HISTORY_CACHE_TTL = int(os.getenv("HISTORY_CACHE_TTL", "3600"))  # 1 hour by default
HISTORY_QUEUE_SIZE = int(os.getenv("HISTORY_QUEUE_SIZE", "10"))  # 10 items by default

# Create MongoDB connection with timeout
try:
    client = MongoClient(MONGODB_URL, serverSelectionTimeoutMS=MONGODB_TIMEOUT)
    db = client[DB_NAME]

    # Collections
    session_collection = db[COLLECTION_NAME]
    logger.info(f"MongoDB connection initialized to {DB_NAME}.{COLLECTION_NAME}")
    
except Exception as e:
    logger.error(f"Failed to initialize MongoDB connection: {e}")
    # Don't raise exception to avoid crash during startup, error handling will be done in functions

# Check MongoDB connection
def check_db_connection():
    """Check MongoDB connection"""
    try:
        # Issue a ping to confirm a successful connection
        client.admin.command('ping')
        logger.info("MongoDB connection is working")
        return True
    except (ConnectionFailure, ServerSelectionTimeoutError) as e:
        logger.error(f"MongoDB connection failed: {e}")
        return False
    except Exception as e:
        logger.error(f"Unknown error when checking MongoDB connection: {e}")
        return False

# Timezone for Asia/Ho_Chi_Minh
asia_tz = pytz.timezone('Asia/Ho_Chi_Minh')

def get_local_time():
    """Get current time in Asia/Ho_Chi_Minh timezone"""
    return datetime.now(asia_tz).strftime("%Y-%m-%d %H:%M:%S")

def get_local_datetime():
    """Get current datetime object in Asia/Ho_Chi_Minh timezone"""
    return datetime.now(asia_tz)

# For backward compatibility
get_vietnam_time = get_local_time
get_vietnam_datetime = get_local_datetime

# Utility functions
def save_session(session_id, factor, action, first_name, last_name, message, user_id, username, response=None):
    """Save user session to MongoDB"""
    try:
        session_data = {
            "session_id": session_id,
            "factor": factor,
            "action": action,
            "created_at": get_local_time(),
            "created_at_datetime": get_local_datetime(),
            "first_name": first_name,
            "last_name": last_name,
            "message": message,
            "user_id": user_id,
            "username": username,
            "response": response
        }
        result = session_collection.insert_one(session_data)
        logger.info(f"Session saved with ID: {result.inserted_id}")
        
        return {
            "acknowledged": result.acknowledged,
            "inserted_id": str(result.inserted_id),
            "session_data": session_data
        }
    except Exception as e:
        logger.error(f"Error saving session: {e}")
        raise

def update_session_response(session_id, response):
    """Update a session with response"""
    try:
        # Lấy session hiện có
        existing_session = session_collection.find_one({"session_id": session_id})
        
        if not existing_session:
            logger.warning(f"No session found with ID: {session_id}")
            return False
        
        result = session_collection.update_one(
            {"session_id": session_id},
            {"$set": {"response": response}}
        )
        
        logger.info(f"Session {session_id} updated with response")
        return True
    except Exception as e:
        logger.error(f"Error updating session response: {e}")
        raise

def get_recent_sessions(user_id, action, n=3):
    """Get n most recent sessions for a specific user and action"""
    try:
        # Truy vấn trực tiếp từ MongoDB
        result = list(
            session_collection.find(
                {"user_id": user_id, "action": action},
                {"_id": 0, "message": 1, "response": 1}
            ).sort("created_at_datetime", -1).limit(n)
        )
        
        logger.debug(f"Retrieved {len(result)} recent sessions for user {user_id}, action {action}")
        return result
    except Exception as e:
        logger.error(f"Error getting recent sessions: {e}")
        return []

def get_chat_history(user_id, n = 5) -> str:
    """
    Lấy lịch sử chat cho user_id từ MongoDB và ghép thành chuỗi theo định dạng:
    
    User: ...
    Bot: ...
    User: ...
    Bot: ...
    
    Chỉ lấy history sau lệnh /start hoặc /clear mới nhất
    """
    try:
        # Tìm session /start hoặc /clear mới nhất
        reset_session = session_collection.find_one(
            {
                "user_id": str(user_id), 
                "$or": [
                    {"action": "start"},
                    {"action": "clear"}
                ]
            },
            sort=[("created_at_datetime", -1)]
        )
        
        # Nếu không tìm thấy session reset nào, lấy n session gần nhất
        if reset_session:
            reset_time = reset_session["created_at_datetime"]
            # Lấy các session sau reset_time
            docs = list(
                session_collection.find({
                    "user_id": str(user_id),
                    "created_at_datetime": {"$gt": reset_time}
                }).sort("created_at_datetime", 1)
            )
            logger.info(f"Lấy {len(docs)} session sau lệnh {reset_session['action']} lúc {reset_time}")
        else:
            # Không tìm thấy reset session, lấy n session gần nhất
            docs = list(session_collection.find({"user_id": str(user_id)}).sort("created_at", -1).limit(n))
            # Đảo ngược để có thứ tự từ cũ đến mới
            docs.reverse()
            logger.info(f"Không tìm thấy session reset, lấy {len(docs)} session gần nhất")
            
        if not docs:
            logger.info(f"Không tìm thấy dữ liệu cho user_id: {user_id}")
            return ""
        
        conversation_lines = []
        # Xử lý từng document theo cấu trúc mới
        for doc in docs:
            factor = doc.get("factor", "").lower()
            action = doc.get("action", "").lower()
            message = doc.get("message", "")
            response = doc.get("response", "")
            
            # Bỏ qua lệnh start và clear
            if action in ["start", "clear"]:
                continue
                
            if factor == "user" and action == "asking_freely":
                conversation_lines.append(f"User: {message}")
                conversation_lines.append(f"Bot: {response}")
        
        # Ghép các dòng thành chuỗi
        return "\n".join(conversation_lines)
    except Exception as e:
        logger.error(f"Lỗi khi lấy lịch sử chat cho user_id {user_id}: {e}")
        return ""

def get_request_history(user_id, n=3):
    """Get the most recent user requests to use as context for retrieval"""
    try:
        # Truy vấn trực tiếp từ MongoDB
        history = get_chat_history(user_id, n)
        
        # Just extract the questions for context
        requests = []
        for line in history.split('\n'):
            if line.startswith("User: "):
                requests.append(line[6:])  # Lấy nội dung sau "User: "
            
        # Join all recent requests into a single string for context
        return " ".join(requests)
    except Exception as e:
        logger.error(f"Error getting request history: {e}")
        return ""