ankitkhandelwal185
ankitkhandelwal185

Reputation: 1121

Send an otp to the desired mobile number using nodejs

I am using Plivo to send sms to the user for otp verification and i am unable to update mobile number and message parameters for different users, here is my code

global.params = {
    'src': 'xx-xxxx', // Sender's phone number with country code
    'dst' : '91'+'xxxxxxxxxx', // Receiver's phone Number with country code
    'text' : "Hi, message from Plivo", // Your SMS Text Message - English
    'url' : "https://intense-brook-8241.herokuapp.com/report/", // The URL to which with the status of the message is sent
    'method' : "GET" // The method used to call the url
};

module.exports={
    foo: function createOTPverify(mobile_number, id){
    var motp = randomize('0',4);
    console.log("motp "+motp);
    global.params.dst = mobile_number;
    global.params.text = "Hi, your verification one time password is "+motp;
    p.send_message(global.params, function(status, response){
    console.log('Status: ', status);
    console.log('API Response:\n', response);
    })
}
}

here everything is working fine but unable to update parameters, i tried it using global but still not able to do, please help

Upvotes: 2

Views: 7453

Answers (1)

Sanjay Achar
Sanjay Achar

Reputation: 408

You're not updating your config array anywhere. Hence, the issue

Check the below gist, it should help you send OTP

https://runkit.com/isanjayachar/plivo-send-otp

const plivo = require('plivo').RestAPI({
  authId: '<your AUTH ID>',
  authToken: '<your AUTH TOKEN>',  
})


function getParmas(phone, otp) {
  return {
    'src': 'xx-xxxx',
    'dst' : '91'+ phone,
    'text' : "Your OTP for verification is " + otp,
    'url' : "https://intense-brook-8241.herokuapp.com/report/",
    'method' : "GET"
  }
}

function sendOTP(phone) {
  var otp;
  // Generate your OTP here
  
  plivo.make_call(getParmas(phone, otp), function(status, response) {
    console.log('Status:' + status)
    console.log('Reponse:' + response)
  })
}

sendOTP('9999999999');

Upvotes: 2

Related Questions