GithubHelp home page GithubHelp logo

maxng07 / sg-weather-forecast Goto Github PK

View Code? Open in Web Editor NEW
5.0 0.0 0.0 2.43 MB

Weather forecast via WhatsApp for Singapore

JavaScript 100.00%
twilio nodejs programmable whatsapp weather-forecast serverless serverless-deployments serverless-applications weather-app key-value

sg-weather-forecast's Introduction

# 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

sg-weather-forecast's People

Contributors

maxng07 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.