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.
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"
)