Sending SMS with Twilio

What You'll Need

Backbench account - Sign Up, the personal account will always remain free to use.

Twilio account - Its free to Sign Up and trial account would be sufficient to try this example. Twilio gives you one number for free to send text messages from. You can only text or call the pre-verified numbers. If you want to test with more numbers, you'll need to verify each one. The restriction is lifted when you upgrade the account.

Prerequisites for the app

  1. Sign In to Backbench account.

  2. Select +, in the upper right corner to create a Bench. For example, say "bench_one" and select CREATE or hit Enter.

Frontend

  1. Select +, in the upper right corner of file manager to create a html file. For example, say "index.html" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

  3. Select save.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>SMS service</title>
</head>
<body>
<div id="contact">
    <h1>Send an SMS</h1>
    <form action="/tw" method="post">
        <fieldset>
            <label for="name">Name:</label><br>
            <input type="text" id="name" name="name" placeholder="Enter your full name" /><br><br>

            <label for="phnum">Mobile number with country code:</label><br>
            <input type="num" id="num" name="num" placeholder="Enter your number" /><br><br>

            <label for="message">Message:</label><br>
            <textarea id="message" placeholder="Message" name="message"></textarea><br><br>

            <input type="submit" value="Send message" />
        </fieldset>
    </form>
</div>
</body>
</html>

Dependencies

Only for nodejs. Create a module package.json and paste following code.

package.json
{
   "dependencies":{
      "twilio":"2.1.1"
   }
}

Backend

  1. Select +, in the upper right corner to create a Module. For example, say "send_sms.js" and select CREATE or hit Enter.

  2. Copy and paste the code module from below.

  3. Replace ACCOUNTSID with your accountsid(Dashboard).

  4. Replace AUTHTOKEN with your AuthToken(Dashboard).

  5. Replace TWILIO NUMBER with the generated number from Twilio account.

  6. Select save.

sendsms.js
const accountSid = "ACCOUNTSID";
const authToken = "AUTHTOKEN";

// require the Twilio module and create a REST client
const client = require("twilio")(accountSid, authToken); 

module.exports.endpoint= function(req,cb) {
client.messages.create(
 {
    to: req.body.num,
    from: "TWILIO NUMBER",
    body: req.body.message
  }, function (err, message) {
    console.log(message.sid);
    cb(undefined, {b: "SMS sucessfully sent"});
  });
    
   // cb(undefined, req.body)
}; 

Last updated