Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -25,23 +25,31 @@ CHATWOOT_BASE_URL = os.getenv("CHATWOOT_BASE_URL") # e.g., https://app
|
|
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):
|
30 |
# payload = await request.json()
|
31 |
# print("📥 Incoming payload:", json.dumps(payload, indent=2))
|
32 |
|
33 |
# message_type = payload.get("message_type", "").lower()
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
#
|
38 |
-
#
|
|
|
39 |
# return {"status": "ignored"}
|
40 |
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
#
|
|
|
43 |
# conversation_id = payload.get("conversation", {}).get("id")
|
44 |
-
|
45 |
# if not user_question or not conversation_id or not CHATWOOT_ACCOUNT_ID:
|
46 |
# print("❌ Missing message, conversation ID, or account ID")
|
47 |
# return {"status": "invalid payload"}
|
@@ -64,7 +72,6 @@ CHATWOOT_ACCOUNT_ID = os.getenv("CHATWOOT_ACCOUNT_ID") # Account ID (integ
|
|
64 |
# print("❌ OpenAI Error:", e)
|
65 |
# answer = "Sorry, I'm having trouble answering right now."
|
66 |
|
67 |
-
# # Prepare Chatwoot message payload
|
68 |
# message_payload = {
|
69 |
# "content": answer,
|
70 |
# "message_type": "outgoing",
|
@@ -100,22 +107,26 @@ async def ask(request: Request):
|
|
100 |
print("📥 Incoming payload:", json.dumps(payload, indent=2))
|
101 |
|
102 |
message_type = payload.get("message_type", "").lower()
|
103 |
-
account_id = payload.get("account", {}).get("id")
|
104 |
-
sender_id = payload.get("sender", {}).get("id")
|
|
|
105 |
|
106 |
# Only respond to incoming messages
|
107 |
if message_type != "incoming":
|
108 |
print("⚠️ Ignoring non-incoming messages")
|
109 |
return {"status": "ignored"}
|
110 |
|
111 |
-
# Ignore messages
|
112 |
if sender_id == account_id:
|
113 |
-
print("⚠️ Ignoring message from bot itself
|
|
|
|
|
|
|
|
|
|
|
114 |
return {"status": "ignored"}
|
115 |
|
116 |
-
# Extract user question and conversation id
|
117 |
user_question = payload.get("content", "")
|
118 |
-
conversation_id = payload.get("conversation", {}).get("id")
|
119 |
if not user_question or not conversation_id or not CHATWOOT_ACCOUNT_ID:
|
120 |
print("❌ Missing message, conversation ID, or account ID")
|
121 |
return {"status": "invalid payload"}
|
@@ -138,6 +149,11 @@ async def ask(request: Request):
|
|
138 |
print("❌ OpenAI Error:", e)
|
139 |
answer = "Sorry, I'm having trouble answering right now."
|
140 |
|
|
|
|
|
|
|
|
|
|
|
141 |
message_payload = {
|
142 |
"content": answer,
|
143 |
"message_type": "outgoing",
|
|
|
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 |
+
# Add at the top after imports:
|
29 |
+
stop_reply_conversations = set()
|
30 |
+
|
31 |
# @app.post("/ask")
|
32 |
# 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") # bot's account id
|
38 |
+
# sender_id = payload.get("sender", {}).get("id") # sender id of this message
|
39 |
+
|
40 |
+
# # Only respond to incoming messages
|
41 |
+
# if message_type != "incoming":
|
42 |
+
# print("⚠️ Ignoring non-incoming messages")
|
43 |
# return {"status": "ignored"}
|
44 |
|
45 |
+
# # Ignore messages sent by bot itself (to avoid loops)
|
46 |
+
# if sender_id == account_id:
|
47 |
+
# print("⚠️ Ignoring message from bot itself to prevent loop.")
|
48 |
+
# return {"status": "ignored"}
|
49 |
|
50 |
+
# # Extract user question and conversation id
|
51 |
+
# user_question = payload.get("content", "")
|
52 |
# conversation_id = payload.get("conversation", {}).get("id")
|
|
|
53 |
# if not user_question or not conversation_id or not CHATWOOT_ACCOUNT_ID:
|
54 |
# print("❌ Missing message, conversation ID, or account ID")
|
55 |
# return {"status": "invalid payload"}
|
|
|
72 |
# print("❌ OpenAI Error:", e)
|
73 |
# answer = "Sorry, I'm having trouble answering right now."
|
74 |
|
|
|
75 |
# message_payload = {
|
76 |
# "content": answer,
|
77 |
# "message_type": "outgoing",
|
|
|
107 |
print("📥 Incoming payload:", json.dumps(payload, indent=2))
|
108 |
|
109 |
message_type = payload.get("message_type", "").lower()
|
110 |
+
account_id = payload.get("account", {}).get("id")
|
111 |
+
sender_id = payload.get("sender", {}).get("id")
|
112 |
+
conversation_id = payload.get("conversation", {}).get("id")
|
113 |
|
114 |
# Only respond to incoming messages
|
115 |
if message_type != "incoming":
|
116 |
print("⚠️ Ignoring non-incoming messages")
|
117 |
return {"status": "ignored"}
|
118 |
|
119 |
+
# Ignore messages from bot itself to prevent loops
|
120 |
if sender_id == account_id:
|
121 |
+
print("⚠️ Ignoring message from bot itself")
|
122 |
+
return {"status": "ignored"}
|
123 |
+
|
124 |
+
# If conversation is blacklisted (bot should stop replying)
|
125 |
+
if conversation_id in stop_reply_conversations:
|
126 |
+
print(f"⚠️ Conversation {conversation_id} is blacklisted, no reply sent.")
|
127 |
return {"status": "ignored"}
|
128 |
|
|
|
129 |
user_question = payload.get("content", "")
|
|
|
130 |
if not user_question or not conversation_id or not CHATWOOT_ACCOUNT_ID:
|
131 |
print("❌ Missing message, conversation ID, or account ID")
|
132 |
return {"status": "invalid payload"}
|
|
|
149 |
print("❌ OpenAI Error:", e)
|
150 |
answer = "Sorry, I'm having trouble answering right now."
|
151 |
|
152 |
+
# If answer is "I'm not sure about that..." message, blacklist this conversation
|
153 |
+
if answer == "I'm not sure about that. Let me connect you with a human agent.":
|
154 |
+
stop_reply_conversations.add(conversation_id)
|
155 |
+
print(f"⚠️ Added conversation {conversation_id} to stop reply blacklist.")
|
156 |
+
|
157 |
message_payload = {
|
158 |
"content": answer,
|
159 |
"message_type": "outgoing",
|