Update app.py
Browse files
app.py
CHANGED
|
@@ -122,7 +122,18 @@ def process_step_by_step(session_id, message):
|
|
| 122 |
msg_lower = message.lower().strip()
|
| 123 |
if session_id not in user_sessions:
|
| 124 |
if is_start_project_request(message):
|
| 125 |
-
user_sessions[session_id] = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
return "🌐 **Do you need a domain name?** (e.g., www.yourbusiness.com)\n\nReply: **Yes** or **No**"
|
| 127 |
return None
|
| 128 |
|
|
@@ -130,12 +141,14 @@ def process_step_by_step(session_id, message):
|
|
| 130 |
step = session.get("step")
|
| 131 |
req = session.get("requirements", {})
|
| 132 |
|
|
|
|
| 133 |
if msg_lower in ["add more", "add something", "extra add"]:
|
| 134 |
session["step"] = "asking_extra"
|
| 135 |
extra_services = get_extra_services()
|
| 136 |
extra_list = "\n".join([f"{v['name']}: +PKR {v['price']:,}, +{v['timeline']} days" for v in extra_services.values()])
|
| 137 |
return f"✨ **Add extra features**\n\nAvailable:\n{extra_list}\n\nType the name or **done**."
|
| 138 |
|
|
|
|
| 139 |
if step == "asking_domain":
|
| 140 |
req["domain"] = "yes" in msg_lower or "han" in msg_lower
|
| 141 |
session["step"] = "asking_pages"
|
|
@@ -175,12 +188,38 @@ def process_step_by_step(session_id, message):
|
|
| 175 |
elif step == "asking_extra":
|
| 176 |
extra_services = get_extra_services()
|
| 177 |
if msg_lower in ["none", "done", "no"]:
|
| 178 |
-
|
| 179 |
-
session["step"] = "
|
| 180 |
-
session["
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
✅ **Summary:**
|
| 186 |
• Domain: {'Yes' if req.get('domain') else 'No'}
|
|
@@ -188,14 +227,14 @@ def process_step_by_step(session_id, message):
|
|
| 188 |
• E‑commerce: {'Yes' if req.get('ecommerce') else 'No'}
|
| 189 |
• Chatbot: {'Yes' if req.get('chatbot') else 'No'}
|
| 190 |
• SEO: {'Yes' if req.get('seo') else 'No'}
|
| 191 |
-
• Extras: {extra_display}
|
| 192 |
|
| 193 |
💰 **Total Cost:** PKR {price:,}
|
| 194 |
⏱️ **Timeline:** {timeline} working days
|
| 195 |
|
| 196 |
❓ **Agree?** (Reply: **Confirm** or **Cancel**)
|
| 197 |
|
| 198 |
-
⚠️ *To add more, type 'add more'*"""
|
| 199 |
else:
|
| 200 |
matched_key = None
|
| 201 |
for key, val in extra_services.items():
|
|
@@ -214,6 +253,81 @@ def process_step_by_step(session_id, message):
|
|
| 214 |
else:
|
| 215 |
return f"❌ Sorry, **'{message}'** is not offered. Available: {', '.join([v['name'] for v in extra_services.values()])}\n\nType a valid name or **done**."
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
elif step == "confirm":
|
| 218 |
if "confirm" in msg_lower or "agree" in msg_lower:
|
| 219 |
project_id, price, timeline = create_project(session_id, req)
|
|
@@ -279,7 +393,7 @@ def get_final_response(user_message, session_id):
|
|
| 279 |
# 4. Simple fallback
|
| 280 |
return "- How can I help? Services: website design, e‑commerce, SEO, chatbots.\n- **To start a project, say 'I want to build a website'**"
|
| 281 |
|
| 282 |
-
# ---------- Admin API routes ----------
|
| 283 |
@app.route('/api/projects', methods=['GET'])
|
| 284 |
def get_projects():
|
| 285 |
snap = ref.child("projects").get()
|
|
@@ -356,7 +470,7 @@ def chat():
|
|
| 356 |
if not message:
|
| 357 |
return jsonify({"reply": "Please say something."})
|
| 358 |
reply = get_final_response(message, session_id)
|
| 359 |
-
# Save chat history (optional
|
| 360 |
ref.child(f"chats/{session_id}/messages").push().set({
|
| 361 |
"role": "user", "content": message, "timestamp": datetime.now().isoformat()
|
| 362 |
})
|
|
|
|
| 122 |
msg_lower = message.lower().strip()
|
| 123 |
if session_id not in user_sessions:
|
| 124 |
if is_start_project_request(message):
|
| 125 |
+
user_sessions[session_id] = {
|
| 126 |
+
"step": "asking_domain",
|
| 127 |
+
"requirements": {},
|
| 128 |
+
"additional_questions": [
|
| 129 |
+
{"key": "purpose", "question": "📌 **What is the main purpose of the website?** (e.g., e-commerce store, portfolio, blog, corporate) – Type **skip** to leave blank."},
|
| 130 |
+
{"key": "theme", "question": "🎨 **Any specific theme or design preference?** (e.g., modern, minimal, colorful, dark) – Type **skip** or describe."},
|
| 131 |
+
{"key": "content", "question": "📝 **Do you have ready content (text/images)?** Or need us to create? (Type **skip** if not needed now.)"},
|
| 132 |
+
{"key": "inventory", "question": "📦 **Do you need inventory management?** (For products, stock tracking) – Yes/No or **skip**."},
|
| 133 |
+
{"key": "data", "question": "🗄️ **Do you need database integration?** (e.g., user accounts, data storage) – Yes/No or **skip**."}
|
| 134 |
+
],
|
| 135 |
+
"additional_index": 0
|
| 136 |
+
}
|
| 137 |
return "🌐 **Do you need a domain name?** (e.g., www.yourbusiness.com)\n\nReply: **Yes** or **No**"
|
| 138 |
return None
|
| 139 |
|
|
|
|
| 141 |
step = session.get("step")
|
| 142 |
req = session.get("requirements", {})
|
| 143 |
|
| 144 |
+
# Handle "add more" after proposal
|
| 145 |
if msg_lower in ["add more", "add something", "extra add"]:
|
| 146 |
session["step"] = "asking_extra"
|
| 147 |
extra_services = get_extra_services()
|
| 148 |
extra_list = "\n".join([f"{v['name']}: +PKR {v['price']:,}, +{v['timeline']} days" for v in extra_services.values()])
|
| 149 |
return f"✨ **Add extra features**\n\nAvailable:\n{extra_list}\n\nType the name or **done**."
|
| 150 |
|
| 151 |
+
# Main step handling
|
| 152 |
if step == "asking_domain":
|
| 153 |
req["domain"] = "yes" in msg_lower or "han" in msg_lower
|
| 154 |
session["step"] = "asking_pages"
|
|
|
|
| 188 |
elif step == "asking_extra":
|
| 189 |
extra_services = get_extra_services()
|
| 190 |
if msg_lower in ["none", "done", "no"]:
|
| 191 |
+
# Move to additional info section
|
| 192 |
+
session["step"] = "asking_additional"
|
| 193 |
+
session["additional_index"] = 0
|
| 194 |
+
# Initialize additional answers dict if not exists
|
| 195 |
+
if "additional_answers" not in session:
|
| 196 |
+
session["additional_answers"] = {}
|
| 197 |
+
# Get first question
|
| 198 |
+
qlist = session.get("additional_questions", [])
|
| 199 |
+
if qlist:
|
| 200 |
+
return qlist[0]["question"]
|
| 201 |
+
else:
|
| 202 |
+
# No additional questions, go to proposal
|
| 203 |
+
session["step"] = "confirm"
|
| 204 |
+
price, timeline = calculate_price_and_timeline(req)
|
| 205 |
+
# Build proposal with any additional answers
|
| 206 |
+
extra_names = [extra_services.get(f, {}).get("name", f) for f in req.get("extra_features", [])]
|
| 207 |
+
extra_display = ", ".join(extra_names) if extra_names else "None"
|
| 208 |
+
add_answers = session.get("additional_answers", {})
|
| 209 |
+
add_text = ""
|
| 210 |
+
if any(add_answers.values()):
|
| 211 |
+
add_text = "\n\n📌 **Additional Info:**\n"
|
| 212 |
+
if add_answers.get("purpose"):
|
| 213 |
+
add_text += f"• Purpose: {add_answers['purpose']}\n"
|
| 214 |
+
if add_answers.get("theme"):
|
| 215 |
+
add_text += f"• Theme: {add_answers['theme']}\n"
|
| 216 |
+
if add_answers.get("content"):
|
| 217 |
+
add_text += f"• Content: {add_answers['content']}\n"
|
| 218 |
+
if add_answers.get("inventory"):
|
| 219 |
+
add_text += f"• Inventory: {add_answers['inventory']}\n"
|
| 220 |
+
if add_answers.get("data"):
|
| 221 |
+
add_text += f"• Data: {add_answers['data']}\n"
|
| 222 |
+
return f"""📋 **Your Project Proposal**
|
| 223 |
|
| 224 |
✅ **Summary:**
|
| 225 |
• Domain: {'Yes' if req.get('domain') else 'No'}
|
|
|
|
| 227 |
• E‑commerce: {'Yes' if req.get('ecommerce') else 'No'}
|
| 228 |
• Chatbot: {'Yes' if req.get('chatbot') else 'No'}
|
| 229 |
• SEO: {'Yes' if req.get('seo') else 'No'}
|
| 230 |
+
• Extras: {extra_display}{add_text}
|
| 231 |
|
| 232 |
💰 **Total Cost:** PKR {price:,}
|
| 233 |
⏱️ **Timeline:** {timeline} working days
|
| 234 |
|
| 235 |
❓ **Agree?** (Reply: **Confirm** or **Cancel**)
|
| 236 |
|
| 237 |
+
⚠️ *To add more extras, type 'add more'*"""
|
| 238 |
else:
|
| 239 |
matched_key = None
|
| 240 |
for key, val in extra_services.items():
|
|
|
|
| 253 |
else:
|
| 254 |
return f"❌ Sorry, **'{message}'** is not offered. Available: {', '.join([v['name'] for v in extra_services.values()])}\n\nType a valid name or **done**."
|
| 255 |
|
| 256 |
+
elif step == "asking_additional":
|
| 257 |
+
# Process additional questions
|
| 258 |
+
idx = session.get("additional_index", 0)
|
| 259 |
+
qlist = session.get("additional_questions", [])
|
| 260 |
+
if idx < len(qlist):
|
| 261 |
+
# Save answer (if not skip/none)
|
| 262 |
+
current_q = qlist[idx]
|
| 263 |
+
key = current_q["key"]
|
| 264 |
+
if msg_lower not in ["skip", "none", "no"]:
|
| 265 |
+
session["additional_answers"][key] = message
|
| 266 |
+
else:
|
| 267 |
+
session["additional_answers"][key] = ""
|
| 268 |
+
# Move to next
|
| 269 |
+
session["additional_index"] = idx + 1
|
| 270 |
+
if idx + 1 < len(qlist):
|
| 271 |
+
return qlist[idx+1]["question"]
|
| 272 |
+
else:
|
| 273 |
+
# All additional questions done, go to proposal
|
| 274 |
+
session["step"] = "confirm"
|
| 275 |
+
price, timeline = calculate_price_and_timeline(req)
|
| 276 |
+
extra_services = get_extra_services()
|
| 277 |
+
extra_names = [extra_services.get(f, {}).get("name", f) for f in req.get("extra_features", [])]
|
| 278 |
+
extra_display = ", ".join(extra_names) if extra_names else "None"
|
| 279 |
+
add_answers = session.get("additional_answers", {})
|
| 280 |
+
add_text = ""
|
| 281 |
+
if any(add_answers.values()):
|
| 282 |
+
add_text = "\n\n📌 **Additional Info:**\n"
|
| 283 |
+
if add_answers.get("purpose"):
|
| 284 |
+
add_text += f"• Purpose: {add_answers['purpose']}\n"
|
| 285 |
+
if add_answers.get("theme"):
|
| 286 |
+
add_text += f"• Theme: {add_answers['theme']}\n"
|
| 287 |
+
if add_answers.get("content"):
|
| 288 |
+
add_text += f"• Content: {add_answers['content']}\n"
|
| 289 |
+
if add_answers.get("inventory"):
|
| 290 |
+
add_text += f"• Inventory: {add_answers['inventory']}\n"
|
| 291 |
+
if add_answers.get("data"):
|
| 292 |
+
add_text += f"• Data: {add_answers['data']}\n"
|
| 293 |
+
return f"""📋 **Your Project Proposal**
|
| 294 |
+
|
| 295 |
+
✅ **Summary:**
|
| 296 |
+
• Domain: {'Yes' if req.get('domain') else 'No'}
|
| 297 |
+
• Pages: {req.get('pages', 5)}
|
| 298 |
+
• E‑commerce: {'Yes' if req.get('ecommerce') else 'No'}
|
| 299 |
+
• Chatbot: {'Yes' if req.get('chatbot') else 'No'}
|
| 300 |
+
• SEO: {'Yes' if req.get('seo') else 'No'}
|
| 301 |
+
• Extras: {extra_display}{add_text}
|
| 302 |
+
|
| 303 |
+
💰 **Total Cost:** PKR {price:,}
|
| 304 |
+
⏱️ **Timeline:** {timeline} working days
|
| 305 |
+
|
| 306 |
+
❓ **Agree?** (Reply: **Confirm** or **Cancel**)
|
| 307 |
+
|
| 308 |
+
⚠️ *To add more extras, type 'add more'*"""
|
| 309 |
+
else:
|
| 310 |
+
# Should not happen
|
| 311 |
+
session["step"] = "confirm"
|
| 312 |
+
price, timeline = calculate_price_and_timeline(req)
|
| 313 |
+
extra_services = get_extra_services()
|
| 314 |
+
extra_names = [extra_services.get(f, {}).get("name", f) for f in req.get("extra_features", [])]
|
| 315 |
+
extra_display = ", ".join(extra_names) if extra_names else "None"
|
| 316 |
+
return f"""📋 **Your Project Proposal**
|
| 317 |
+
|
| 318 |
+
✅ **Summary:**
|
| 319 |
+
• Domain: {'Yes' if req.get('domain') else 'No'}
|
| 320 |
+
• Pages: {req.get('pages', 5)}
|
| 321 |
+
• E‑commerce: {'Yes' if req.get('ecommerce') else 'No'}
|
| 322 |
+
• Chatbot: {'Yes' if req.get('chatbot') else 'No'}
|
| 323 |
+
• SEO: {'Yes' if req.get('seo') else 'No'}
|
| 324 |
+
• Extras: {extra_display}
|
| 325 |
+
|
| 326 |
+
💰 **Total Cost:** PKR {price:,}
|
| 327 |
+
⏱️ **Timeline:** {timeline} working days
|
| 328 |
+
|
| 329 |
+
❓ **Agree?** (Reply: **Confirm** or **Cancel**)"""
|
| 330 |
+
|
| 331 |
elif step == "confirm":
|
| 332 |
if "confirm" in msg_lower or "agree" in msg_lower:
|
| 333 |
project_id, price, timeline = create_project(session_id, req)
|
|
|
|
| 393 |
# 4. Simple fallback
|
| 394 |
return "- How can I help? Services: website design, e‑commerce, SEO, chatbots.\n- **To start a project, say 'I want to build a website'**"
|
| 395 |
|
| 396 |
+
# ---------- Admin API routes (unchanged) ----------
|
| 397 |
@app.route('/api/projects', methods=['GET'])
|
| 398 |
def get_projects():
|
| 399 |
snap = ref.child("projects").get()
|
|
|
|
| 470 |
if not message:
|
| 471 |
return jsonify({"reply": "Please say something."})
|
| 472 |
reply = get_final_response(message, session_id)
|
| 473 |
+
# Save chat history (optional)
|
| 474 |
ref.child(f"chats/{session_id}/messages").push().set({
|
| 475 |
"role": "user", "content": message, "timestamp": datetime.now().isoformat()
|
| 476 |
})
|