Neurotech-HQ/heyoo

Send Template message with components

yodaljit opened this issue · 5 comments

I'm trying to send a message template with 2 variables but the message is not being sent. I there any issue with my code? Please help.

messenger.send_template("artist_approve", "91"+instance.phone, components={"type": "body","parameters": [{"type": "text","text": instance.user.first_name+" "+instance.user.last_name},{"type": "text","text": "https://dirums.com/artist-details/"+instance.slug}]})

I have the same issue, please help or guidance.

Hi @MesserOr @yodaljit

Might be an issue with the wrapper let me have a close look

If you already found a way around you this, you can contribute but submitting a pull request with the changes

Thank you
Kalebu

There is something wrong in the method send_template, the components parameter seems to use dict, but in reality it would need to be a list, like in the examples in https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates#text-based

Your components would need to be:

messenger.send_template("artist_approve", "PHONE", components=[
    {
      "type": "body",
      "parameters": [
        {
          "type": "text",
          "text": "YOUR_TEXT"
        },
        {
          "type": "text",
          "text": "https: //dirums.com/artist-details/SLUG"
        }
      ]
    }
  ]

I don't know how to submit a PR, but I think the correct method would be: (I marked with # CHANGE my modifications)

def send_template(self, template: str, recipient_id: str, recipient_type="individual",
                      lang: str = "en_US", components: List = None):  # CHANGE
        """
        Sends a template message to a WhatsApp user, Template messages can either be;
            1. Text template
            2. Media based template
            3. Interactive template
        You can customize the template message by passing a dictionary of components.
        You can find the available components in the documentation.
        https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates
        Args:
            template[str]: Template name to be sent to the user
            recipient_id[str]: Phone number of the user with country code wihout +
            lang[str]: Language of the template message
            components[list]: List of components to be sent to the user  # CHANGE
        Example:
            >>> from whatsapp import WhatsApp
            >>> whatsapp = WhatsApp(token, phone_number_id)
            >>> whatsapp.send_template("hello_world", "5511999999999", lang="en_US"))
        """
        if components is None:  # CHANGE: TO NOT USE LIST AS DEFAULT, BECAUSE IT IS MUTABLE
            components = []
        data = {
            "messaging_product": "whatsapp",
            "recipient_type": recipient_type,
            "to": recipient_id,
            "type": "template",
            "template": {
                "name": template,
                "language": {"code": lang},
                "components": components,
            },
        }
        logging.info(f"Sending template to {recipient_id}")
        r = requests.post(self.url, headers=self.headers, json=data)

        if r.status_code == 200:
            logging.info(f"Template sent to {recipient_id}")
            return r.json()
        logging.info(f"Template not sent to {recipient_id}")
        logging.info(f"Status code: {r.status_code}")
        logging.info(f"Response: {r.json()}")
        return r.json()

Hope it helps!!

Best regards, Diego.

Thank you for spotting @diegocgaona
Let me update it ASAP

Updated it, I will now close this issue

Thanks to @diegocgaona @yodaljit