Skip to content

maxng07/sg-weather-forecast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

# Singapore-weather-forecast
Weather forecast via WhatsApp in Singapore 

This is a communication app using the popular messenging WhatsApp. The whatsapp API and the backend Function as a Service and Storage are all using Twilio platform. Technically, you can move the Function and storage to another Cloud Platform and use just the Twilio Whatsapp API. This is a mini project to test out Twilio programmable SMS/Voice, WhatsApp was chosen because its popular in SG and its cost free to send message in Whatsapp versus international SMS (no SG phone number (E164) available in trial account).

Twilio Function is Function as a Service, a Serverless event triggered runtime environment that runs Nodejs, so the code is in Node/JS.
The function checks the datasource stored in Twilio Assets, at this time, Twilio does not allow remote API to update the datasource file except via its console. So manual process has to be done. If other Cloud Platform is used for Function and storage, this should be able to be automated.

The backend data feed is from NEA / Govtech API, https://data.gov.sg/dataset/weather-forecast
NEA/Gov Tech does not have metadata for the JSON data and is a dump of the complete set of datas, including area regions, location via GPS (latitude/longtitude), weather forecasts by areas, date/time. They offer 2 sets of data, either via date or date_time. The data response is in JSON string.

You can directly invoke the Twilio Function by accessing via HTTPS instead of Twilio WhatsApp API to invoke the API for testing and debugging purposes. The Function can also be used for browser application.

Twilio Function has a runtime of 5s, otherwise the function will be terminated. If you need to loop through an array or fetch from an external source, care should be taken to ensure the 5s limit is not reached. Nodejs code has to be simplified and clean.

Twilio programmable WhatsApp has the webhook event to trigger function via https. When message arrives on the WhatsAPP API, trigger a URL. At this time of writing, Twilio function and assets are using AWS lambda, this might changed in the future.

The pdf version provides more information on the setup using Twilio Platform, programmable SMS, Function and Assets. The nodejs codes are in weather.js and the sample datasource are in weather.json. Note that weather.js have taken the 5s runtime limitation into consideration. 

weather.js logic :- open the weather.json file hosted in Twilio Asset, compare the incoming HTTP body for
1. ALL - prints out the complete JS array objects in JSON array. Currently Twilio Messaging Platform has a length limit of 1600 characters. The raw array would exceed this length. 
2. Specific Area - compare the specific area and print out the matching value JSON array JS object
3. If there is no match, return null
This can be further enhance to guide users to select the right area/region.

The callback uses Twilio event - message response.

The pdf is a more detailed explanation on setting this up on Twilio Platform, using the Function (Serverless Function as a Service), Assets and WhatsApp Communication.
*********************** 13/01/2019 ************************
Enhancement: Added 2 new enhancement 
1. Added the ability to ask what are the regions/Area that the datasource contains weather forecast by sending "Area"
2. Customised the reply message for List of Areas vs Weather Forecast