A serverless app that texts me positive COVID-19 news

Gwyneth PeΓ±a-Siguenza - Apr 8 '20 - - Dev Community

About

I built a serverless application that sends me a positive news story related to COVID-19 every morning before I wake up, via SMS.

During this ongoing pandemic, my problem has been too much news consumption, and a lot of it impacts me negatively. I figured now would be an excellent opportunity to create something that brings a little positivity into my day.

Category

Interesting Integrations.

Github repo

GitHub logo madebygps / daily-positive-news-sms-serverless

A serverless app that sends via text message a positive news story about COVID-19.

Video on how to get the project working locally (text instructions provided below if you prefer that)

πŸ‘©πŸ½β€πŸ’» How to setup code environment

Follow this tutorial and you will install VS Code and the necessary Azure extensions needed.

πŸ›  Setup API keys and credentials

You will need:

πŸ“¦ Packages used

These should be included in the project when you clone it, however, if there is some error, you can reinstall them.

Twilio

Install via .NET CLI

dotnet add package Twilio
Enter fullscreen mode Exit fullscreen mode

Use

using Twilio;
using Twilio.Rest.Api.V2010.Account;
Enter fullscreen mode Exit fullscreen mode

TextAnalytics v3 preview

Install via .NET CLI

dotnet add package Azure.AI.TextAnalytics --version 1.0.0-preview.3
Enter fullscreen mode Exit fullscreen mode

Use

using Azure.AI.TextAnalytics;
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ How to setup local.settings.json

I've excluded my local.settings.json file for obvious reasons. Make sure to include these records in there once you have them.

Microsoft timezone documentation

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<replace_with_your_webjobsstorage>",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "TextAnalyticsApiKeyCredential":"<replace>",
    "CognitiveServicesEndpoint":"<replace>",
    "TwilioSid":"<replace>",
    "TwilioAuthToken":"<replace>",
    "NewsApiKey":"<replace>",
    "TwilioPhoneNumber":"<replace>",
    "MyPhoneNumber":"<replace_with_number_you_ant_to_send_sms_to>",
    "WEBSITE_TIME_ZONE":"<replace_with_your_timezone"
  }
}
Enter fullscreen mode Exit fullscreen mode

⚑️ How to execute locally

In VS code, select the run Tab on the left, then hit the Play button on the top.

How to run

What is RunOnStartUp?

The app will run once since the

RunOnStartup=true
Enter fullscreen mode Exit fullscreen mode

is set to true. Before deploying to production, remove this.

πŸ“³ Demo

You will get a text to the number you put into your local.settings.json

SMS text

In the VS code console output, you will also see the story it sent you.

Console output

You will also see it in your Twilio SMS dashboard

Twilio dash

πŸš€ How to deploy to Azure

Here is a written tutorial on how to Publish a Function to Azure

My Youtube video also shows how to do this.

Make sure to remove RunOnStartUp in the trigger or set to false. See this Microsoft doc

⏰ Change what time the app runs

This line here has the CRON expression

public static void Run([TimerTrigger("0 30 6 * * *", RunOnStartup=true)]TimerInfo myTimer, ILogger log)
Enter fullscreen mode Exit fullscreen mode

If you would like to change the time, change the expression part, here are some examples.

"0 30 6 * * *"
Enter fullscreen mode Exit fullscreen mode

πŸ–Ό MMS capabilities

Since I am in the US, I can send images to my phone number, more info here, that is done in the send message method

static void SendMessage (string fromNumber, string toNumber, string articleUrl, string articleTitle, string imageUrl )
Enter fullscreen mode Exit fullscreen mode

Feel free to remove this if you are outside of US or Canada.

If the image has no article URL, it will default to a stock photo I got from Unsplash

πŸ—ž Fine-tune your newsfeed

You can fine tune the JSON returned from News API with these parameters simply add/remove/edit the variables of the newsAPIEndpointURL


// NEWS API Search parameters and URL
string searchKeyword = "Covid";
string sortBy = "relevancy";
string pageSize = "100";
string searchLanguage = "en";
string fromDate = DateTime.Today.AddDays (-1).ToString ("yyyy-MM-dd");
var newAPIEndpointURL = $"https://newsapi.org/v2/everything?from={fromDate}&sortBy={sortBy}&pageSize={pageSize}&language={searchLanguage}&q={searchKeyword}&apiKey={newsApiKey}";
Enter fullscreen mode Exit fullscreen mode

πŸ‘·πŸ½β€β™€οΈ Known issues and areas of improvement

  • Some of the stories sent are not necessarily positive, but since they contain words like "tests positive" or "better" they are returned as a positive sentiment. Tweaks to the sentiment labeling method and exploring more of text analytics could better this. I've added some examples below.

  • I haven't been programming for very long so I know I might not be following best practices (OOP design and error handling), I will try to improve that as I get more practice and experience.

  • I was getting this Twilio error with certain articles, due to their image size, I implemented a method to check the article image size

static double GetMediaFileSize (string imageUrl) {
            var fileSizeInMegaByte = 0.0;
            var webRequest = HttpWebRequest.Create (imageUrl);
            webRequest.Method = "HEAD";

            using (var webResponse = webRequest.GetResponse ()) {
                var fileSize = webResponse.Headers.Get ("Content-Length");
                fileSizeInMegaByte = Math.Round (Convert.ToDouble (fileSize) / 1024.0 / 1024.0, 2);
            }

            return fileSizeInMegaByte;
        }
Enter fullscreen mode Exit fullscreen mode

in case the image is larger than 4.9MB, I set the article image to a default image that I know is correctly sized. An improvement here would be to resize the image instead of changing to a default one.

πŸ’™ Thanks to

πŸ‘€ More examples

msg5
msg6
msg6

. . . . . . . . . . . . . . . . . . . . . .