Advertisement
Guest User

Twilo Incorporation IH

a guest
Apr 14th, 2021
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ih = require('../index')
  2.  
  3. // Author Luke Garceau
  4. // example will use twilio to notify a user when a message has occured on the blockchain
  5. // This program is a watcher for the chain and will send a user a text message whenever the a given device on the chain has a given attribute with the specified value
  6.  
  7.  
  8. // Twilio Variables
  9. const accountSID = "";
  10. const authToken = "";
  11. const client = require("twilio")(accountSID, authToken)
  12.  
  13. // IH Variables
  14. const deviceAdrCheck = "";
  15. const aesSec = "";
  16. const aesIV = "";
  17.  
  18. // Program Variables
  19. const attribute = "lock"
  20. const notifyVal = 1;
  21. const phone = "";
  22. const from = "";
  23.  
  24. // Logic
  25.  
  26. async function sendTxt(val){
  27.     client.messages
  28.         .create({
  29.             body: `Your IoT Device has been locked! And set to ${val}`,
  30.             from: from,
  31.             to: phone
  32.         })
  33.         .then(message => console.log(message.sid))
  34. }
  35.  
  36. async function checkChain(){
  37.     while (true) {
  38.         const rep = await ih.getChainEvents(deviceAdrCheck, aesSec, aesIV);
  39.         for (let index = 0; index < rep.length; index++) {
  40.             const message = array[index];
  41.             try {
  42.                 if(message['attributes'][attribute] == notifyVal){
  43.                     sendTxt(message)
  44.                 }
  45.             } catch (e) {
  46.                 console.log(`Exception in checkChain() ${e}`)
  47.             }
  48.         }
  49.     }
  50. }
  51.  
  52. checkChain()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement