Пример #1
0
    def send_generic_template(
        self,
        recipient_id=None,
        user_ref=None,
        phone_number=None,
        elements=None,
        quick_replies=None,
    ):
        """Sends a generic template to the recipient.

        # Arguments
            recipient_id: page specific id of the recipient
            user_ref: optional. user_ref from the checkbox plugin
            phone_number: Optional. Phone number of the recipient with the format +1(212)555-2368. Your bot must be approved for Customer Matching to send messages this way.
            elements: An array of element objects that describe instances of the generic template to be sent. Specifying multiple elements will send a horizontally scrollable carousel of templates. A maximum of 10 elements is supported.
            quick_replies: An array of objects the describe the quick reply buttons to send. A maximum of 11 quick replies are supported.

        """

        generic_template_payload = GenericTemplatePayload(
            dict(template_type="generic", elements=elements)
        )
        attachment = Attachment(dict(type="template", payload=generic_template_payload))
        message = Message({"quick_replies": quick_replies, "attachment": attachment})

        response = self._post(message, recipient_id, user_ref, phone_number)
        return response
Пример #2
0
    def send_text_message(self, recipient_id=None, text=None, user_ref=None):
        """Sends a simple text message to a given recipient.

        # Arguments
            recipient_id: id of the recipient
            text: message to be sent to the recipient

        # Examples
        ```python
        client.send_text_message(recipient_id="2157136727638083", text="hello world!")
        ```
        """
        message = Message({"text": text})
        recipient = Recipient({"id": recipient_id, "user_ref": user_ref})
        request = Request({"recipient": recipient, "message": message})
        # throws DataError if validation fails
        request.validate()
        params = {'access_token': self.page_access_token}
        response = requests.post(API_URL,
                                 params=params,
                                 json=request.to_primitive())
        json_response = response.json()
        if response.status_code == 400 and json_response.get("error", {}).get(
                "type", "") == "OAuthException":
            raise OAuthException(json_response.get("error").get("message", ""))
        return response
Пример #3
0
    def send_list_template(
        self,
        recipient_id=None,
        user_ref=None,
        phone_number=None,
        elements=None,
        buttons=None,
        quick_replies=None,
    ):
        """Sends a list template to the recipient.

        # Arguments
            recipient_id: page specific id of the recipient
            user_ref: optional. user_ref from the checkbox plugin
            phone_number: Optional. Phone number of the recipient with the format +1(212)555-2368. Your bot must be approved for Customer Matching to send messages this way.
            elements: Array of objects that describe items in the list. Minimum of 2 elements required. Maximum of 4 elements is supported.
            buttons: Button to display at the bottom of the list. Maximum of 1 button is supported.
            quick_replies: An array of objects the describe the quick reply buttons to send. A maximum of 11 quick replies are supported.

        """

        list_template_payload = ListTemplatePayload(
            dict(template_type="list", elements=elements, buttons=buttons)
        )
        attachment = Attachment(dict(type="template", payload=list_template_payload))
        message = Message({"quick_replies": quick_replies, "attachment": attachment})

        response = self._post(message, recipient_id, user_ref, phone_number)
        return response
Пример #4
0
    def send_button_template(
        self,
        recipient_id=None,
        user_ref=None,
        phone_number=None,
        text=None,
        quick_replies=None,
        buttons=None,
    ):
        """Sends a button template to the recipient.

        # Arguments
            recipient_id: page specific id of the recipient
            user_ref: optional. user_ref from the checkbox plugin
            phone_number: Optional. Phone number of the recipient with the format +1(212)555-2368. Your bot must be approved for Customer Matching to send messages this way.
            text: UTF-8-encoded text of up to 640 characters. Text will appear above the buttons.
            quick_replies: An array of objects the describe the quick reply buttons to send. A maximum of 11 quick replies are supported.
            buttons: Set of 1-3 buttons that appear as call-to-actions.

        """

        button_template_payload = ButtonTemplatePayload(
            dict(template_type="button", text=text, buttons=buttons)
        )
        attachment = Attachment(dict(type="template", payload=button_template_payload))
        message = Message({"quick_replies": quick_replies, "attachment": attachment})
        response = self._post(message, recipient_id, user_ref, phone_number)
        return response
