-
Notifications
You must be signed in to change notification settings - Fork 230
Open
Labels
Description
I have two two site with Frappe Whatsapp Installed, With different Phone Number Connected,
Webhook url domain i have like this
- https://domain.in
- https://chat.domain.in (Sub Domain)
When Meta Webhook Requested, Frappe Whatsapp Adding the Message in Both Site, Because of its notchecking the Phone ID in Webhook Request
Solution
def post():
"""Post."""
data = frappe.local.form_dict
frappe.get_doc({
"doctype": "WhatsApp Notification Log",
"template": "Webhook",
"meta_data": json.dumps(data)
}).insert(ignore_permissions=True)
> # Extract incoming phone number ID from the webhook payload
> incoming_phone_id = None
>
> if data.get("entry") and isinstance(data["entry"], list):
> incoming_phone_id = data["entry"][0]["changes"][0]["value"].get("metadata", {}).get("phone_number_id")
> elif data.get("entry"):
> incoming_phone_id = data["entry"]["changes"][0]["value"].get("metadata", {}).get("phone_number_id")
>
> # If no phone_id in metadata, try to get it from other locations
> if not incoming_phone_id:
> if data.get("entry") and isinstance(data["entry"], list) and data["entry"][0].get("changes", []):
> value = data["entry"][0]["changes"][0].get("value", {})
> if value.get("messages") and value["messages"]:
> incoming_phone_id = value.get("phone_number_id")
>
> # Get this site's WhatsApp phone ID
> site_phone_id = frappe.db.get_single_value(
> "WhatsApp Settings", "phone_id"
> )
>
> # If phone IDs don't match, log and return early with 200 OK
> if incoming_phone_id and incoming_phone_id != site_phone_id:
> return Response("OK", status=200)
>
> messages = []
> try:
> messages = data["entry"][0]["changes"][0]["value"].get("messages", [])
> except KeyError:
> messages = data["entry"]["changes"][0]["value"].get("messages", [])
> sender_profile_name = next(
> (
> contact.get("profile", {}).get("name")
> for entry in data.get("entry", [])
> for change in entry.get("changes", [])
> for contact in change.get("value", {}).get("contacts", [])
> ),
> None,
> )
>
if messages:
for message in messages:
message_type = message['type']
is_reply = True if (message.get('context') and 'id' in message.get('context', {})) else False
reply_to_message_id = message['context']['id'] if is_reply else None
if message_type == 'text':
frappe.get_doc({
"doctype": "WhatsApp Message",
"type": "Incoming",
.................................... Continue Code