Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,9 +23,9 @@ 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")
|
| 25 |
CHATWOOT_API_KEY = os.getenv("CHATWOOT_API_KEY")
|
| 26 |
-
CHATWOOT_ACCOUNT_ID = int(os.getenv("CHATWOOT_ACCOUNT_ID"
|
| 27 |
|
| 28 |
-
#
|
| 29 |
stop_reply_conversations = set()
|
| 30 |
|
| 31 |
@app.post("/ask")
|
|
@@ -33,49 +33,47 @@ async def ask(request: Request):
|
|
| 33 |
payload = await request.json()
|
| 34 |
print("π₯ Incoming payload:", json.dumps(payload, indent=2))
|
| 35 |
|
| 36 |
-
message_type = payload.get("message_type", "").lower()
|
| 37 |
account_id = payload.get("account", {}).get("id")
|
| 38 |
-
sender_id = payload.get("sender", {}).get("id")
|
| 39 |
conversation_id = payload.get("conversation", {}).get("id")
|
|
|
|
| 40 |
sender_role = payload.get("sender", {}).get("role", "").lower()
|
|
|
|
| 41 |
message_content = payload.get("content", "").strip()
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
if account_id != CHATWOOT_ACCOUNT_ID:
|
| 45 |
-
print(f"β Ignoring message from account ID {account_id} (only {CHATWOOT_ACCOUNT_ID} is allowed)")
|
| 46 |
-
return {"status": "ignored: invalid account"}
|
| 47 |
-
|
| 48 |
-
# Only respond to incoming messages
|
| 49 |
if message_type != "incoming":
|
| 50 |
-
print("β οΈ Ignoring non-incoming
|
| 51 |
return {"status": "ignored"}
|
| 52 |
|
| 53 |
-
#
|
| 54 |
if sender_id == account_id:
|
| 55 |
-
print("β οΈ Ignoring
|
| 56 |
return {"status": "ignored"}
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
if sender_role == "agent":
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
return {"status": "bot resumed"}
|
| 65 |
-
else:
|
| 66 |
-
stop_reply_conversations.add(conversation_id)
|
| 67 |
-
print(f"π« Human agent takeover. AI disabled for conversation {conversation_id}")
|
| 68 |
-
return {"status": "human takeover"}
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
if conversation_id in stop_reply_conversations:
|
| 71 |
-
print(f"π«
|
| 72 |
-
return {"status": "ignored:
|
| 73 |
|
|
|
|
| 74 |
if not message_content or not conversation_id:
|
| 75 |
-
print("β Missing
|
| 76 |
return {"status": "invalid payload"}
|
| 77 |
|
| 78 |
-
#
|
| 79 |
messages = [
|
| 80 |
{"role": "system", "content": system_prompt},
|
| 81 |
{"role": "user", "content": message_content},
|
|
@@ -89,17 +87,17 @@ async def ask(request: Request):
|
|
| 89 |
max_tokens=200,
|
| 90 |
)
|
| 91 |
answer = response.choices[0].message.content.strip()
|
| 92 |
-
print("β
|
| 93 |
except Exception as e:
|
| 94 |
print("β OpenAI Error:", e)
|
| 95 |
answer = "Sorry, I'm having trouble answering right now."
|
| 96 |
|
| 97 |
-
|
| 98 |
-
if answer == fallback:
|
| 99 |
stop_reply_conversations.add(conversation_id)
|
| 100 |
-
print(f"π« Fallback
|
| 101 |
|
| 102 |
await send_chatwoot_message(conversation_id, answer)
|
|
|
|
| 103 |
return {"status": "ok"}
|
| 104 |
|
| 105 |
|
|
@@ -111,6 +109,7 @@ async def send_chatwoot_message(conversation_id: str, content: str):
|
|
| 111 |
"content_type": "text",
|
| 112 |
"content_attributes": {}
|
| 113 |
}
|
|
|
|
| 114 |
try:
|
| 115 |
async with httpx.AsyncClient() as http:
|
| 116 |
url = f"{CHATWOOT_BASE_URL}/api/v1/accounts/{CHATWOOT_ACCOUNT_ID}/conversations/{conversation_id}/messages"
|
|
|
|
| 23 |
# Chatwoot config
|
| 24 |
CHATWOOT_BASE_URL = os.getenv("CHATWOOT_BASE_URL")
|
| 25 |
CHATWOOT_API_KEY = os.getenv("CHATWOOT_API_KEY")
|
| 26 |
+
CHATWOOT_ACCOUNT_ID = int(os.getenv("CHATWOOT_ACCOUNT_ID")) # e.g., 123911
|
| 27 |
|
| 28 |
+
# Track conversations where AI should stop replying
|
| 29 |
stop_reply_conversations = set()
|
| 30 |
|
| 31 |
@app.post("/ask")
|
|
|
|
| 33 |
payload = await request.json()
|
| 34 |
print("π₯ Incoming payload:", json.dumps(payload, indent=2))
|
| 35 |
|
|
|
|
| 36 |
account_id = payload.get("account", {}).get("id")
|
|
|
|
| 37 |
conversation_id = payload.get("conversation", {}).get("id")
|
| 38 |
+
sender_id = payload.get("sender", {}).get("id")
|
| 39 |
sender_role = payload.get("sender", {}).get("role", "").lower()
|
| 40 |
+
message_type = payload.get("message_type", "").lower()
|
| 41 |
message_content = payload.get("content", "").strip()
|
| 42 |
|
| 43 |
+
# Ignore non-incoming messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if message_type != "incoming":
|
| 45 |
+
print("β οΈ Ignoring non-incoming message")
|
| 46 |
return {"status": "ignored"}
|
| 47 |
|
| 48 |
+
# Bot must not reply to itself
|
| 49 |
if sender_id == account_id:
|
| 50 |
+
print("β οΈ Ignoring bot's own message")
|
| 51 |
return {"status": "ignored"}
|
| 52 |
|
| 53 |
+
# Handle special bot resume command
|
| 54 |
+
if sender_role == "agent" and message_content.lower() == "/botresume":
|
| 55 |
+
stop_reply_conversations.discard(conversation_id)
|
| 56 |
+
print(f"βΉοΈ Bot resumed for conversation {conversation_id}")
|
| 57 |
+
await send_chatwoot_message(conversation_id, "Bot resumed and will reply to users now.")
|
| 58 |
+
return {"status": "bot resumed"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Stop AI if the message came from any account *other than* web inbox
|
| 61 |
+
if account_id != CHATWOOT_ACCOUNT_ID:
|
| 62 |
+
stop_reply_conversations.add(conversation_id)
|
| 63 |
+
print(f"π« Message from other inbox (account ID {account_id}) β disabling AI for conversation {conversation_id}")
|
| 64 |
+
return {"status": "AI disabled due to human intervention"}
|
| 65 |
+
|
| 66 |
+
# Check if AI is blacklisted for this conversation
|
| 67 |
if conversation_id in stop_reply_conversations:
|
| 68 |
+
print(f"π« AI is disabled for conversation {conversation_id}")
|
| 69 |
+
return {"status": "ignored: human takeover"}
|
| 70 |
|
| 71 |
+
# Ensure all data is present
|
| 72 |
if not message_content or not conversation_id:
|
| 73 |
+
print("β Missing content or conversation ID")
|
| 74 |
return {"status": "invalid payload"}
|
| 75 |
|
| 76 |
+
# Build messages for GPT
|
| 77 |
messages = [
|
| 78 |
{"role": "system", "content": system_prompt},
|
| 79 |
{"role": "user", "content": message_content},
|
|
|
|
| 87 |
max_tokens=200,
|
| 88 |
)
|
| 89 |
answer = response.choices[0].message.content.strip()
|
| 90 |
+
print("β
GPT Answer:", answer)
|
| 91 |
except Exception as e:
|
| 92 |
print("β OpenAI Error:", e)
|
| 93 |
answer = "Sorry, I'm having trouble answering right now."
|
| 94 |
|
| 95 |
+
if answer == "I'm not sure about that. Let me connect you with a human agent.":
|
|
|
|
| 96 |
stop_reply_conversations.add(conversation_id)
|
| 97 |
+
print(f"π« Fallback answer, disabling AI for conversation {conversation_id}")
|
| 98 |
|
| 99 |
await send_chatwoot_message(conversation_id, answer)
|
| 100 |
+
|
| 101 |
return {"status": "ok"}
|
| 102 |
|
| 103 |
|
|
|
|
| 109 |
"content_type": "text",
|
| 110 |
"content_attributes": {}
|
| 111 |
}
|
| 112 |
+
|
| 113 |
try:
|
| 114 |
async with httpx.AsyncClient() as http:
|
| 115 |
url = f"{CHATWOOT_BASE_URL}/api/v1/accounts/{CHATWOOT_ACCOUNT_ID}/conversations/{conversation_id}/messages"
|