Пример #5
0
def test_validation_when_text_of_message_is_too_long(client):
    """
    GIVEN a Message object with a too long text
    WHEN validating the object
    THEN is throws a validation error
    """
    m = Message({"text": "*" * 2001})
    with pytest.raises(DataError):
        m.validate()
Пример #6
0
    def send_receipt_template(
        self,
        recipient_id=None,
        user_ref=None,
        phone_number=None,
        recipient_name=None,
        elements=None,
        order_number=None,
        currency=None,
        payment_method=None,
        timestamp=None,
        address=None,
        summary=None,
        adjustments=None,
        quick_replies=None,
    ):
        """Sends a receipt template to the recipient.

        # Arguments
            recipient_id: page specific id of the recipient
            user_ref: optional. user_ref from the checkbox plugin
            phone_number: Optional. Phone number of the recipient with the format +1(212)555-2368. Your bot must be approved for Customer Matching to send messages this way.
            elements: Optional. Array of a maximum of 100 element objects that describe items in the order. Sort order of the elements is not guaranteed.
            order_number: The order number. Must be unique.
            currency: The currency of the payment.
            payment_method: The payment method used. Providing enough information for the customer to decipher which payment method and account they used is recommended. This can be a custom string, such as, "Visa 1234".
            timestamp: Optional. Timestamp of the order in seconds.
            address: Optional. The shipping address of the order.
            summary: The payment summary.
            adjustments: Optional. An array of payment objects that describe payment adjustments, such as discounts.
            quick_replies: An array of objects the describe the quick reply buttons to send. A maximum of 11 quick replies are supported.

        """

        receipt_template_payload = ReceiptTemplatePayload(
            dict(
                template_type="receipt",
                recipient_name=recipient_name,
                elements=elements,
                order_number=order_number,
                currency=currency,
                payment_method=payment_method,
                timestamp=timestamp,
                address=address,
                summary=summary,
                adjustments=adjustments,
            )
        )
        attachment = Attachment(dict(type="template", payload=receipt_template_payload))
        message = Message({"quick_replies": quick_replies, "attachment": attachment})

        response = self._post(message, recipient_id, user_ref, phone_number)
        return response
Пример #7
0
    def send_text(self, recipient_id=None, user_ref=None, phone_number=None, text=None):
        """Sends a text message to the recipient.

        # Arguments
            recipient_id: page specific id of the recipient
            user_ref: Optional. user_ref from the checkbox plugin
            phone_number: Optional. Phone number of the recipient with the format +1(212)555-2368. Your bot must be approved for Customer Matching to send messages this way.
            text: must be UTF-8 and has a 2000 character limit.


        """

        message = Message({"text": text})

        response = self._post(message, recipient_id, user_ref, phone_number)
        return response
Пример #8
0
    def send_file(
        self,
        recipient_id=None,
        user_ref=None,
        phone_number=None,
        url=None,
        quick_replies=None,
    ):
        """Sends a file to the recipient.

        # Arguments
            recipient_id: page specific id of the recipient
            user_ref: Optional. user_ref from the checkbox plugin
            url: URL of the file
            quick_replies: An array of objects the describe the quick reply buttons to send. A maximum of 11 quick replies are supported.

        """
        attachment = Attachment(
            dict(type="file", payload=RichMediaPayload(dict(url=url)))
        )
        message = Message({"quick_replies": quick_replies, "attachment": attachment})
        response = self._post(message, recipient_id, user_ref, phone_number)
        return response
Пример #9
0
    def send_quick_replies(
        self,
        recipient_id=None,
        user_ref=None,
        phone_number=None,
        text=None,
        quick_replies=None,
    ):
        """Sends quick replies to the recipient.

        # Arguments
            recipient_id: page specific id of the recipient
            user_ref: Optional. user_ref from the checkbox plugin
            phone_number: Optional. Phone number of the recipient with the format +1(212)555-2368. Your bot must be approved for Customer Matching to send messages this way.
            text: must be UTF-8 and has a 2000 character limit.
            quick_replies: An array of objects the describe the quick reply buttons to send. A maximum of 11 quick replies are supported.


        """

        message = Message({"text": text, "quick_replies": quick_replies})

        response = self._post(message, recipient_id, user_ref, phone_number)
        return response