2. Working with notifications using the SendGrid and Twilio services
In this chapter, we will look at the following:
- Sending an email notification using SendGrid service
- Sending an email notification dynamically to the end user
- Implementing email logging in Azure Blob Storage
- Modifying the email content to include an attachment
- Sending an SMS notification to the end user using the Twilio service
One of the key features required for the smooth running of business applications is to have a reliable communication system between the business and its customers. The communication channel usually operates two-way, by either sending a message to the administrators managing the application or by sending alerts to customers via emails or SMS to their mobile phones.
Azure can integrate with two popular communication services: SendGrid for emails, and Twilio for working with text messages. In this chapter, we will learn how to leverage both of these communication services to send messages between business administrators and end users.
Figure 2.1 is the architecture that we will be using for utilizing SendGrid and Twilio Output Bindings with HTTP and queue triggers:
- Client applications (web/mobile) make Http Requests, which trigger the Http Trigger.
- The Http Trigger creates a message to the Queue.
- A Queue Trigger is invoked as soon as a message arrives...
Sending an email notification using SendGrid service
In this recipe, we will learn how to create a SendGrid output binding and send an email notification, containing static content, to the website administrator. Since our use case involves just one administrator, we will be hard-coding the email address of the administrator in the To address field of the SendGrid output (message) binding.
We'll perform the following steps before moving on to the next section:
- We will create a SendGrid account API key from the Azure portal.
- We will generate an API key from the SendGrid portal.
- We will configure the SendGrid API key with the Azure Function app.
In this section, we'll be creating a Send service and also generate the API by performing the following steps:
- Navigate to the Azure portal and create a SendGrid Email Delivery account by searching for it in the marketplace...
Sending an email notification dynamically to the end user
In the previous recipe, we hard-coded most of the attributes related to sending an email to an administrator as there was just one administrator. In this recipe, we will modify the previous recipe to send a
Thank you for registration email to the users themselves.
- The SendGrid account is created and an API key is generated in the SendGrid portal.
- An App settings configuration is created in the configuration of the function app.
- The App settings key is configured in the SendGrid output (message) bindings.
How to do it…
Let's make changes to the
RegisterUser function to accept the
Implementing email logging in Azure Blob Storage
Most of the business applications for automated emails are likely to involve sending emails containing various notifications and alerts to the end user. At times, it is not uncommon for users to not receive any emails, even though we, as developers, don't see any error in the application while sending such notification alerts.
There might be multiple reasons why such users might not have received the email. Each of the email service providers has different spam filters that can block the emails from the end user's inbox. As these emails may have important information to convey, it makes sense to store the email content of all the emails that are sent to the end users, so that we can retrieve the data at a later stage for troubleshooting any unforeseen issues.
In this recipe, you will learn how to create a new email log file with the
.log extension for each new registration. This log file can be used as redundancy for...
Modifying the email content to include an attachment
In this recipe, you will learn how to send a file as an attachment to the registered user. In our previous recipe, we created a log file of the email content, which we will use as an email attachment for this instance. However, in real-world applications, you might not intend to send log files to the end user.
How to do it...
- Make the changes to the code to create a log file with the
RowKeyof the table. We will achieve this using the IBinder...
Sending an SMS notification to the end user using the Twilio service
In most of the previous recipes of this chapter, we have worked with SendGrid triggers to send emails in different scenarios. In this recipe, you will learn how to send notifications via text messages, using one of the leading cloud communication platforms, named Twilio.
Twilio is a cloud communication platform-as-a-service platform. Twilio allows software developers to programmatically make and receive phone calls, send and receive text messages, and perform other communication functions using its web service APIs. Learn more about Twilio at https://www.twilio.com/.
- Create a trial Twilio account at https://www.twilio.com/try-twilio.
- Following the successful creation of the account, grab the ACCOUNT SID and AUTH TOKEN from the Twilio Dashboard and save it for future reference, as...