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 __init__(self, leased_phone_number, target_phone_numbers, comm_service_connection_string): self.leased_phone_number = leased_phone_number self.target_phone_numbers = target_phone_numbers self.connection_string = comm_service_connection_string self.sms_client = SmsClient.from_connection_string( self.connection_string)
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 cf_communication_sms(cli_ctx, kwargs): from azure.communication.sms import SmsClient connection_string = kwargs.pop('connection_string', None) if connection_string is None: error_msg = 'Please specify --connection-string, or set AZURE_COMMUNICATION_CONNECTION_STRING.' raise RequiredArgumentMissingError(error_msg) client = SmsClient.from_connection_string(connection_string) return client
def test_send_sms_unauthorized_from_phone_number(self): sms_client = SmsClient.from_connection_string(self.connection_str) with pytest.raises(HttpResponseError) as ex: # calling send() with sms values sms_client.send(from_="+14255550123", to=[self.phone_number], message="Hello World via SMS") assert ex.value.message is not None
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_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_single(self): sms_client = SmsClient.from_connection_string(self.connection_str) # 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 test_send_sms_fake_from_phone_number(self): sms_client = SmsClient.from_connection_string(self.connection_str) with pytest.raises(HttpResponseError) as ex: # calling send() with sms values 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
def setUp(self): super(SMSClientTest, self).setUp() if self.is_playback(): self.phone_number = "+18000005555" else: self.phone_number = os.getenv("PHONE_NUMBER") self.recording_processors.extend([ BodyReplacerProcessor(keys=["to", "from", "messageId"]), ResponseReplacerProcessor(keys=[self._resource_name])]) self.sms_client = SmsClient.from_connection_string(self.connection_str)
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)
def send_sms(self): connection_string = "COMMUNICATION_SERVICES_CONNECTION_STRING" sms_client = SmsClient.from_connection_string(connection_string) # calling send() with sms values smsresponse = 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 print(smsresponse)
def mysms_AZcom(recepient,value): # configuring your account. connection_string = os.getenv('COMMUNICATION_SERVICES_CONNECTION_STRING') # Creating the Client object which is used to send SMS. sms_client = SmsClient.from_connection_string(connection_string) from_phone="xxxxxxxxxx" if to_phone: //do nothing else: logging.info('couldnt get recepient phone number') if value: //do nothing else: //asuming it is intentional for testing
def test_send_sms_multiple_with_options(self): sms_client = SmsClient.from_connection_string(self.connection_str) # calling send() with sms values sms_responses = 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])
def test_send_sms_fake_to_phone_number(self): sms_client = SmsClient.from_connection_string(self.connection_str) # calling send() with sms values sms_responses = 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
def test_send_sms_unique_message_ids(self): sms_client = SmsClient.from_connection_string(self.connection_str) # calling send() with sms values sms_responses_1 = 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 = 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
def send_sms_to_single_recipient(self): sms_client = SmsClient.from_connection_string(self.connection_string) # calling send() with sms values sms_responses = 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 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 send_sms_to_multiple_recipients(self): sms_client = SmsClient.from_connection_string(self.connection_string) # calling send() with sms values sms_responses = 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 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))
import os from azure.communication.sms import SmsClient try: # Quickstart code goes here # Create the SmsClient object which will be used to send SMS messages sms_client = SmsClient.from_connection_string("<connection_string>") ## Send a 1:1 SMS Message # calling send() with sms values # sms_responses = sms_client.send( # from_="<from-phone-number>", # to="<to-phone-number>", # message="Hello World via SMS", # enable_delivery_report=True, # optional property # tag="custom-tag") # optional property # Send a 1:N SMS Message # calling send() with sms values sms_responses = sms_client.send( from_="<from-phone-number>", to=["<to-phone-number-1>", "<to-phone-number-2>"], message="Hello World via SMS Python", enable_delivery_report=True, # optional property tag="custom-tag") # optional property except Exception as ex: print('Exception:') print(ex)
def cf_communication_sms(cli_ctx, kwargs): from azure.communication.sms import SmsClient connection_string = kwargs.pop('connection_string', None) client = SmsClient.from_connection_string(connection_string) return client