DEV Community

loading...
Cover image for A serverless app that texts me positive COVID-19 news

A serverless app that texts me positive COVID-19 news

madebygps profile image Gwyneth Peña S. Updated on ・5 min read

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

Use

using Twilio;
using Twilio.Rest.Api.V2010.Account;

TextAnalytics v3 preview

Install via .NET CLI

dotnet add package Azure.AI.TextAnalytics --version 1.0.0-preview.3

Use

using Azure.AI.TextAnalytics;

🔑 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"
  }
}

⚡️ 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

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)

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

"0 30 6 * * *"

🖼 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 )

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}";

👷🏽‍♀️ 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;
        }

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

Discussion (25)

pic
Editor guide
Collapse
bernardbaker profile image
Bernard Baker

Great use of Twilio ⚡. It could reach millions. And positive news is always a good thing 😍.

Is it available online? And have you thought about using news providers in different countries for other languages? So many great ideas.

Collapse
madebygps profile image
Gwyneth Peña S. Author

Yes it’s open source available on my github. So many great ideas you have! I’ll continue to grow the project, I think it’s a good start at the moment.

Collapse
bernardbaker profile image
Bernard Baker

It is. Keep going. We can work towards something that is available online for people around the world.

Thread Thread
madebygps profile image
Gwyneth Peña S. Author

Thank you for the kind words and feedback.

Collapse
madza profile image
Madza

For those looking to buy, "stock market bears reward patience" will be a positive one, for those to sell - negative :) There will be several opportunities to buy quality names at bargain prices over the course of the next six months.

Good job on developing the app, also nice YT channel!

Collapse
madebygps profile image
Gwyneth Peña S. Author

Thanks for that insight Madza! Also, I appreciate the kind feedback. I hope you have a great day.

Collapse
madza profile image
Madza

You should prolly make another app to combat negative ones, lol.. 😄
It scans through all the news, finds all the negative ones, detects author of each post, scraps his/her email, sends message to it: I did read your article {article_name} in {source_name}. I will not stop to spam you, unless you do in {source_name}. 😄😄

Collapse
r0b profile image
Robert Osborne

Great idea! I'm planning on doing something similar. I'm excited to see the end product. 😄

Collapse
madebygps profile image
Gwyneth Peña S. Author

Just checked your project, very very cool! Can't wait to see end product.

Collapse
glennchon profile image
Glenn

Awesome! I love the recent surge I'm seeing in the positive psych approach to gathering info.

Collapse
madebygps profile image
Gwyneth Peña S. Author

Thanks Glenn! I agree, anything we can do to boost positivity is a win!

Collapse
graciegregory profile image
Collapse
madebygps profile image
Gwyneth Peña S. Author

Thanks for checking it out :)

Collapse
rhymes profile image
rhymes

Great idea! :D

Collapse
madebygps profile image
Collapse
huncyrus profile image
huncyrus

I like this wholesome/positive approach of covid news aggregation, would be nice to see more this kind of focus.

Collapse
madebygps profile image
Gwyneth Peña S. Author

I agree! We need a little more light at the end of the tunnel.

Collapse
lamb003 profile image
Chad Lamb

This is such a great idea!
I think many of us are struggling with the overload of news everyday, especially bad news.

Collapse
madebygps profile image
Gwyneth Peña S. Author

It definitely gets to me. Thanks for the kind words.

Collapse
bgibb95 profile image
Brendan

This is awesome! Thanks so much for sharing. The sentiment analysis is a great idea.

This is my first exposure to azure functions, I typically use GCP. Very interesting.

Collapse
madebygps profile image
Gwyneth Peña S. Author

I have a few more videos on my YouTube channel on azure functions if you’re interested :) thanks for the kind words and feedback.

Collapse
shimphillip profile image
Phillip Shim

Nice writeup!

Collapse
madebygps profile image
Gwyneth Peña S. Author

Thank you Phillip

Collapse
monfernape profile image
Usman Khalil

Loved the idea. I've been struggling learning twilio. Hope this one helps

Collapse
madebygps profile image
Gwyneth Peña S. Author

Thank you, friend! If you need any help, I might be able to provide some guidance, reach out at any time.