/curtains

🌅 Receive a customised SMS every morning using Twilio, Amazon CloudWatch Events & AWS Lambda.

Primary LanguageJavaThe UnlicenseUnlicense

Curtains

🌅 Receive a customised SMS every morning using Twilio, Amazon CloudWatch Events & AWS Lambda.

Example Output

GitHub release (latest by date including pre-releases) GitHub CodeFactor

Table of Contents

Motivation

Waking up, I would frequently get distracted, checking other apps after I'd looked at the weather on my phone. Since this app uses SMS, I can still stay informed about the day and prevent distraction from happening by keeping my phone offline until I'm ready to start the day.

Features

  • Weather Reporting: actual temperature, weather condition, humidity, precipitation, wind & sunset time.
  • News: today's top headline with link to story & historic headline with link to story.
  • Affirmation: an affirmation.

Weather and news are customisable. App design allows for new components to be added easily.

Source Contents

The project source includes function code and supporting resources:

  • CurtainsFunction/src/main - Java code to run the function.
  • CurtainsFunction/src/test - Unit tests.
  • CurtainsFunction/pom.xml - A Maven build file.

Use the following instructions to deploy the application.

How to Use?

Prerequisites

Setup Guide

Before Starting

  • Depending on the size and frequency that you run your function you may incur some AWS cost, although this is very unlikely unless you significantly change the application or run it multiple times per day. To check use this calculator.
  • Sending SMSs via Twilio will eventually incur a cost. For the purpose of testing this app you can use Twilio's trial credit, but ongoing use will require payment. See Twilio's Programmable SMS pricing page for more details.

Code

This part of the guide is based upon using IntelliJ IDEA, however, the steps should be extremely similar for other IDEs. If you have any issue feel free to reach out.

  1. Complete all of the requirements.
  2. Clone this repo.
  3. Open IntelliJ IDEA > on the main menu: 'Open or Import' > select pom.xml from the cloned repo and click 'Open'.
  4. Edit the MY_TIMEZONE variable in Config.java.
  5. Edit Wttr.java to adjust weather details following the internal documentation and comments.
  6. Edit News.java to adjust news details and lookback period following the internal documentation and comments.
  7. Edit DailyMessage.java if changes were made in steps 5/6 following the internal documentation and comments.
  8. Add the environment variables listed in Config.java to IntelliJ's run configuration for testing. Note that these do not need "" string qualifiers.
  9. Add the code below to DailyMessage.java.
    public static void main(String[] args) throws IOException {
        String todaysMessage = new DailyMessage().getTodaysMessage();
        System.out.println(todaysMessage);
        // new TwilioSMSSender().sendMessage(todaysMessage);
        // System.out.println("Message sent successfully!");
    }

Debug and run with comments to test that your message is formatted correctly. Uncommenting the two lines will send the message to Twilio and then to your RECEIVER_PHONE_NUMBER. This will charge your Twilio account, however, you should have trial credit.

  1. If you received the message correctly, delete the code above from DailyMessage.java and edit the pom.xml file, remembering to change this author line.
  2. Open Intellij's Terminal and write mvn package, then hit ctrl + Enter. Maven should build the project and hopefully display a 'BUILD SUCCESS' message.
  3. If this happened, congratulations you can move to the next section!

AWS

  1. Login to the AWS Console.
  2. Search for and select 'Lambda'. Then select your region.
  3. Select 'Create function' > 'Author from scratch' > set the function name as 'curtainsFunction' > set the runtime as 'Java 11' and press 'Create function'.
  4. Go to 'Function code', click 'Actions' > select 'Upload a .zip or .jar file' > find your project's 'target' folder and look for a file called something like: Curtains-1.0-SNAPSHOT.jar. Select this and hit upload.
  5. Click 'Edit environment variables'. Add the environment variables here as you did in step 8, once done press 'Save'. Note: you may want to select 'Encryption in transit'. To see more on how to configure this security feature please see 'Securing environment variables'.
  6. Scroll down to 'Basic settings', select 'Edit' > change the 'Handler' to curtains.DailyMessageHandler::eventHandler > set the 'Timeout' to 40sec > set 'Memory (MB)' to 192 MB and leave 'Execution role' as 'Use an existing role' > press 'Save'. Note that if you have made significant edits to the program you may need to increase both the 'Timeout' and 'Memory (MB)' limits.
  7. Select 'Add trigger' > search for 'EventBridge (CloudWatch Events)' > for 'Rule' click ' 'Create a new rule' > provide a descriptive name > set 'Rule type' as 'Schedule expression' > set the 'Schedule expression' depending on when you want to receive the message, for example, daily at 8am UTC is: cron(0 8 ? * * *) > leave 'Enable trigger' unchecked > press 'Add'. Note: AWS does not use standard unix or quartz cron expression formats. More detail can be found here.
  8. Press 'Select a test event' > 'Configure test events' > 'Create new test event' and select 'Amazon CloudWatch' > provide an 'Event name' and press 'Create'.
  9. You are now ready to test your app! Press 'Test', after some time you should be shown the message 'Execution result: succeeded' and receive your message.
  10. If this is the case, enable the CloudWatch Events trigger and voila! You have a working, automated SMS app!

To Do

  • Implement unit tests for functionality that uses environment variables.

Acknowledgments

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

Unlicense