Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -223,59 +223,80 @@ async def get_chatwoot_conversation(conversation_id: str):
|
|
| 223 |
def format_slack_message(conversation_data):
|
| 224 |
"""Format conversation data into Slack blocks"""
|
| 225 |
if not conversation_data:
|
| 226 |
-
return {"text": "Error
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
"
|
| 238 |
-
"
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
"type": "section",
|
| 243 |
-
"fields": [
|
| 244 |
-
{
|
| 245 |
-
"type": "mrkdwn",
|
| 246 |
-
"text": f"*Status:* {conv['status']}"
|
| 247 |
-
},
|
| 248 |
-
{
|
| 249 |
-
"type": "mrkdwn",
|
| 250 |
-
"text": f"*Inbox:* {conv['inbox']['name']}"
|
| 251 |
-
},
|
| 252 |
-
{
|
| 253 |
-
"type": "mrkdwn",
|
| 254 |
-
"text": f"*Assignee:* {conv['meta']['assignee']['name'] if conv['meta']['assignee'] else 'Unassigned'}"
|
| 255 |
-
},
|
| 256 |
-
{
|
| 257 |
-
"type": "mrkdwn",
|
| 258 |
-
"text": f"*Created:* {conv['created_at']}"
|
| 259 |
}
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
blocks.append({
|
| 272 |
"type": "section",
|
| 273 |
"text": {
|
| 274 |
"type": "mrkdwn",
|
| 275 |
-
"text": f"{
|
| 276 |
}
|
| 277 |
})
|
| 278 |
blocks.append({"type": "divider"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
return {
|
| 281 |
"response_type": "in_channel",
|
|
|
|
| 223 |
def format_slack_message(conversation_data):
|
| 224 |
"""Format conversation data into Slack blocks"""
|
| 225 |
if not conversation_data:
|
| 226 |
+
return {"response_type": "ephemeral", "text": "β Error: No conversation data received"}
|
| 227 |
|
| 228 |
+
try:
|
| 229 |
+
# Get messages from the payload
|
| 230 |
+
messages = conversation_data.get("payload", [])
|
| 231 |
+
if not messages:
|
| 232 |
+
return {"response_type": "ephemeral", "text": "βΉοΈ No messages found in this conversation"}
|
| 233 |
+
|
| 234 |
+
# Create blocks for the message
|
| 235 |
+
blocks = [
|
| 236 |
+
{
|
| 237 |
+
"type": "header",
|
| 238 |
+
"text": {
|
| 239 |
+
"type": "plain_text",
|
| 240 |
+
"text": f"π¬ Conversation #{messages[0].get('conversation_id', 'N/A')}",
|
| 241 |
+
"emoji": True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
}
|
| 243 |
+
},
|
| 244 |
+
{
|
| 245 |
+
"type": "divider"
|
| 246 |
+
}
|
| 247 |
+
]
|
| 248 |
+
|
| 249 |
+
# Add messages
|
| 250 |
+
for msg in sorted(messages, key=lambda x: x.get('created_at', 0)):
|
| 251 |
+
content = msg.get("content", "")
|
| 252 |
+
if not content or not isinstance(content, str):
|
| 253 |
+
continue
|
| 254 |
+
|
| 255 |
+
# Determine sender info
|
| 256 |
+
sender_data = msg.get("sender", {})
|
| 257 |
+
if msg.get("message_type") == 0: # incoming message
|
| 258 |
+
sender_name = sender_data.get("name", "User")
|
| 259 |
+
sender_emoji = "π€"
|
| 260 |
+
else: # outgoing message
|
| 261 |
+
sender_name = sender_data.get("available_name", sender_data.get("name", "Bot"))
|
| 262 |
+
sender_emoji = "π€"
|
| 263 |
+
|
| 264 |
+
# Format timestamp if available
|
| 265 |
+
timestamp = msg.get("created_at")
|
| 266 |
+
if timestamp and isinstance(timestamp, (int, float)):
|
| 267 |
+
from datetime import datetime
|
| 268 |
+
try:
|
| 269 |
+
dt = datetime.fromtimestamp(timestamp)
|
| 270 |
+
time_str = dt.strftime("%b %d, %H:%M")
|
| 271 |
+
time_display = f"`{time_str}`"
|
| 272 |
+
except:
|
| 273 |
+
time_display = ""
|
| 274 |
+
else:
|
| 275 |
+
time_display = ""
|
| 276 |
+
|
| 277 |
+
# Add message block
|
| 278 |
blocks.append({
|
| 279 |
"type": "section",
|
| 280 |
"text": {
|
| 281 |
"type": "mrkdwn",
|
| 282 |
+
"text": f"{sender_emoji} *{sender_name}* {time_display}\n{content}"
|
| 283 |
}
|
| 284 |
})
|
| 285 |
blocks.append({"type": "divider"})
|
| 286 |
+
|
| 287 |
+
return {
|
| 288 |
+
"response_type": "in_channel",
|
| 289 |
+
"blocks": blocks
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
except Exception as e:
|
| 293 |
+
import traceback
|
| 294 |
+
print(f"Error formatting Slack message: {e}")
|
| 295 |
+
print(traceback.format_exc())
|
| 296 |
+
return {
|
| 297 |
+
"response_type": "ephemeral",
|
| 298 |
+
"text": f"β Error formatting conversation: {str(e)}"
|
| 299 |
+
}
|
| 300 |
|
| 301 |
return {
|
| 302 |
"response_type": "in_channel",
|