Creating COVID19 stats newsletter using Azure Function Apps
Tags: azure, serverless, python
Published: 2021-04-14 07:24:07

Welcome to my blog series **Fun with Function Apps** where we will be learning about function apps their implementation through amazing use cases.

In this blog, we are create two function apps - HTTP Trigger & Timer Trigger which will help us create a newsletter consisting of COVID-19 India daily stats. The users can subscribe/ unsubscribe to the newsletter through the [website](https://bit.ly/azurecovid)

Lets get started...

The architecture of the solution will be as follows![Screenshot 2021-04-14 at 2.16.05 AM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cls0idx269cyf6abmdi.png)

## Pre-requisites
1. Azure Account - (Free)
2. Basic Python Programming
3. Basic Web Development Skills (HTML, CSS & JS)

```
Warning: Kindly be aware of the incurring costs 
```

### Step 1. Setting up Storage Account

Go to Azure Portal and create a storage account. Under the storage account, create a table with ParitionKey, RowKey and email as its columns. 
Make note of the table name, storage account name, key for the later use. 

![Screenshot 2021-04-14 at 12.00.07 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkdiwlwqskxplkufrao6.png)

### Step 2. Creating HTTP Trigger Function App

The purpose of the HTTP Trigger Function App is to perform subscribe and unsubscribe actions and the respective data will be updated in the Storage Account table. 

Follow the steps given here to create a [Python HTTP Trigger Function App](https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-python)

Replace the init.py code with the following and substitute all the necessary requirements. 

{% gist https://gist.github.com/Vivek0712/b8494b9401362fd15a9578ad4b7f724b %}

After deploying your app, copy the function url.

To subscribe sample@sample.com 
< function url > /api/HttpTrigger?action=subscribe&email=sample@sample.com

To unsubscribe sample@sample.com
< function url > /api/HttpTrigger?action=unsubscribe&email=sample@sample.com
 
### Step 3: Create SendGrid Account

To send the mails, you need require SendGrid Account. Follow the [steps](https://docs.microsoft.com/en-us/azure/sendgrid-dotnet-how-to-send-email) to generate your SendGrid API Key.

### Step 4: Create Timer Trigger Function App

Follow the steps in this tutorial to create a Timer Trigger Function App. Also, make sure you select Python Run Time. 
We need to send the COVID-19 email newsletter at 6pm IST to all the subscribers.

Use the [NCRONTAB](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=csharp#ncrontab-expressions) schedule : 0 30 12 * * *

Add the SendGrid bindings to your function.json file

```
{
  "scriptFile": "__init__.py",
  "version": "2.0",
  
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 30 12 * * *"
    },
    {
      "type": "sendGrid",
      "name": "sendGridMessage",
      "direction": "out",
      "apiKey": "sendgrid_api",
      "from": "vivekraja98@gmail.com"
    }
  ],
  "disabled": false
}
```
The init.py to send the emails on schedule is as follows 

{% gist https://gist.github.com/Vivek0712/234475d80b1ec7e0ca84e2efeb37c738 %}

Now navigate to your Function App in the azure portal -> Configuration. Under Application settings, create a key "sendgrid_api" and paste the Send Grid API Key you had generated in the previous step.

### Step 5: Creating & Deploying the website to Azure Static Web App Service

Create a simple website where the user can enter his email address to subscribe/ unsubscribe. Call the respective HTTP Trigger Function URL for the same. 

![Screenshot 2021-04-14 at 12.33.12 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/op45s6hz5vhzthar3foe.png)
 
Create a Github Account and push all your source code. 

Now to deploy you web app to Azure Static Web App Service. 

1. Create a repository in Github and upload all the source files.
2. Go to Azure Portal -> Azure Static Web App (Preview) -> Create
3. Enter the relevant details and sign up with your Github Account for the deployment details.
4. Select your repo and the branch.
5. Create

Once your app is deployed, you will be able to see the URL of the website. Copy the URL and navigate to the HTTP Trigger Functions. Under it, go the CORS and paste your website URL.

## Congratulations! You have created a COVID-19 stats Newsletter.

Feel free to fork my GitHub Repo to come up with more interesting use cases. {% github Vivek0712/covid-newsletter no-readme%}

If you have made to this far end of the article, like and share to your fellow devs. For any doubts, help, suggestions and feedback, reach out to me through [Twitter](https://twitter.com/VivekRaja007)