Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I trigger a WhatsApp Notification? #59

Closed
ahassoun opened this issue Feb 9, 2024 · 5 comments
Closed

How can I trigger a WhatsApp Notification? #59

ahassoun opened this issue Feb 9, 2024 · 5 comments

Comments

@ahassoun
Copy link

ahassoun commented Feb 9, 2024

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

@simran-baliyan
Copy link

+1

@shridarpatil
Copy link
Owner

shridarpatil commented Feb 12, 2024

@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
Copy link
Author

ahassoun commented Mar 2, 2024

@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
Copy link
Author

ahassoun commented Mar 3, 2024

@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:

Correcting the Client-side Script

  1. Variable Scope: The variable name isn't defined in your function. You should use frm.doc.name to get the name of the current document.
  2. Asynchronous Server Call: Use 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"));
    }
});

Required Server-side Method

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.

Copy link

github-actions bot commented Jul 1, 2024

Stale issue message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants