Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -225,19 +225,41 @@ async def get_chatwoot_conversation(conversation_id: int) -> Optional[dict]:
|
|
| 225 |
|
| 226 |
msgs_resp.raise_for_status()
|
| 227 |
|
| 228 |
-
# The API returns the payload
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
| 230 |
if not isinstance(messages, list):
|
| 231 |
-
print(f"⚠️ Unexpected
|
| 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 |
-
#
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
except httpx.HTTPStatusError as e:
|
| 243 |
print(f"\n❌ HTTP Error: {e}")
|
|
|
|
| 225 |
|
| 226 |
msgs_resp.raise_for_status()
|
| 227 |
|
| 228 |
+
# The API returns the payload with meta and messages
|
| 229 |
+
response_data = msgs_resp.json()
|
| 230 |
+
|
| 231 |
+
# Extract messages from the response
|
| 232 |
+
messages = response_data.get('payload', [])
|
| 233 |
if not isinstance(messages, list):
|
| 234 |
+
print(f"⚠️ Unexpected messages format. Expected list, got: {type(messages)}")
|
|
|
|
| 235 |
return None
|
| 236 |
|
| 237 |
print(f"\n📩 Successfully parsed {len(messages)} messages")
|
| 238 |
if messages:
|
| 239 |
print(f"First message: {messages[0].get('content', 'No content')[:100]}...")
|
| 240 |
|
| 241 |
+
# Filter out system messages and format for Slack
|
| 242 |
+
filtered_messages = []
|
| 243 |
+
for msg in messages:
|
| 244 |
+
# Only include messages with content and from known senders
|
| 245 |
+
if msg.get('content') and msg.get('sender') and msg.get('sender').get('type') in ['contact', 'user']:
|
| 246 |
+
filtered_msg = {
|
| 247 |
+
'id': msg.get('id'),
|
| 248 |
+
'content': msg.get('content'),
|
| 249 |
+
'created_at': msg.get('created_at'),
|
| 250 |
+
'sender': {
|
| 251 |
+
'type': msg.get('sender', {}).get('type'),
|
| 252 |
+
'name': msg.get('sender', {}).get('name', 'Unknown')
|
| 253 |
+
},
|
| 254 |
+
'message_type': msg.get('message_type')
|
| 255 |
+
}
|
| 256 |
+
filtered_messages.append(filtered_msg)
|
| 257 |
+
|
| 258 |
+
# Return the filtered messages with metadata
|
| 259 |
+
return {
|
| 260 |
+
'meta': response_data.get('meta', {}),
|
| 261 |
+
'payload': filtered_messages
|
| 262 |
+
}
|
| 263 |
|
| 264 |
except httpx.HTTPStatusError as e:
|
| 265 |
print(f"\n❌ HTTP Error: {e}")
|