Chandima Prabhath commited on
Commit
56fb4f0
·
1 Parent(s): f0fa946

Enhance webhook handling: ignore replies and mentions, and set PORT directly in the run command

Browse files
Files changed (1) hide show
  1. app.py +7 -3
app.py CHANGED
@@ -18,7 +18,6 @@ GREEN_API_MEDIA_URL = os.getenv("GREEN_API_MEDIA_URL", "https://api.green-api.co
18
  GREEN_API_TOKEN = os.getenv("GREEN_API_TOKEN")
19
  GREEN_API_ID_INSTANCE = os.getenv("GREEN_API_ID_INSTANCE")
20
  WEBHOOK_AUTH_TOKEN = os.getenv("WEBHOOK_AUTH_TOKEN")
21
- PORT = 7860
22
  image_dir = "/tmp/images"
23
  audio_dir = "/tmp/audio"
24
 
@@ -193,12 +192,17 @@ async def whatsapp_webhook(request: Request):
193
  except KeyError as e:
194
  return JSONResponse(content={"error": f"Missing key in data: {e}"}, status_code=200)
195
 
196
- # Ignore replies between other users
197
  if 'extendedTextMessageData' in message_data:
198
  ctx = message_data['extendedTextMessageData'].get('contextInfo', {})
 
199
  if ctx.get('quotedMessageId'):
200
  logging.debug(f"Ignoring reply message (quotedMessageId={ctx['quotedMessageId']})")
201
  return {"success": True}
 
 
 
 
202
 
203
  # Enqueue tasks instead of spawning threads
204
  if body.lower().startswith('/imagine'):
@@ -215,4 +219,4 @@ async def whatsapp_webhook(request: Request):
215
 
216
  if __name__ == '__main__':
217
  import uvicorn
218
- uvicorn.run(app, host="0.0.0.0", port=PORT)
 
18
  GREEN_API_TOKEN = os.getenv("GREEN_API_TOKEN")
19
  GREEN_API_ID_INSTANCE = os.getenv("GREEN_API_ID_INSTANCE")
20
  WEBHOOK_AUTH_TOKEN = os.getenv("WEBHOOK_AUTH_TOKEN")
 
21
  image_dir = "/tmp/images"
22
  audio_dir = "/tmp/audio"
23
 
 
192
  except KeyError as e:
193
  return JSONResponse(content={"error": f"Missing key in data: {e}"}, status_code=200)
194
 
195
+ # Ignore replies and mentions
196
  if 'extendedTextMessageData' in message_data:
197
  ctx = message_data['extendedTextMessageData'].get('contextInfo', {})
198
+ # ignore replies
199
  if ctx.get('quotedMessageId'):
200
  logging.debug(f"Ignoring reply message (quotedMessageId={ctx['quotedMessageId']})")
201
  return {"success": True}
202
+ # ignore mentions
203
+ if ctx.get('mentionedJidList'):
204
+ logging.debug(f"Ignoring mention message (mentionedJidList={ctx['mentionedJidList']})")
205
+ return {"success": True}
206
 
207
  # Enqueue tasks instead of spawning threads
208
  if body.lower().startswith('/imagine'):
 
219
 
220
  if __name__ == '__main__':
221
  import uvicorn
222
+ uvicorn.run(app, host="0.0.0.0", port=7860)