Fred808 commited on
Commit
ead3fd2
·
verified ·
1 Parent(s): 0d6c258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -235,25 +235,34 @@ async def on_startup():
235
  async def enhanced_chatbot_handler(request: Request, bg: BackgroundTasks):
236
  data = await request.json()
237
  user_id = data["user_id"]
238
- message = data.get("message", "")
239
 
240
  # Initialize conversation state if it doesn't exist
241
  if user_id not in user_state:
242
  user_state[user_id] = ConversationState()
243
-
244
  state = user_state[user_id]
245
  state.update_last_active()
246
-
247
- # Dummy conversation handling logic
248
  response = await DeliveryUXManager.generate_response_template(user_id)
249
- response["content"]["text"] = f"Received your message: {message}"
250
- state.add_context("user", message)
251
 
252
- # Background task to update user's last interaction in the DB
253
- bg.add_task(update_user_last_interaction, user_id)
 
 
 
 
 
 
 
 
 
254
 
 
 
255
  return JSONResponse(response)
256
 
 
257
  @app.post("/ux/preferences")
258
  async def update_ux_preferences(request: Request):
259
  data = await request.json()
 
235
  async def enhanced_chatbot_handler(request: Request, bg: BackgroundTasks):
236
  data = await request.json()
237
  user_id = data["user_id"]
238
+ message = data.get("message", "").strip().lower()
239
 
240
  # Initialize conversation state if it doesn't exist
241
  if user_id not in user_state:
242
  user_state[user_id] = ConversationState()
 
243
  state = user_state[user_id]
244
  state.update_last_active()
245
+
246
+ # Generate a response template
247
  response = await DeliveryUXManager.generate_response_template(user_id)
 
 
248
 
249
+ # Handle specific commands
250
+ if message == "delivery":
251
+ # For example, generate a dummy delivery ID using the current timestamp
252
+ delivery_id = f"DEL-{int(time.time())}"
253
+ response["content"]["text"] = f"Your delivery request has been received. Your delivery id is {delivery_id}."
254
+ elif message == "rates":
255
+ response["content"]["text"] = "Delivery rates:\n• Standard: ₦500\n• Express: ₦1000"
256
+ elif message == "track delivery":
257
+ response["content"]["text"] = "Please provide your delivery ID (e.g., 'track delivery DEL-123456789')."
258
+ else:
259
+ response["content"]["text"] = f"Received your message: {message}"
260
 
261
+ state.add_context("user", message)
262
+ bg.add_task(update_user_last_interaction, user_id)
263
  return JSONResponse(response)
264
 
265
+
266
  @app.post("/ux/preferences")
267
  async def update_ux_preferences(request: Request):
268
  data = await request.json()