Part 2 - Multichannel notification system using Azure Communication Services and Azure Functions
In the first part of this topic, we setup all the Azure resources like the Azure Communication Services for Email, SMS, WhatsApp for Business and developed the Azure Functions code for the email trigger. In this second part, we will complete coding the remaining Azure Functions triggers and then go ahead to deploy the multichannel notification system to Azure Functions, testing the Email, SMS, and WhatsApp triggers with OpenAI GPTs. Let’s get started!
Prerequisite
To follow this tutorial, ensure you have completed the first part of this topic.
Coding the SMSTrigger
Enhancing the SMSTrigger
Azure Function from the default template involves a series of steps. These steps will transform the basic Function into one that can send SMS messages using Azure Communication Services. Below is a guide to get you from the default HTTP triggered function to the finished SMSTrigger
.
Step 1: Set Up the Function Template
Follow the instructions for setting up the function template from the Email section and name the trigger as ‘SMSTrigger’ or any other string you prefer.
Step 2: Add Azure Communication Services SMS Reference
Add a reference to using Azure.Communication.Sms
then create a property in the SMS Trigger class to hold an instance of SmsClient
and a property to hold the email sender address.
csharp
Step 3: Read Configuration and Initialize SmsClient
In the constructor of the SMSTrigger
class, read the Azure Communication Services connection string from the environment variables using the Environment.GetEnvironmentVariable()
method and initialize the SmsClient
instance.
Be sure to check if the connection string is null, and if so, throw an exception to indicate that the environment variable is missing:
csharp
Step 4: Define the Request Model
Create a request model class within the SMSTrigger
class called SmsRequest
. This model should contain properties for the message text and the phone number to which the message will be sent.
csharp
Step 5: Parse the Request Body
Change the Run
function to be async
as we will perform asynchronous operations. Use a StreamReader
to read the request body as a string and deserialize it into an SmsRequest
object using JsonSerializer
.
csharp
If the request body fails to deserialize into SmsRequest
, return a BadRequestResult
:
csharp
Step 6: Define the Sender and Send an SMS
Retrieve the sender's phone number from the environment variables with Environment.GetEnvironmentVariable()
. Then, attempt to send the SMS with a try-catch block, handling any RequestFailedException
that may occur and logging the relevant information:
csharp
Step 7: Return a Success Response
If sending the SMS is successful, return an OkObjectResult
to the caller indicating that the SMS has been sent.
csharp
Final Code
The final SMSTrigger
Azure Function, with the steps implemented, should look as follows:
csharp
This completed SMSTrigger
Azure Function can now facilitate SMS as part of your multichannel notification system.
Coding the WhatsAppTrigger
Creating a functional WhatsAppTrigger
Azure Function involves iterating on the default HTTP-triggered function template provided by Azure Functions for C#. We will modify this template to integrate Azure Communication Services for sending WhatsApp messages via template messages. Follow the steps below to transform this template into a complete WhatsAppTrigger
function:
Step 1: Set Up the Function Template
Follow the instructions in the first step for setting up SMS trigger and name the function as WhatsAppTrigger
. Set the authorization level to anonymous or function, depending on your security preference.
Step 2: Reference the Azure Communication Services Messages Package
Ensure the Azure.Communication.Messages
NuGet package is included in your project to enable messaging features needed for WhatsApp. Install the package with the following command in Visual Studio Code’s terminal:
bash
Add a reference to using Azure.Communication.Messages
then create a property in the WhatsApp
Trigger class to hold an instance of NotificationMessagesClient
and a property to hold the WhatsApp identifier.
csharp
Step 3: Read Configuration and Initialize NotificationMessagesClient
Update the WhatsAppTrigger
class constructor to read the Azure Communication Services connection string from environment variables using Environment.GetEnvironmentVariable()
and initialize NotificationMessagesClient
with this connection string:
csharp
Step 4: Define the Request Model
Create a request model class named WhatsAppRequest
within the WhatsAppTrigger
class, containing properties for the destination phone number, template name, language, and template parameters:
csharp
Step 5: Parse the Request Body
Convert the Run function to be async
to enable asynchronous work. Use StreamReader
to read the request body and deserialize it to a WhatsAppRequest
instance using System.Text.Json.JsonSerializer
with JsonNamingPolicy.CamelCase
.
csharp
Handle potential deserialization failure by returning BadRequestResult
:
csharp
Step 6: Prepare Template Message and Send WhatsApp Message
Modify the try-catch block to construct a SendMessageOptions
object using MessageTemplateWhatsAppBindingsand MessageTemplate
, and then make a call to _messagesClient.SendMessageAsync(sendTemplateMessageOptions)
:
csharp
Step 7: Return Success Response
After sending the WhatsApp message successfully, return an OkObjectResult stating "WhatsApp sent successfully!".
csharp
Final Code
Following the described steps, the final WhatsAppTrigger
Azure Function should look like this:
csharp
The WhatsAppTrigger
Azure Function is now ready to send WhatsApp template messages. Be sure to test it extensively and remember to handle any issues related to input validation and communicate with the Azure Communication Services API correctly.
Deployment and Testing
After developing the multichannel notification system using Azure Functions, the next step is to deploy and test the functions. This section will guide you through deploying your Azure Function to the cloud and testing the Email, SMS, and WhatsApp triggers.
Deploying the Azure Function
Deployment of your Azure Function can be done right from Visual Studio Code with the Azure Functions extension.
- Publish the Function App: In Visual Studio Code, sign in to Azure if you haven't already. In the Azure Functions extension tab, find the 'Deploy to Function App...' button and select it.
- Choose Your Function App: You can either create a new Function App or deploy it to an existing one. If it's the first time you are deploying, choose 'Create New Function App in Azure...'.
- Set the Configuration: Provide a unique name for your Function App, select a runtime stack (.NET Core in this case), choose the appropriate region, and confirm your selections.
- Wait for Deployment: The deployment process will take a few minutes. Monitor the output window for completion status and any potential errors.
Set Up Application Settings
After deployment, you need to configure the application settings (environment variables) in Azure.
- Open the Function App: Navigate to the Azure Portal, and find your Function App under 'All Resources' or by searching the name you provided.
- Access Application Settings: In the Function App's menu, go to 'Configuration' under the 'Settings' section.
- Add the Settings: Click on 'New application setting' and add the key-value pairs for the environment variables specified in your
local.settings.json
:COMMUNICATION_SERVICES_CONNECTION_STRING
,SENDER_EMAIL_ADDRESS
,SENDER_PHONE_NUMBER
,WHATSAPP_NUMBER
, etc.,
json
- Save and Restart: After adding the required settings, make sure to save the configurations and restart the Function App to ensure the new settings take effect.
Alternatively, when the Function has finished deploying, you can click on 'Upload settings' to upload your settings from local.settings.json. Don't forget to restart the Function App after uploading the settings.
Testing the Function
With the deployment complete and the environment configured, it's time to verify that your function works as intended through each communication channel.
Testing Email Notifications
To test the EmailTrigger
function:
- Send an HTTP POST Request: Use a tool like Postman to send a POST request to the Function App's URL suffixed with
/api/EmailTrigger
. The body should contain JSON with keys forsubject
,htmlContent
, andrecipient
. - Verify Email Receipt: Check the recipient's email inbox for the message. Ensure that the subject and content match what you sent through the POST request.
Testing SMS Notifications
To test the SMSTrigger
function:
- Send an HTTP POST Request: Using Postman, send a POST request to the Function App's URL with
/api/SMSTrigger
at the end. The body of your request should contain JSON withmessage
andphoneNumberkeys
. - Check for SMS: Ensure that the specified phone number receives the SMS and the message content matches the request.
Testing WhatsApp Notifications
To test the WhatsAppTrigger
function:
- Send an HTTP POST Request: Use Postman again to POST to the Function URL, this time ending with
/api/WhatsAppTrigger
. Include a JSON body with keys forphoneNumber
,templateName
,templateLanguage
, andtemplateParameters
. - Confirm WhatsApp Message: Verify that the WhatsApp message reaches the intended recipient with correct template filling.
Integrate with OpenAI GPTs
In OpenAI GPTs editor, click 'new GPT' and 'configure'. Name it "Email Sender" and set the description and instructions as mentioned.
Add Actions and JSON Schema
Click 'Create new action' in your GPT configuration. Enter the following JSON:
json
Leave Authentication to none, and Privacy Policy blank.
Test Your GPT
Finally, try out your GPT in the preview pane to see it in action!
By following these steps, you can easily integrate Azure Communication Services with OpenAI GPTs to send emails effortlessly.
Conclusion and Further Reading
We have successfully walked through the journey of building a serverless multichannel notification system using Azure Functions and Azure Communication Services. This system can send timely and personalized notifications across multiple channels, such as Email, SMS, and WhatsApp. In addition, we have explored how to enhance our system with sophisticated content generation capabilities using OpenAI GPTs.
The modular nature of the Azure Functions framework allows your application to scale and adapt easily to changing requirements and traffic demands. Meanwhile, Azure Communication Services enrich the user experience by meeting customers on their preferred platforms, contributing to a seamless and cohesive communication strategy.
As developers, there's always room to expand our knowledge and add robust features to our applications. Here are some suggestions for further exploration and resources that can assist you in taking your applications to the next level:
- Azure Communication Services AI samples: One stop shop for GitHub samples for AI-powered communication solutions.
- Azure Functions Best Practices: Learn about best practices for designing and implementing Azure Functions by visiting Azure Functions best practices.
- Azure Communication Services Documentation: Explore the full capabilities of Azure Communication Services including chat, phone numbers, video calling, and more on the Azure Communication Services documentation.
- Security and Compliance in Azure: Understand the best practices for security and compliance in Azure applications, particularly relevant for handling sensitive user communication data. Check the Microsoft Azure Trust Center.
- OpenAI GPT Documentation: For more insight into using and customizing OpenAI GPTs, refer to the OpenAI API documentation.
- Azure AI Services: Azure offers a range of AI services beyond just communication. Explore Azure AI services for more advanced scenarios such as speech recognition, machine translation, and anomaly detection at Azure AI services documentation.
- Handling Large-scale Data: To handle a large amount of data and improve the performance of communication systems, consider learning about Azure's data-handling services like Azure Cosmos DB, Azure SQL Database, and Azure Cache for Redis. Start with the Azure Data storage documentation.
- Monitoring and Diagnostics: Improve the reliability of your applications by implementing robust monitoring and diagnostics tools. Azure offers several tools such as Azure Monitor and Application Insights. Dive into Application Insights for Azure Functions.
- Serverless Workflow Automation with Azure Logic Apps: Enhance your serverless applications using Azure Logic Apps to automate and simplify workflows. Learn more about Azure Logic Apps at What is Azure Logic Apps?.
Happy coding!
Published on:
Learn moreRelated posts
Disconnected operations for Azure Local
Introducing the new Linux-based Azure Cosmos DB Emulator (Preview)
We are excited to announce the preview release of the new Linux-based Azure Cosmos DB Emulator! This latest version is built to provide faster...
Azure Cosmos DB Shines at Microsoft Ignite 2024!
Microsoft Ignite 2024 took over the Windy City this week, bringing with it new technological innovation and exciting product announcements apl...