Closed ahassoun closed 4 months ago
+1
@ahassoun On click of button you can call an api with below snippet In you python code you can add the below snippet
doc = frappe.get_doc("<doctype name>", "<name>")
frappe.get_doc(
"WhatsApp Notification",
notification_name
).send_template_message(doc)
@ahassoun On click of button you can call an api with below snippet In you python code you can add the below snippet
doc = frappe.get_doc("<doctype name>", "<name>") frappe.get_doc( "WhatsApp Notification", notification_name ).send_template_message(doc)
Thanks, worked perfectly.
@ahassoun can you share me code to which doctype you implemented? Complete code from button creation to trigger.
You can not directly call a Python method from the client script.
Here's how you can properly trigger a server-side method to send a WhatsApp message from a custom button in the "Sales Invoice" DocType:
name
isn't defined in your function. You should use frm.doc.name
to get the name of the current document.frappe.call
to make an asynchronous call to a server-side method. You cannot directly call server-side methods like send_scheduled_message
from the client-side without this.Here's a corrected version of the client-side script:
frappe.ui.form.on('Sales Invoice', {
refresh(frm) {
frm.add_custom_button(__('Send Invoice'), function() {
// Use frm.doc.name to get the current document's name
frappe.call({
method: 'your_app_path.your_module_name.your_method', // Specify the correct path to your server-side method
args: {
docname: frm.doc.name,
notification_name: 'WN-0001' // Assuming 'WN-0001' is the name of your WhatsApp Notification DocType instance
},
callback: function(r) {
if (!r.exc) {
// Handle successful message sending here, e.g., show a message to the user
frappe.msgprint(__('WhatsApp message sent successfully.'));
}
}
});
}, __("Send WhatsApp Alert"));
}
});
You'll need to implement a server-side method that the client-side script can call. This method will retrieve the WhatsApp Notification
document and then call its send_scheduled_message
method with the appropriate arguments.
Here's an example of what the server-side method could look like:
# Example server-side method in your custom app
import frappe
from frappe.core.doctype.whatsapp_notification.whatsapp_notification import WhatsAppNotification
@frappe.whitelist()
def send_whatsapp_invoice(docname, notification_name):
try:
whatsapp_notification = frappe.get_doc('WhatsApp Notification', notification_name)
doc = frappe.get_doc('Sales Invoice', docname)
whatsapp_notification.send_scheduled_message(doc)
return {'status': 'success'}
except Exception as e:
frappe.log_error(frappe.get_traceback(), 'send_whatsapp_invoice Error')
return {'status': 'error', 'error': str(e)}
Make sure to replace 'your_app_path.your_module_name.your_method'
in the client-side script with the actual path to this server-side method, such as 'your_app.doctype.sales_invoice.sales_invoice.send_whatsapp_invoice'
.
This setup ensures that when the "Send Invoice" button is clicked, it will make an AJAX call to the specified server-side method, which then handles the process of sending the WhatsApp Notification. Remember to adjust the server-side method path and any DocType names or field names to fit your actual setup.
Stale issue message
Hello!
I have a WhatsApp Template, Approved, and pulled successfully. AFAIK, the only way to initiate a message from this app is via
WhatsApp Notification
DocType.The
DocType Event
menu does not have what I need. I need a custom event.More specifically, I have a custom button on a DocType Form View, and I need the notification to be triggered once the button is clicked.
Could you please give me high level directions on how to achive this?
BR