How to use textLocal API with NodeJS and sequelize to send OTP on mobile

0 votes
I wanted to use textLOcal in nodejs with Sequelize to send OTP to mobile numbers.
May 27 in Node-js by Vaani
• 7,020 points
139 views

1 answer to this question.

0 votes

Use request npm to create a http call.

request-promise —save npm I npm I npm I npm I npm I n

Create a folder called "services" and a file called "TextLocalSMS.js" for the class "TextLocalSMS.js."

var httprequest = require('request-promise');

module.exports = class TextLocalSMS {

constructor() {
}

async callApi(METHOD,URL,BODY){
    var options = {
        method: METHOD,
        uri: URL,
        headers:{
             'Content-Type': 'application/json',
             'Cache-Control': 'no-cache'
        },
        json:true
    };
    
    let response= await httprequest(options).then((result)=>{
        console.log(`SMS API: ${URL} RESULT =`,result);
        return result; 
    }).catch(function (err) {
        console.log(`SMS API: ${URL} ERROR =`,err.message);
        return err;
    });
    return response;
}

async sendSMS(toNumbers,rawMessage){
    let url = 'https://api.textlocal.in/send/';
    let sender = encodeURIComponent('TXTLCL');
    let encoded_message = encodeURIComponent(rawMessage);
    let body={
        apikey:'API_KEY',
        numbers:toNumbers.join(','),
        sender:sender,
        message:encoded_message
    };
    let result = await callApi('POST',url,body);
    return result;
}

}

Now require this file when you want to use it , suppose in app.js

var TextLocalSMS = require('./services/TextLocalSMS');

app.get('/demo',async (req,res)=>{

     // add logic for get user's phone nubmer from database here 
     // ...


     let otp = Math.floor(100000 + Math.random() * 900000);
     let text_msg = `MyWebsite.com -  OTP : ${otp} for reset your account password .`;
     let toNumbers = ['918123456789'];
     let smsService = new TextLocalSMS();
     let smsSent = await smsService.sendSMS(toNumbers,text_msg);

     // database query to store OTP with user_id in your database if otp successfully send to user 
     // ...


     return res.json(smsSent);
})
answered May 27 by Neha
• 8,560 points

Related Questions In Node-js

0 votes
1 answer

How to use an include with attributes with sequelize?

Hello @kartik, Something like this should work foo.findAll({ ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,740 points
7,098 views
0 votes
1 answer

How to make login with Nodejs and MongoDB?

MongoDB Login and Registration with Node js ...READ MORE

answered Jun 16 in Node-js by Neha
• 8,560 points
355 views
0 votes
1 answer

How to programmatically send a 404 response with Express/Node?

Hello @kartik, The method is: res.sendStatus(404); Express will send a ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,740 points
1,791 views
0 votes
1 answer

How to create an Excel File with Nodejs?

Hello @kartik, Just create a file with Tabs ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,740 points
1,147 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
998 views
0 votes
1 answer
0 votes
1 answer

Otp Verification in nodejs?

Yes, a package named sendotp exists. This ...READ MORE

answered Jun 10 in Node-js by Neha
• 8,560 points
23 views
0 votes
1 answer
0 votes
1 answer

File Download on NodeJS with use opensubtitles API

The problem is the charset output (default ...READ MORE

answered Jun 7 in Node-js by Neha
• 8,560 points
95 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP