π Receive a customised SMS every morning using Twilio, Amazon CloudWatch Events & AWS Lambda.
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.
- 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.
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.
- An AWS account.
- A Twilio account.
- A News API API Key.
- A The Guardian Open Platform API Key.
- A Java 11 runtime environment.
- Maven.
- 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.
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.
- Complete all of the requirements.
- Clone this repo.
- Open IntelliJ IDEA > on the main menu: 'Open or Import' > select
pom.xml
from the cloned repo and click 'Open'. - Edit the
MY_TIMEZONE
variable in Config.java. - Edit Wttr.java to adjust weather details following the internal documentation and comments.
- Edit News.java to adjust news details and lookback period following the internal documentation and comments.
- Edit DailyMessage.java if changes were made in steps 5/6 following the internal documentation and comments.
- Add the environment variables listed in Config.java to IntelliJ's run configuration for testing. Note that these do not need "" string qualifiers.
- 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.
- 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.
- Open Intellij's Terminal and write
mvn package
, then hitctrl + Enter
. Maven should build the project and hopefully display a 'BUILD SUCCESS' message. - If this happened, congratulations you can move to the next section!
- Login to the AWS Console.
- Search for and select 'Lambda'. Then select your region.
- Select 'Create function' > 'Author from scratch' > set the function name as 'curtainsFunction' > set the runtime as 'Java 11' and press 'Create function'.
- 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. - 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'.
- 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.
- 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. - Press 'Select a test event' > 'Configure test events' > 'Create new test event' and select 'Amazon CloudWatch' > provide an 'Event name' and press 'Create'.
- 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.
- If this is the case, enable the CloudWatch Events trigger and voila! You have a working, automated SMS app!
- Implement unit tests for functionality that uses environment variables.
- A big thank you to Matthew Gilliard for the code & walk through in 'Wake up to Useful Daily Messages with Java and Azure Functions'.
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.