Search code examples
node.jstwilio

How to Pass Data Variable into Twilio Text Body


Using NodeJS server side, I have a variable with data in it called lowestPrice. I want to be able to pass this variable's data into the body of a Twilio text being sent.

Below is what I'm trying but it does not work. Is this possible? If so, how do I accomplish this?

const accountSid = 'abc123';
const authToken = 'def456';
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: 'Current Lowest Price: ' + $lowestPrice ,
     from: '+11234567890',
     to: '+10987654321'
    })
  .then(message => console.log(message.sid));

Many Thanks!


Solution

  • with javascript you can do like this

    const accountSid = 'abc123';
    const authToken = 'def456';
    const client = require('twilio')(accountSid, authToken);
    
    let lowestPrice  = 100;
    
    client.messages
      .create({
         body: `Current Lowest Price: ${lowestPrice}` ,
         from: '+11234567890',
         to: '+10987654321'
        })
      .then(message => console.log(message.sid));
    

    EDIT - Using node.js

    const { toPhoneNum, lowestPrice} = req.body;
    
    client.messages
      .create({
         body: lowestPrice,
         from: '+11234567890',
         to: toPhoneNum
        })
      .then(message => console.log(message.sid));
    

    Now you can pass the Current Lowest Price: 100 message in the body