def send_sms(resource_endpoint, from_phone_number, to_phone_number, message_content): sms_client = SmsClient(resource_endpoint, credential) response = sms_client.send( from_=from_phone_number, to=[to_phone_number], message=message_content, enable_delivery_report=True # optional property ) return response
def test_send_message_parameters(self, mock_send): phone_number = "+14255550123" msg = "Hello World via SMS" tag = "custom-tag" sms_client = SmsClient("https://endpoint", FakeTokenCredential()) sms_client.send( from_=phone_number, to=[phone_number], message=msg, enable_delivery_report=True, tag=tag) send_message_request = mock_send.call_args[0][0] self.assertEqual(phone_number, send_message_request.from_property) self.assertEqual(phone_number, send_message_request.sms_recipients[0].to) self.assertIsNotNone(send_message_request.sms_recipients[0].repeatability_request_id) self.assertIsNotNone(send_message_request.sms_recipients[0].repeatability_first_sent) self.assertTrue(send_message_request.sms_send_options.enable_delivery_report) self.assertEqual(tag, send_message_request.sms_send_options.tag)
def test_send_sms_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() sms_client = SmsClient(endpoint, credential) # calling send() with sms values sms_responses = 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])
def sms_token_credential_auth(self): # To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have # AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables. sms_client = SmsClient(self.endpoint, DefaultAzureCredential()) # calling send() with sms values sms_responses = sms_client.send(from_="<leased-phone-number>", to=["<to-phone-number>"], message="Hello World via SMS") 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))
def test_send_message(self): phone_number = "+14255550123" raised = False def mock_send(*_, **__): return mock_response(status_code=202, json_payload={ "value": [ { "to": phone_number, "messageId": "id", "httpStatusCode": "202", "errorMessage": "null", "repeatabilityResult": "accepted", "successful": "true" } ] }) sms_client = SmsClient("https://endpoint", FakeTokenCredential(), transport=Mock(send=mock_send)) sms_response = None try: sms_responses = sms_client.send( from_=phone_number, to=[phone_number], message="Hello World via SMS", enable_delivery_report=True, tag="custom-tag") sms_response = sms_responses[0] except: raised = True raise self.assertFalse(raised, 'Expected is no excpetion raised') self.assertEqual(phone_number, sms_response.to) self.assertIsNotNone(sms_response.message_id) self.assertEqual(202, sms_response.http_status_code) self.assertIsNotNone(sms_response.error_message) self.assertTrue(sms_response.successful)