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
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:
- TwilioSid, TwilioAuthToken, TwilioPhoneNumber
- Azure account, CognitiveServicesEndpoint and TextAnalyticsApiKeyCredential Use the Free - Web/Container plan, it contains the features we need, at a free tier, more info here
- NewsApiKey
๐ฆ Packages used
These should be included in the project when you clone it, however, if there is some error, you can reinstall them.
Twilio
dotnet add package Twilio
Use
using Twilio;
using Twilio.Rest.Api.V2010.Account;
TextAnalytics v3 preview
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.
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
In the VS code console output, you will also see the story it sent you.
You will also see it in your Twilio SMS dashboard
๐ 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
- The amazing community at Dev.to and Twilio. Here is the link to the hackathon post if you'd also like to participate (Do it ๐).
- Powered by newsapi.org
- Twilio for providing credits to work on this project
- Stock newspaper photo from AbsolutVision