Code Monkey home page Code Monkey logo

Comments (5)

shridarpatil avatar shridarpatil commented on August 23, 2024

This is fixed

from frappe_whatsapp.

umarless avatar umarless commented on August 23, 2024

Hello I am also getting same error message and the message is successfully delivering.

As I set "After Save" in "DocType Event", Changes are not getting saved, but message is successfully delivering.

Please suggest how to fix.

### App Versions

{
"erpnext": "14.46.2",
"frappe": "14.54.0",
"frappe_whatsapp": "0.0.2",
"hrms": "14.15.0"
}

### Route

Form/Employee/HR-EMP-00001

### Traceback
Traceback (most recent call last):
  File "apps/frappe_whatsapp/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py", line 165, in notify
    frappe.get_doc({
  File "apps/frappe/frappe/model/document.py", line 309, in save
    return self._save(*args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 331, in _save
    return self.insert()
  File "apps/frappe/frappe/model/document.py", line 263, in insert
    self._validate()
  File "apps/frappe/frappe/model/document.py", line 533, in _validate
    self._validate_mandatory()
  File "apps/frappe/frappe/model/document.py", line 868, in _validate_mandatory
    raise frappe.MandatoryError(
frappe.exceptions.MandatoryError: [WhatsApp Message, 008aa04037]: content_type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 95, in application
    response = frappe.api.handle()
  File "apps/frappe/frappe/api.py", line 54, in handle
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 47, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 85, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1622, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/desk/form/save.py", line 31, in savedocs
    doc.save()
  File "apps/frappe/frappe/model/document.py", line 309, in save
    return self._save(*args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 360, in _save
    self.run_post_save_methods()
  File "apps/frappe/frappe/model/document.py", line 1092, in run_post_save_methods
    self.run_method("on_update")
  File "apps/frappe/frappe/model/document.py", line 919, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 1281, in composer
    return composed(self, method, *args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 1265, in runner
    add_to_return_value(self, f(self, method, *args, **kwargs))
  File "apps/frappe_whatsapp/frappe_whatsapp/utils/__init__.py", line 27, in run_server_script_for_doc_event
    ).send_template_message(doc)
  File "apps/frappe_whatsapp/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py", line 146, in send_template_message
    self.notify(data)
  File "apps/frappe_whatsapp/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py", line 177, in notify
    response = frappe.flags.integration_request.json()['error']
KeyError: 'error'
### Request Data
{
	"type": "POST",
	"args": {
		"doc": "{\"name\":\"HR-EMP-00001\",\"owner\":\"Administrator\",\"creation\":\"2023-11-26 11:10:26.224093\",\"modified\":\"2023-11-26 11:10:26.224093\",\"modified_by\":\"Administrator\",\"docstatus\":0,\"idx\":0,\"employee\":\"HR-EMP-00001\",\"naming_series\":\"HR-EMP-\",\"first_name\":\"Umar\",\"last_name\":\"Khan\",\"employee_name\":\"umar khan\",\"gender\":\"Male\",\"date_of_birth\":\"1970-11-25\",\"date_of_joining\":\"2023-09-01\",\"status\":\"Active\",\"create_user_permission\":1,\"company\":\"Motor Care\",\"notice_number_of_days\":0,\"date_of_retirement\":\"2030-11-25\",\"cell_number\":\"+918087616076\",\"prefered_contact_email\":\"\",\"unsubscribed\":0,\"current_accommodation_type\":\"\",\"permanent_accommodation_type\":\"\",\"ctc\":0,\"salary_currency\":\"OMR\",\"salary_mode\":\"\",\"marital_status\":\"\",\"blood_group\":\"\",\"leave_encashed\":\"\",\"lft\":1,\"rgt\":2,\"old_parent\":\"\",\"doctype\":\"Employee\",\"external_work_history\":[],\"education\":[],\"internal_work_history\":[],\"__last_sync_on\":\"2023-11-26T07:22:14.520Z\",\"__unsaved\":1}",
		"action": "Save"
	},
	"btn": {
		"jQuery36006108461538344191": {
			"events": {
				"click": [
					{
						"type": "click",
						"origType": "click",
						"guid": 1211,
						"namespace": ""
					}
				]
			}
		}
	},
	"freeze": true,
	"headers": {},
	"error_handlers": {},
	"url": "/api/method/frappe.desk.form.save.savedocs"
}
### Response Data
{
	"exception": "KeyError: 'error'",
	"_server_messages": "[\"{\\\"message\\\": \\\"Error: Value missing for WhatsApp Message: Content Type\\\", \\\"title\\\": \\\"Message\\\"}\"]"
}

from frappe_whatsapp.

felixlu07 avatar felixlu07 commented on August 23, 2024

Same here! Its quite odd. The message DID successfully get sent. However, if the event was set as sales invoice after_save, then the saving of the sales invoice would fail, but the message would be successfully sent. Same error message as

Message
Error: Value missing for WhatsApp Message: Content Type
{
  "exc_type": "DoesNotExistError",
  "exception": "KeyError: 'error'",
  "exc": "[\"Traceback (most recent call last):\\n  File \\\"apps/frappe_whatsapp/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py\\\", line 189, in notify\\n    frappe.get_doc({\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 309, in save\\n    return self._save(*args, **kwargs)\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 331, in _save\\n    return self.insert()\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 263, in insert\\n    self._validate()\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 533, in _validate\\n    self._validate_mandatory()\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 868, in _validate_mandatory\\n    raise frappe.MandatoryError(\\nfrappe.exceptions.MandatoryError: [WhatsApp Message, 2b153819f1]: content_type\\n\\nDuring handling of the above exception, another exception occurred:\\n\\nTraceback (most recent call last):\\n  File \\\"apps/frappe/frappe/app.py\\\", line 95, in application\\n    response = frappe.api.handle()\\n  File \\\"apps/frappe/frappe/api.py\\\", line 55, in handle\\n    return frappe.handler.handle()\\n  File \\\"apps/frappe/frappe/handler.py\\\", line 47, in handle\\n    data = execute_cmd(cmd)\\n  File \\\"apps/frappe/frappe/handler.py\\\", line 85, in execute_cmd\\n    return frappe.call(method, **frappe.form_dict)\\n  File \\\"apps/frappe/frappe/__init__.py\\\", line 1628, in call\\n    return fn(*args, **newargs)\\n  File \\\"apps/frappe/frappe/desk/form/save.py\\\", line 31, in savedocs\\n    doc.save()\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 309, in save\\n    return self._save(*args, **kwargs)\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 331, in _save\\n    return self.insert()\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 289, in insert\\n    self.run_post_save_methods()\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 1092, in run_post_save_methods\\n    self.run_method(\\\"on_update\\\")\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 919, in run_method\\n    out = Document.hook(fn)(self, *args, **kwargs)\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 1281, in composer\\n    return composed(self, method, *args, **kwargs)\\n  File \\\"apps/frappe/frappe/model/document.py\\\", line 1265, in runner\\n    add_to_return_value(self, f(self, method, *args, **kwargs))\\n  File \\\"apps/frappe_whatsapp/frappe_whatsapp/utils/__init__.py\\\", line 27, in run_server_script_for_doc_event\\n    ).send_template_message(doc)\\n  File \\\"apps/frappe_whatsapp/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py\\\", line 170, in send_template_message\\n    self.notify(data)\\n  File \\\"apps/frappe_whatsapp/frappe_whatsapp/frappe_whatsapp/doctype/whatsapp_notification/whatsapp_notification.py\\\", line 202, in notify\\n    response = frappe.flags.integration_request.json()['error']\\nKeyError: 'error'\\n\"]",
  "_server_messages": "[\"{\\\"message\\\": \\\"Error: Value missing for WhatsApp Message: Content Type\\\", \\\"title\\\": \\\"Message\\\"}\"]"
}

@shridarpatil any ideas on this?

from frappe_whatsapp.

felixlu07 avatar felixlu07 commented on August 23, 2024

@umarless If you are familiar with how to modify some of the ERPNext doctype and the Python script within whatsapp_message.py , you should modify this particular function before_insert to set self.content_type a default value in case it is not sent. The issue is that whenever the event is fired, the WhatsApp notification is triggered to be inserted into the WhatsApp Message DOCTYPE. However, it fails to be inserted because content_type is a mandatory field. When we trigger an event-based WhatsAppMessage template, this content_type data is not being sent to the WhatsApp Message DOCTYPE, therefore, it is erroring out. After I set it to a default, then everything works okay for now.

def before_insert(self):
        """Send message."""
        # default content_type to 'text' if not provided
        if not self.content_type:
            self.content_type = "text"

        if self.type == 'Outgoing' and self.message_type != 'Template':
            if self.attach and not self.attach.startswith("http"):
                link = frappe.utils.get_url() + '/' + self.attach
            else:
                link = self.attach

            data = {
                "messaging_product": "whatsapp",
                "to": self.format_number(self.to),
                "type": self.content_type
            }

            if self.content_type in ['document', 'image', 'video']:
                data[self.content_type.lower()] = {
                    "link": link,
                    "caption": self.message
                }
            elif self.content_type == "text":
                data["text"] = {
                    "preview_url": True,
                    "body": self.message
                }
            elif self.content_type == "audio":
                data["audio"] = {  # This should likely be "audio" instead of "text"
                    "link": link
                }

            try:
                self.notify(data)
                self.status = "Success"
            except Exception as e:
                self.status = "Failed"
                frappe.throw(f"Failed to send message {str(e)}")

from frappe_whatsapp.

shridarpatil avatar shridarpatil commented on August 23, 2024

@felixlu07 I guess you are on older version of the app can you update the app?

from frappe_whatsapp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.