Sending SMS Notifications with Azure Communication Services in Python
[ad_1]
Introducing Azure Communication Services
In our previous article, we discussed how to extract text from images using Azure OCR and cross-check it with an Excel database. Now, we will take it a step further by utilizing Azure Communication Services to send SMS messages. This integration allows us to seamlessly communicate with individuals based on the data obtained from the cross-checking process.
To better understand this use case, we recommend reviewing the previous article if you haven’t already. Additionally, we have uploaded the complete source code of this use case for your reference.
Step 1. Installing the Azure Communication SMS Package
pip install azure-communication-sms
Step 2. Importing the necessary libraries
from azure.communication.sms import SmsClient
from azure.core.credentials import AzureKeyCredential
Step 3. Retrieving the phone number from the Excel file
phone_number = matching_rows.iloc[0]['Phone Number']
print("Phone Number:", phone_number)
Step 4. Authenticating to the Azure Communication service
key = 'YOUR-KEY'
endpoint="YOUR-END-POINT"
Replace 'your-key'
and 'your-end-point'
with your actual endpoint URL and subscription key obtained from Azure Communication Service.
Step 5. Initializing the SMS client
credential = AzureKeyCredential(key)
sms_client = SmsClient(endpoint, credential)
- Create an
AzureKeyCredential
object using the providedkey
value. This credential will be used to authenticate the SMS client. - Initialize the
SmsClient
using theendpoint
andcredential
parameters. - This step sets up the SMS client, enabling you to send SMS messages using the Azure Communication Services API.
Step 6. Composing and sending the message
message = "Hello! This is a test message."
sms_response = sms_client.send(
from_="YOUR_PHONE_NUMBER",
to=[phone_number],
message=message
- Assign a string value to the
message
variable, representing the content of the message you want to send (e.g., "Hello! This is a test message."). - Call the
send
method of thesms_client
to send the message. - The
send
method takes the following parameters: from_
: Specifies the sender's phone number. Replace "YOUR_PHONE_NUMBER" with the actual phone number from which you want to send the message.to
: Specifies the recipient's phone number. It should be provided as a list and, in this case, contains thephone_number
variable extracted from the Excel file.message
: Specifies the content of the message, stored in themessage
variable.- The response received from the API after sending the message is stored in the
sms_response
variable. - This step composes and sends the message using the Azure Communication Services API.
To get a number to send from Azure Communication Services,
Click on the Phone Number option in the left pane of Azure Communication Services.
Note: To access this feature, you need to have a pay-as-you-go subscription.
Step 7. Checking if the message was sent successfully
if sms_response.successful:
print("Message sent successfully!")
else:
print("Message sending failed.")
- The code uses an
if
statement to check thesuccessful
property of thesms_response
object. - If the
successful
property isTrue
, it means the message was sent successfully. - In that case, it prints the message “Message sent successfully!”
- If the
successful
property isFalse
, it means the message sending failed. - In that case, it prints the message “Message sending failed.”
- This step provides feedback on the status of the message sending process, indicating whether it was successful or not.
By following these steps, you can easily retrieve the phone number from an Excel sheet and initiate SMS notifications using Azure Communication Services.
At Skrots, we also provide similar services like Azure Communication Services — allowing you to send SMS notifications with ease. Our platform offers comprehensive communication solutions that can be seamlessly integrated into your existing workflows. To learn more about our services, visit https://skrots.com. Additionally, you can explore the wide range of services we offer at https://skrots.com/services. Thank you for considering Skrots for your communication needs!
Thanks, Harsh
Founder | CEO — Skrots
Learn more about our blog at Blog at Skrots. Checkout our list of services on Skrots. Give a look at our website design at Skrots . Checkout our LinkedIn Page at LinkedIn.com. Check out our original post at https://blog.skrots.com/sending-sms-notifications-with-azure-communication-services-in-python/?feed_id=556&_unique_id=64b61a572790a.