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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -190,32 +190,29 @@ async def send_to_slack(message: str):
190
  except Exception as e:
191
  print("❌ Slack Send Error:", e)
192
 
193
- async def get_chatwoot_conversation(conversation_id: str):
194
- """Fetch conversation details from Chatwoot"""
195
  try:
196
- async with httpx.AsyncClient() as http:
197
- # Get conversation details
198
- conv_url = f"{CHATWOOT_BASE_URL}/api/v1/accounts/{CHATWOOT_ACCOUNT_ID}/conversations/{conversation_id}"
199
- conv_resp = await http.get(
200
- conv_url,
201
- headers={"api_access_token": CHATWOOT_API_KEY}
202
- )
203
- conv_resp.raise_for_status()
204
- conversation = conv_resp.json()
205
-
206
- # Get conversation messages
207
  msgs_url = f"{CHATWOOT_BASE_URL}/api/v1/accounts/{CHATWOOT_ACCOUNT_ID}/conversations/{conversation_id}/messages"
208
- msgs_resp = await http.get(
209
- msgs_url,
210
- headers={"api_access_token": CHATWOOT_API_KEY}
211
- )
212
  msgs_resp.raise_for_status()
 
 
213
  messages = msgs_resp.json()
 
214
 
215
- return {
216
- "conversation": conversation,
217
- "messages": messages
218
- }
219
  except Exception as e:
220
  print(f"❌ Error fetching Chatwoot conversation: {e}")
221
  return None
 
190
  except Exception as e:
191
  print("❌ Slack Send Error:", e)
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"Fetching messages from: {msgs_url}")
206
+
207
+ msgs_resp = await http.get(msgs_url, headers=headers)
 
208
  msgs_resp.raise_for_status()
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
+ # Return the messages in the expected format
215
+ return {"payload": messages}
 
 
216
  except Exception as e:
217
  print(f"❌ Error fetching Chatwoot conversation: {e}")
218
  return None