Chandima Prabhath commited on
Commit
8f336a6
·
1 Parent(s): 458794e

Refactor BotConfig to clarify BOT_JID comment; enhance quoted message handling in whatsapp_webhook for improved user reply processing.

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -22,7 +22,7 @@ class BotConfig:
22
  GREEN_API_ID_INSTANCE = os.getenv("GREEN_API_ID_INSTANCE")
23
  WEBHOOK_AUTH_TOKEN = os.getenv("WEBHOOK_AUTH_TOKEN")
24
  BOT_GROUP_CHAT = "[email protected]"
25
- BOT_JID = os.getenv("BOT_JID") # e.g. "[email protected]"
26
  IMAGE_DIR = "/tmp/images"
27
  AUDIO_DIR = "/tmp/audio"
28
  DEFAULT_IMAGE_COUNT = 4
@@ -212,44 +212,39 @@ async def whatsapp_webhook(request: Request):
212
  if chat_id != BotConfig.BOT_GROUP_CHAT or data.get("typeWebhook") != "incomingMessageReceived":
213
  return {"success": True}
214
 
215
- md = data.get("messageData", {})
216
- mid = data["idMessage"]
217
- text_data = md.get("textMessageData") or md.get("extendedTextMessageData")
218
- if not text_data:
219
- return {"success": True}
220
-
221
- body = text_data.get("textMessage", text_data.get("text", "")).strip()
222
- ctx = text_data.get("contextInfo", {})
223
 
224
  # --- Quoted‑reply handling ---
225
- if ctx.get("quotedMessageId"):
226
- # only if the quoted message was sent by our bot
227
- quoted_sender = ctx.get("participant") or ctx.get("quotedMessageSender")
228
- if quoted_sender == BotConfig.BOT_JID:
229
- # extract quoted text
230
- qm = ctx.get("quotedMessage", {})
231
- if "textMessageData" in qm:
232
- quoted_text = qm["textMessageData"].get("textMessage", "")
233
- else:
234
- quoted_text = qm.get("extendedTextMessageData", {}).get("text", "")
235
- # build and send LLM reply
236
  prompt = (
237
  f"You asked: {quoted_text}\n"
238
- f"User replied: {body}\n"
239
  "Provide a helpful, concise follow‑up."
240
  )
241
  reply = generate_llm(prompt)
242
  client.send_message(mid, chat_id, reply)
243
- # in either case, do not process further
244
  return {"success": True}
245
 
246
- # --- Skip @‑mentions of others ---
 
 
 
 
 
247
  if ctx.get("mentionedJidList"):
248
  return {"success": True}
249
 
250
  low = body.lower()
251
 
252
- # --- COMMANDS (unchanged) ---
253
  if low == "/help":
254
  client.send_message(mid, chat_id, help_text)
255
  return {"success": True}
 
22
  GREEN_API_ID_INSTANCE = os.getenv("GREEN_API_ID_INSTANCE")
23
  WEBHOOK_AUTH_TOKEN = os.getenv("WEBHOOK_AUTH_TOKEN")
24
  BOT_GROUP_CHAT = "[email protected]"
25
+ BOT_JID = os.getenv("BOT_JID") # your bot's own WhatsApp JID
26
  IMAGE_DIR = "/tmp/images"
27
  AUDIO_DIR = "/tmp/audio"
28
  DEFAULT_IMAGE_COUNT = 4
 
212
  if chat_id != BotConfig.BOT_GROUP_CHAT or data.get("typeWebhook") != "incomingMessageReceived":
213
  return {"success": True}
214
 
215
+ md = data.get("messageData", {})
216
+ mid = data["idMessage"]
 
 
 
 
 
 
217
 
218
  # --- Quoted‑reply handling ---
219
+ if md.get("typeMessage") == "quotedMessage":
220
+ ext = md.get("extendedTextMessageData", {})
221
+ quoted = md.get("quotedMessage", {})
222
+ quoted_participant = ext.get("participant")
223
+ # only if the quoted message was from the bot
224
+ if quoted_participant == BotConfig.BOT_JID:
225
+ user_reply = ext.get("text", "")
226
+ quoted_text = quoted.get("textMessage", "")
 
 
 
227
  prompt = (
228
  f"You asked: {quoted_text}\n"
229
+ f"User replied: {user_reply}\n"
230
  "Provide a helpful, concise follow‑up."
231
  )
232
  reply = generate_llm(prompt)
233
  client.send_message(mid, chat_id, reply)
 
234
  return {"success": True}
235
 
236
+ # --- Extract normal text + skip mentions ---
237
+ text_data = md.get("textMessageData") or md.get("extendedTextMessageData")
238
+ if not text_data:
239
+ return {"success": True}
240
+ body = text_data.get("textMessage", text_data.get("text", "")).strip()
241
+ ctx = text_data.get("contextInfo", {})
242
  if ctx.get("mentionedJidList"):
243
  return {"success": True}
244
 
245
  low = body.lower()
246
 
247
+ # --- COMMANDS ---
248
  if low == "/help":
249
  client.send_message(mid, chat_id, help_text)
250
  return {"success": True}