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, 2022 in Node-js by Vaani
• 7,070 points
2,257 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, 2022 by Neha
• 9,020 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,840 points
15,047 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, 2022 in Node-js by Neha
• 9,020 points
10,223 views
0 votes
0 answers

How can i make my REST API Faster with nodejs and express?

Summarize the problem My problem: I have built ...READ MORE

Aug 19, 2022 in Node-js by Neha
• 9,020 points
810 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,840 points
3,565 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
1,946 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,470 views
0 votes
1 answer

Otp Verification in nodejs?

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

answered Jun 10, 2022 in Node-js by Neha
• 9,020 points
4,807 views
0 votes
1 answer

How to host MEAN stack application with Angular and nodejs on windows IIS

It's fine that you're using Angular. Be ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
1,364 views
0 votes
1 answer

File Download on NodeJS with use opensubtitles API

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

answered Jun 7, 2022 in Node-js by Neha
• 9,020 points
1,023 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP