Send SMS from NODE JS and EJS template engine using twilio


    INTRODUCTION

    Sending sms is enough common in ultramodern operations and it can be complex. still, with spring charge and twilio, it’s just a piece of a cutlet. We can see our app being able of transferring sms in just many twinkles and you can work to any position you want. 
     
     Sending SMS currently in utmost of the operations is enough common thing to confirm identity like transferring secret canons, two factor authentication, order dispatches from different shopping spots and so on. Twilio is one of the similar sms providers with large request share and numerous companies are using it as it's presumably one of the easy to apply services in utmost of the programming languages and indeed beyond that. In this composition I'll show you how to shoot sms using twilio service in Java operation. For this purpose I'll be using Spring charge along with java to make it indeed realistic and practical.

    PREREQUISITE

    - NODE JS and NPM should have already installed in your computer.
    -Knowledge how to use the third-party library
    - Basic knowledge of 
    • NODE, EXPRESS
    • nodemon 
    and after creating project install express, twilio package on your project by following commands:
    npm install express
    npm install twilio
    and other necessary packages


    CREATING AND SETTING UP TWILIO ACCOUNT

    We have to create twilio account from twilio website. After creating your account you will get the following credentials 
    • ACCOUNT SID
    • AUTH TOKEN
    • VIRTUAL TRIAL NUMBER

    CREATING PROJECT

    Create a NOJE.JS project by entering the command "npm init -y" ,  Here -y is optional
    After hitting the command you get package.json and following response in terminal


    CODE EXAMPLES

    • Project Structure


    • Add   "type": "module"  in package.json file  and Add  "start": "nodemon ."  inside 'scripts' components in package.json file .
    • creating server , routes and defining constants
      
    import express from 'express'
    import twilio from 'twilio'
    import bodyParser from 'body-parser'
    
    const sid="AC63e1963378f6fa76fb2f8fc53222cc1a"
    const token="ae9a25b533d7c5511014444b313873c0"
    const from=+19854411173
    const client=twilio(sid,token)
    
    const PORT=5000;
    const app=express();
    app.use(express.json())
    app.use(bodyParser.urlencoded({extended:true}))
    
    app.set('view engine','ejs')
    
    app.get('/',(req,res)=>{
        res.render('send')
    })
    
    app.post('/send',(req,res)=>{
    })
    
    app.listen(PORT,()=>{
        console.log('Server Started...');
    })
    
    
    • coding sms sending inside routes
      
    const to=req.body.destnum
    const body=req.body.msg
    client.messages
     .create({from,to,body})
     .then(()=>{
        res.send('SMS sent successfylly')
      })
      .catch((err)=>{
      console.log(err);
    })
        
    • creating frontend: send.ejs
      
      <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Send SMS</title>
    </head>
    <body>
        <div class="container" style="width: 38rem;">
    
        <form method="post" action="/send">
            <legend>SEND SMS</legend>
    
            <div class="mb-3">
              <label class="form-label">To</label>
              <input type="number" name="destnum" required class="form-control" placeholder="number">
            </div>
            <div class="mb-3">
                <label class="form-label">Message</label>
                <input type="text" name="msg" class="form-control" placeholder="Message">
            </div>
            <button type="submit" class="btn btn-primary">Send</button>
          </form>
        </div>
    
    </body>
    </html>
    
    

    RUNNING A PROJECT

    Run Your Project by Simply typing "npm start" in terminal


    RESUTL/ OUTPUT

    To send sms you have to enter your phone nuber with country code and '+' sign




    0 Comments