How to get daily weather notification on mobile using Node.js and Twilio API ?

Looking for weather information daily is a trivial task. As programmers, we can make this task easier by creating a notification service. We will use the Twilio SMS API for sending SMS and Node.js for writing the service which runs every day at a particular time.

Getting Twilio Credentials: To use Twilio APIs, we need to register our app and get the API keys from Twilio’s console. We need to get ACCOUNT SID, AUTH TOKEN, and TRIAL NUMBER. You can get all these credentials from here.

Twilio Console

Save these credentials to .env file in your project’s root directory along with your phone number. Since its a security best practice to save every credential in a separate file.

filter_none

edit
close

play_arrow

link
brightness_4
code

ACC_SID = 'your-account-sid'
AUTH_TOKEN = 'your-auth-token'
  
TO = 'your-number-with-country-code'
FROM = 'twilio-trial-number'

chevron_right


Sending SMS: In order to send SMS, we can use Twilio’s library for Node.js. Install the library using the following command:

npm install twilio

Now let’s test our credentials by sending a message. Add the following code to the index.js file.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Getting Data from .env file
const accountSid = process.env.ACC_SID;
const authToken = process.env.AUTH_TOKEN;
  
const twilio = require("twilio");
const client = new twilio(accountSid, authToken);
  
client.messages
    .create({
        body: "Hello from GeeksForGeeks!",
        to: process.env.TO,
        from: process.env.FROM
    })
    .then(message => console.log(message.sid));

chevron_right


If all goes good then you will receive an SMS from Twilio with the text “Hello from GeeksForGeeks!”.

Getting the weather information: To get the latest weather information, we will use Openweathermap API which is freely available.

To use the API signup on the website and go to the dashboard to generate the API key as follows:

Openweather map.org Dashboard

We will use the Node.js’s request library to fetch the data from the API. Install the dependency using the following command:

npm install request

And then add the following code snippet to the index.js file.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Import request library
const request = require("request");
  
function getdata() {
    request(
    { json: true },
    (err, res, body) => {
        if (err) {
            return console.log(err);
        }
  
        // Printing fetched data
        console.log(body); 
    });
}
  
// Calling function
getData();

chevron_right


Now, create a function to send notification data and add the code for sending SMS like below:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Send message
function sendNotification(msg) {
  client.messages
    .create({
      body: msg,
      to: process.env.TO,
      from: process.env.FROM
    })
    .then(message => console.log(message.sid));
}

chevron_right


Now we have to create a message from the data received from the weather API. Replace the getData() function with the following code:

filter_none

edit
close

play_arrow

link
brightness_4
code

function getdata(){
  request(
    { json: true },
    (err, res, body) => {
        if (err) {
            return console.log(err);
        }
        
        // Create a message
        let msg = `\nToday's Weather : \n${body.weather[0].main}, ${body.main.temp}°C\nHumidity : ${body.main.humidity}%    
      `;
        
        // Calling Send Message function
        sendNotification(msg);
    });
}

chevron_right


Now, to send the message every day, we have to set up a cronjob using the node-cron library.

npm install node-cron

Add the cronjob to call getData() method every day at 8:00 AM:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Importing library
const twilio = require("node-cron");
  
// Cronjob runs everyday at 8:00 AM
cron.schedule("0 8 * * *", () => {
  
    // Calling getData method which
    // calls the send message method
    getData(); 
});

chevron_right


If everything goes fine then you will receive an SMS displaying the weather information.

Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.