ethiotech4848 commited on
Commit
9cb41cb
Β·
verified Β·
1 Parent(s): 974c992

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -23,7 +23,7 @@ client.base_url = os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1")
23
  # Chatwoot config
24
  CHATWOOT_BASE_URL = os.getenv("CHATWOOT_BASE_URL") # e.g., https://app.chatwoot.com
25
  CHATWOOT_API_KEY = os.getenv("CHATWOOT_API_KEY") # API Access Token of bot
26
- BOT_AGENT_ID = os.getenv("BOT_AGENT_ID") # Agent ID (integer)
27
 
28
  @app.post("/ask")
29
  async def ask(request: Request):
@@ -33,8 +33,8 @@ async def ask(request: Request):
33
  user_question = payload.get("message") or payload.get("content") or ""
34
  conversation_id = payload.get("conversation", {}).get("id")
35
 
36
- if not user_question or not conversation_id:
37
- print("❌ Missing message or conversation ID")
38
  return {"status": "invalid payload"}
39
 
40
  messages = [
@@ -55,17 +55,18 @@ async def ask(request: Request):
55
  print("❌ OpenAI Error:", e)
56
  answer = "Sorry, I'm having trouble answering right now."
57
 
58
- # Send answer back to Chatwoot conversation
59
  message_payload = {
60
  "content": answer,
61
  "message_type": "outgoing",
62
  "private": False,
63
- "agent_id": int(BOT_AGENT_ID)
 
64
  }
65
 
66
  try:
67
  async with httpx.AsyncClient() as http:
68
- chatwoot_url = f"{CHATWOOT_BASE_URL}/api/v1/conversations/{conversation_id}/messages"
69
  print("πŸ“€ Sending to Chatwoot:", chatwoot_url)
70
  print("πŸ“¦ Payload to Chatwoot:", json.dumps(message_payload, indent=2))
71
 
 
23
  # Chatwoot config
24
  CHATWOOT_BASE_URL = os.getenv("CHATWOOT_BASE_URL") # e.g., https://app.chatwoot.com
25
  CHATWOOT_API_KEY = os.getenv("CHATWOOT_API_KEY") # API Access Token of bot
26
+ CHATWOOT_ACCOUNT_ID = os.getenv("CHATWOOT_ACCOUNT_ID") # Account ID (integer)
27
 
28
  @app.post("/ask")
29
  async def ask(request: Request):
 
33
  user_question = payload.get("message") or payload.get("content") or ""
34
  conversation_id = payload.get("conversation", {}).get("id")
35
 
36
+ if not user_question or not conversation_id or not CHATWOOT_ACCOUNT_ID:
37
+ print("❌ Missing message, conversation ID, or account ID")
38
  return {"status": "invalid payload"}
39
 
40
  messages = [
 
55
  print("❌ OpenAI Error:", e)
56
  answer = "Sorry, I'm having trouble answering right now."
57
 
58
+ # Prepare Chatwoot message payload
59
  message_payload = {
60
  "content": answer,
61
  "message_type": "outgoing",
62
  "private": False,
63
+ "content_type": "text",
64
+ "content_attributes": {}
65
  }
66
 
67
  try:
68
  async with httpx.AsyncClient() as http:
69
+ chatwoot_url = f"{CHATWOOT_BASE_URL}/api/v1/accounts/{CHATWOOT_ACCOUNT_ID}/conversations/{conversation_id}/messages"
70
  print("πŸ“€ Sending to Chatwoot:", chatwoot_url)
71
  print("πŸ“¦ Payload to Chatwoot:", json.dumps(message_payload, indent=2))
72