After insert hook not working

HI EVERYONE ,

IAM DOING THIS HEALTHCARE PROJECT. I HAVE DONE A CODE TO SEND A WHATSAPP MESSAGE TO PATIENT WHENEVER THEY ARE REGISTERED . THE CODING IS WORKING FINE IN MY LOCAL ENVIRONMENT BUT NOT AT ALL WORKING IN PRODUCTION. I CANT FIND ANY ERRORS IN LOGS AS WELL. PLEASE HAVE A LOOK.

#PT REG WHATSAPP MESSAGE

import requests
def send_patient_registration_message(doc, method):
mobile = doc.custom_whatsapp_number
user = doc.patient_name
if not mobile:
frappe.log_error(“No mobile number for patient”, “WhatsApp Error”)
return
payload = {
“apiKey”: “===APIKEY===”,
“campaignName”: “patient_registration”,
“destination”: mobile,
“userName”: user,
“templateParams”: [
doc.name
    ]

}
try:
res = requests.post(
“https://backend.aisensy.com/campaign/t1/api/v2”,
json=payload,
headers={“Content-Type”: “application/json”}
    )
frappe.log_error(
f"Payload: {payload}\nResponse: {res.text}",
“AiSensy WhatsApp Response”
    )
except Exception:
frappe.log_error(
frappe.get_traceback(),
“AiSensy WhatsApp Failed”
    ) 
doc_events = {“Patient” : {
    "after_insert": 
“wellness.utils.send_patient_registration_message”
},
}

@Ashique Please note, after insert will not do auto commit, so you will not find logs if you are adding it in after insert.

@kolate_sambhaji
Thanks for the response.
how can we debug the actual issue here

One more thing, if you change anything in hooks.py then it will not reflect immediately, you need to do bench restart or supervisor restart
On development instance you can add few print statement and debug, and add frappe.db.commit() for quick fix

Edit:Also you can use better approach like background job for sending notification and see background job log

def send_patient_registration_message(doc, method):
    """Queue WhatsApp sending in background"""
    
    # Quick validation
    if not doc.custom_whatsapp_number:
        return
    
    # Enqueue background job
    frappe.enqueue(
        method="your_app.api.whatsapp.send_whatsapp_background",
        queue="short",
        timeout=30,
        patient_id=doc.name,
        patient_name=doc.patient_name,
        mobile=doc.custom_whatsapp_number
    )
    # Returns immediately - document saves without waiting


def send_whatsapp_background(patient_id, patient_name, mobile):
    """Background job to send WhatsApp"""
    
    # Clean mobile
    mobile = mobile.replace(" ", "").replace("-", "").strip()
    
    # Validate
    if not mobile.isdigit() or len(mobile) < 10:
        frappe.log_error(
            f"Invalid mobile for {patient_id}: {mobile}",
            "WhatsApp - Invalid Mobile"
        )
        return
    
    try:
        api_key = frappe.db.get_single_value("AiSensy Settings", "api_key") or "===APIKEY==="
        
        payload = {
            "apiKey": api_key,
            "campaignName": "patient_registration",
            "destination": mobile,
            "userName": patient_name,
            "templateParams": [patient_id]
        }
        
        response = requests.post(
            "https://backend.aisensy.com/campaign/t1/api/v2",
            json=payload,
            headers={"Content-Type": "application/json"},
            timeout=10
        )
        
        # Log result
        if response.status_code == 200:
            frappe.logger().info(f"WhatsApp sent to {mobile} for {patient_id}")
        else:
            frappe.log_error(
                f"Status {response.status_code}: {response.text}",
                f"WhatsApp Failed - {patient_id}"
            )
            
    except Exception as e:
        frappe.log_error(
            f"Patient: {patient_id}\nMobile: {mobile}\n{frappe.get_traceback()}",
            "WhatsApp Send Failed"
        )

Thank you
let me try this and get back to you