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
Top comments (25)
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.
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.
It is. Keep going. We can work towards something that is available online for people around the world.
Thank you for the kind words and feedback.
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!
Thanks for that insight Madza! Also, I appreciate the kind feedback. I hope you have a great day.
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}. 😄😄
Great idea! I'm planning on doing something similar. I'm excited to see the end product. 😄
Just checked your project, very very cool! Can't wait to see end product.
Awesome! I love the recent surge I'm seeing in the positive psych approach to gathering info.
Thanks Glenn! I agree, anything we can do to boost positivity is a win!
I LOVE THIS.
Thanks for checking it out :)
Great idea! :D
Thanks :)
I like this wholesome/positive approach of covid news aggregation, would be nice to see more this kind of focus.
I agree! We need a little more light at the end of the tunnel.
This is such a great idea!
I think many of us are struggling with the overload of news everyday, especially bad news.
It definitely gets to me. Thanks for the kind words.
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.
I have a few more videos on my YouTube channel on azure functions if you’re interested :) thanks for the kind words and feedback.
Nice writeup!
Thank you Phillip