Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions waba_integration/api/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,44 @@

@frappe.whitelist(allow_guest=True)
def handle():

if frappe.request.method == "GET":
return verify_token_and_fulfill_challenge()

try:
form_dict = frappe.local.form_dict
frappe.get_doc(
{"doctype": "WABA Webhook Log", "payload": frappe.as_json(form_dict)}
).insert(ignore_permissions=True)

messages = form_dict["entry"][0]["changes"][0]["value"].get("messages", [])
statuses = form_dict["entry"][0]["changes"][0]["value"].get("statuses", [])

for status in statuses:
process_status_update(status)

for message in messages:
create_waba_whatsapp_message(message)

# find if a WABA WhatsApp Message with Message ID exists in the system
if frappe.db.exists("WABA WhatsApp Message", {"id": message.get("id")}): # if already exists, then skip the message
continue
elif message.get("errors") and message.get("errors")[0].get("code") == 131051: # if got an error code 131051, then skip the message
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this magic error code?

continue
elif message.get("type") == 'reaction': # if got a reaction, then skip the message
continue
elif message.get("type") not in ['text', 'image', 'audio', 'video', 'system', 'document']: # if any unsupported message type, then skip the message
frappe.log_error("Log From IDML,Ignoring this Message Because Unsupported Message Type", frappe.as_json(message))
continue
else: # else create a new WABA WhatsApp Message
create_waba_whatsapp_message(message)


except Exception:
form_dict = frappe.local.form_dict
frappe.get_doc(
{"doctype": "WABA Webhook Log", "payload": frappe.as_json(form_dict)}
).insert(ignore_permissions=True)
except Exception:
frappe.log_error("WABA Webhook Log Error", frappe.get_traceback())
frappe.log_error("Log From IDML", frappe.as_json(form_dict))
frappe.throw("Something went wrong")


Expand Down