Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -192,29 +192,63 @@ async def send_to_slack(message: str):
|
|
192 |
|
193 |
async def get_chatwoot_conversation(conversation_id: int) -> Optional[dict]:
|
194 |
"""Fetch conversation details and messages from Chatwoot"""
|
|
|
|
|
|
|
|
|
195 |
try:
|
196 |
headers = {
|
197 |
-
"api_access_token": CHATWOOT_API_KEY,
|
198 |
"Content-Type": "application/json",
|
199 |
"Accept": "application/json"
|
200 |
}
|
|
|
201 |
|
202 |
async with httpx.AsyncClient(timeout=30.0) as http:
|
203 |
# Get conversation messages directly since that's what we need
|
204 |
msgs_url = f"{CHATWOOT_BASE_URL}/api/v1/accounts/{CHATWOOT_ACCOUNT_ID}/conversations/{conversation_id}/messages"
|
205 |
-
print(f"
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
# The API returns the payload directly as an array of messages
|
211 |
-
messages = msgs_resp.json()
|
212 |
-
print(f"Fetched {len(messages) if messages else 0} messages")
|
213 |
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
except Exception as e:
|
217 |
-
|
|
|
|
|
|
|
218 |
return None
|
219 |
|
220 |
def format_slack_message(conversation_data):
|
|
|
192 |
|
193 |
async def get_chatwoot_conversation(conversation_id: int) -> Optional[dict]:
|
194 |
"""Fetch conversation details and messages from Chatwoot"""
|
195 |
+
print(f"\n🔍 Starting to fetch conversation {conversation_id}")
|
196 |
+
print(f"Using base URL: {CHATWOOT_BASE_URL}")
|
197 |
+
print(f"Account ID: {CHATWOOT_ACCOUNT_ID}")
|
198 |
+
|
199 |
try:
|
200 |
headers = {
|
201 |
+
"api_access_token": CHATWOOT_API_KEY[:5] + "..." if CHATWOOT_API_KEY else "None", # Log first 5 chars of token for security
|
202 |
"Content-Type": "application/json",
|
203 |
"Accept": "application/json"
|
204 |
}
|
205 |
+
print(f"Request headers: {headers}")
|
206 |
|
207 |
async with httpx.AsyncClient(timeout=30.0) as http:
|
208 |
# Get conversation messages directly since that's what we need
|
209 |
msgs_url = f"{CHATWOOT_BASE_URL}/api/v1/accounts/{CHATWOOT_ACCOUNT_ID}/conversations/{conversation_id}/messages"
|
210 |
+
print(f"\n🌐 Making request to: {msgs_url}")
|
211 |
+
print(f"Method: GET")
|
212 |
+
print(f"Full URL: {msgs_url}")
|
213 |
+
print(f"Headers: {headers}")
|
|
|
|
|
|
|
|
|
214 |
|
215 |
+
try:
|
216 |
+
msgs_resp = await http.get(msgs_url, headers=headers)
|
217 |
+
print(f"\n✅ Response received")
|
218 |
+
print(f"Status code: {msgs_resp.status_code}")
|
219 |
+
print(f"Response headers: {dict(msgs_resp.headers)}")
|
220 |
+
|
221 |
+
# Log response body (truncated if too long)
|
222 |
+
response_text = msgs_resp.text
|
223 |
+
print(f"Response length: {len(response_text)} characters")
|
224 |
+
print(f"First 500 chars of response: {response_text[:500]}...")
|
225 |
+
|
226 |
+
msgs_resp.raise_for_status()
|
227 |
+
|
228 |
+
# The API returns the payload directly as an array of messages
|
229 |
+
messages = msgs_resp.json()
|
230 |
+
if not isinstance(messages, list):
|
231 |
+
print(f"⚠️ Unexpected response format. Expected list, got: {type(messages)}")
|
232 |
+
print(f"Response content: {messages}")
|
233 |
+
return None
|
234 |
+
|
235 |
+
print(f"\n📩 Successfully parsed {len(messages)} messages")
|
236 |
+
if messages:
|
237 |
+
print(f"First message: {messages[0].get('content', 'No content')[:100]}...")
|
238 |
+
|
239 |
+
# Return the messages in the expected format
|
240 |
+
return {"payload": messages}
|
241 |
+
|
242 |
+
except httpx.HTTPStatusError as e:
|
243 |
+
print(f"\n❌ HTTP Error: {e}")
|
244 |
+
print(f"Response: {e.response.text}")
|
245 |
+
return None
|
246 |
+
|
247 |
except Exception as e:
|
248 |
+
import traceback
|
249 |
+
print(f"\n❌ Unexpected error: {str(e)}")
|
250 |
+
print("Stack trace:")
|
251 |
+
print(traceback.format_exc())
|
252 |
return None
|
253 |
|
254 |
def format_slack_message(conversation_data):
|