ethiotech4848 commited on
Commit
bcf43e9
Β·
verified Β·
1 Parent(s): 3668129

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -44
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 fetching conversation"}
227
 
228
- conv = conversation_data["conversation"]
229
- messages = conversation_data["messages"]
230
-
231
- # Create blocks for the message
232
- blocks = [
233
- {
234
- "type": "header",
235
- "text": {
236
- "type": "plain_text",
237
- "text": f"πŸ’¬ Conversation #{conv['id']}",
238
- "emoji": True
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
- "type": "divider"
264
- }
265
- ]
266
-
267
- # Add messages
268
- for msg in sorted(messages, key=lambda x: x['created_at']):
269
- if msg["message_type"] in ["incoming", "outgoing"] and msg["content"].strip():
270
- sender = "πŸ‘€" if msg["message_type"] == "incoming" else "πŸ€–"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  blocks.append({
272
  "type": "section",
273
  "text": {
274
  "type": "mrkdwn",
275
- "text": f"{sender} *{msg['sender']['available_name']}*\n{msg['content']}"
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",