Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
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
|
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 |
-
#
|
59 |
message_payload = {
|
60 |
"content": answer,
|
61 |
"message_type": "outgoing",
|
62 |
"private": False,
|
63 |
-
"
|
|
|
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 |
|