Exemple #1
0
    async def send_sms_to_single_recipient_async(self):
        sms_client = SmsClient.from_connection_string(self.connection_string)

        async with sms_client:
            try:
                # calling send() with sms values
                sms_responses = await sms_client.send(
                    from_="<leased-phone-number>",
                    to="<to-phone-number>",
                    message="Hello World via SMS",
                    enable_delivery_report=True,  # optional property
                    tag="custom-tag")  # optional property
                sms_response = sms_responses[0]

                if (sms_response.successful):
                    print(
                        "Message with message id {} was successful sent to {}".
                        format(sms_response.message_id, sms_response.to))
                else:
                    print(
                        "Message failed to send to {} with the status code {} and error: {}"
                        .format(sms_response.to, sms_response.http_status_code,
                                sms_response.error_message))
            except Exception:
                print(Exception)
                pass
    async def test_send_sms_unauthorized_from_phone_number_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        with pytest.raises(HttpResponseError) as ex:
            async with sms_client:
                # calling send() with sms values
                await sms_client.send(from_="+14255550123",
                                      to=[self.phone_number],
                                      message="Hello World via SMS")

        assert ex.value.message is not None
    async def test_send_sms_fake_from_phone_number_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        with pytest.raises(HttpResponseError) as ex:
            async with sms_client:
                # calling send() with sms values
                await sms_client.send(from_="+15550000000",
                                      to=[self.phone_number],
                                      message="Hello World via SMS")

        assert str(ex.value.status_code) == "400"
        assert ex.value.message is not None
    async def test_send_sms_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        async with sms_client:
            # calling send() with sms values
            sms_response = await sms_client.send(
                from_phone_number=PhoneNumber(self.phone_number),
                to_phone_numbers=[PhoneNumber(self.phone_number)],
                message="Hello World via SMS",
                send_sms_options=SendSmsOptions(enable_delivery_report=True))  # optional property

            assert sms_response.message_id is not None
    async def test_send_sms_single_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        async with sms_client:
            # calling send() with sms values
            sms_responses = await sms_client.send(
                from_=self.phone_number,
                to=self.phone_number,
                message="Hello World via SMS")

            assert len(sms_responses) == 1

            self.verify_successful_sms_response(sms_responses[0])
    async def test_send_sms_multiple_with_options_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        async with sms_client:
            # calling send() with sms values
            sms_responses = await sms_client.send(
                from_=self.phone_number,
                to=[self.phone_number, self.phone_number],
                message="Hello World via SMS",
                enable_delivery_report=True,  # optional property
                tag="custom-tag")  # optional property

            assert len(sms_responses) == 2

            self.verify_successful_sms_response(sms_responses[0])
            self.verify_successful_sms_response(sms_responses[1])
    async def test_send_sms_single_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        async with sms_client:
            # calling send() with sms values
            sms_responses = await sms_client.send(
                from_=self.phone_number,
                to=self.phone_number,
                message="Hello World via SMS",
                enable_delivery_report=True,  # optional property
                tag="custom-tag")  # optional property

            assert len(sms_responses) is 1

            for sms_response in sms_responses:
                self.verify_sms_response(sms_response)
    async def send_sms_async(self):
        connection_string = "COMMUNICATION_SERVICES_CONNECTION_STRING"
        sms_client = SmsClient.from_connection_string(connection_string)

        async with sms_client:
            try:
                # calling send() with constructed request object
                smsresponse = await sms_client.send(
                    from_phone_number=PhoneNumberIdentifier("<leased-phone-number>"),
                    to_phone_numbers=[PhoneNumberIdentifier("<to-phone-number>")],
                    message="Hello World via SMS",
                    send_sms_options=SendSmsOptions(enable_delivery_report=True)) # optional property
            except Exception:
                print(Exception)
                pass

            print(smsresponse)
    async def send_sms_async(self):
        connection_string = "COMMUNICATION_SERVICES_CONNECTION_STRING"
        sms_client = SmsClient.from_connection_string(connection_string)

        async with sms_client:
            try:
                # calling send() with sms values
                sms_responses = await sms_client.send(
                    from_="<leased-phone-number>",
                    to=[
                        "<to-phone-number-1>", "<to-phone-number-2>",
                        "<to-phone-number-3>"
                    ],
                    message="Hello World via SMS",
                    enable_delivery_report=True,  # optional property
                    tag="custom-tag")  # optional property
            except Exception:
                print(Exception)
                pass

            failed_recipients = []
            for sms_response in sms_responses:
                if (sms_response.successful):
                    print(
                        "Message with message id {} was successful sent to {}".
                        format(sms_response.message_id, sms_response.to))
                else:
                    print(
                        "Message failed to send to {} with the status code {} and error: {}"
                        .format(sms_response.to, sms_response.http_status_code,
                                sms_response.error_message))
                    if (sms_response.http_status_code != 400):
                        failed_recipients.append(sms_response.to)

            try:
                # calling send() with failed recipients
                sms_responses = await sms_client.send(
                    from_="<leased-phone-number>",
                    to=failed_recipients,
                    message="Hello World via SMS",
                    enable_delivery_report=True,  # optional property
                    tag="custom-tag")  # optional property
            except Exception:
                print(Exception)
                pass
    async def test_send_sms_fake_to_phone_number_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        async with sms_client:
            # calling send() with sms values
            sms_responses = await sms_client.send(
                from_=self.phone_number,
                to=["+15550000000"],
                message="Hello World via SMS")

            assert len(sms_responses) == 1

            assert sms_responses[0].message_id is None
            assert sms_responses[0].http_status_code == 400
            assert sms_responses[
                0].error_message == "Invalid To phone number format."
            assert not sms_responses[0].successful
    async def test_send_sms_invalid_to_phone_number_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        async with sms_client:
            # calling send() with sms values
            sms_responses = await sms_client.send(
                from_=self.phone_number,
                to=["+1234567891011"],
                message="Hello World via SMS",
                enable_delivery_report=True,  # optional property
                tag="custom-tag")  # optional property

            assert len(sms_responses) is 1

        for sms_response in sms_responses:
            assert sms_response.http_status_code == 400
            assert not sms_response.successful
    async def test_send_sms_unique_message_ids_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        async with sms_client:
            # calling send() with sms values
            sms_responses_1 = await sms_client.send(
                from_=self.phone_number,
                to=[self.phone_number],
                message="Hello World via SMS")

            # calling send() again with the same sms values
            sms_responses_2 = await sms_client.send(
                from_=self.phone_number,
                to=[self.phone_number],
                message="Hello World via SMS")

            assert sms_responses_1[0].message_id != sms_responses_2[
                0].message_id
    async def test_send_sms_unique_message_ids_async(self):

        sms_client = SmsClient.from_connection_string(self.connection_str)

        async with sms_client:
            # calling send() with sms values
            sms_responses_1 = await sms_client.send(
                from_=self.phone_number,
                to=[self.phone_number],
                message="Hello World via SMS")

            # calling send() again with the same sms values
            sms_responses_2 = await sms_client.send(
                from_=self.phone_number,
                to=[self.phone_number],
                message="Hello World via SMS")

            self.verify_successful_sms_response(sms_responses_1[0])
            self.verify_successful_sms_response(sms_responses_2[0])
            # message ids should be unique due to having a different idempotency key
            assert sms_responses_1[0].message_id != sms_responses_2[
                0].message_id