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['Phone Number'] print("Phone Number:", phone_number)
Step 4. Authenticating to the Azure Communication service
key = 'YOUR-KEY' endpoint="YOUR-END-POINT"
'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
AzureKeyCredentialobject using the provided
keyvalue. This credential will be used to authenticate the SMS client.
- Initialize the
- 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
messagevariable, representing the content of the message you want to send (e.g., “Hello! This is a test message.”).
- Call the
sendmethod of the
sms_clientto send the message.
sendmethod 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 the
phone_numbervariable extracted from the Excel file.
message: Specifies the content of the message, stored in the
- The response received from the API after sending the message is stored in the
- 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
ifstatement to check the
successfulproperty of the
- If the
True, it means the message was sent successfully.
- In that case, it prints the message “Message sent successfully!”
- If the
False, 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!