'How to write google function with Twilio to send SMS on value change in Realtime database

I am writing my first google cloud function I don't have in depth knowledge of it. I am trying to write a google cloud Function with Twilio. When ever the value of water change in my Realtime Database it will send a SMS to a given number that the water value of this machine has been changed so the message look like "Water value of "Machine ID" has been changed". I have try to do but I did not find any proper documentation or video on YouTube which Full Fill all the requirement. I was doing research on this from last week I and write some code can anyone please check my code and tell what changes I have to made to get what I want. here is what I have write till now.

exports.testing = functions.https.onRequest((request, response) => {
   var db = admin.database();
    var ref = db.ref("/07D0921210004");//This 07D0921210004 is the machineID
    ref.on("value", function (snapshot) {
      var water = snapshot.val().water;
      const accountSid = process.env.TWILIO_ACCOUNT_SID;
      const authToken = process.env.TWILIO_AUTH_TOKEN;
      const client = require('twilio')(accountSid, authToken); 

      client.messages
  .create({
     body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
     from: '+1501***2661',
     to: '+155586***10'
   })
  .then(message => console.log(message.sid));
     
     
    },);
});

This is my database structure

{
07D0921210004 
{
Date:"value"
Water:"value"
}
}


Solution 1:[1]

When ever the value of water change in my Realtime Database...

This kind of trigger is called background trigger and for the Realtime Database the Cloud Function looks like:

exports.sendSMS = functions.database.ref('{machineId}')
    .onCreate((snapshot, context) => {
      // Value of the node
      const nodeValue = snapshot.val();

      // ....

     });

The code of the Cloud Function in your question is for an HTTPS Cloud Function which is triggered through an HTTP request (similar to a REST API endpoint). So it will not be triggered when an event occurs in the Realtime Database.


So, your should adapt your code along the following lines (untested). Note the use of async/await, in order to take into account the asynchronicity of the create() method and the Cloud Function life cycle management.

exports.sendSMS = functions.database.ref('{machineId}')
    .onCreate(async (snapshot, context) => {
        
      // Value of water
      var water = snapshot.val().water;

      const accountSid = ...
      const authToken = ...
      const client = require('twilio')(accountSid, authToken); 
      
      const message = await client.messages
      .create({
         body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
         from: '+1501***2661',
         to: '+155586***10'
       });
       
       console.log(message.sid)
       return null;

     });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1