5/31/15

Twilio SMS API Integration & Sending OTP in Spring MVC

What is Twilio:-
Twilio provide services like Voice, Video, WebRTC and SMS and many more. Twilio offers these cloud based services on pay per use basis.
For API docs you can visit url : https://www.twilio.com/api








Below i am describing how to integrate Twilio API to send OTP SMS to user mobile numbers

In Spring MVC Project
If you are going to use Twilio SMS service in Spring MVC project then first you need to include Twilio library dependency into your pom.xml (In case of Maven build).

<!-- Dependancy for Twilio -->
  <dependency>
   <groupId>com.twilio.sdk</groupId>
   <artifactId>twilio-java-sdk</artifactId>
   <version>3.7.1</version>
   <scope>compile</scope>
  </dependency>

You may also need some extra dependencies like httpcore and httpclient. So include below given dependencies also

<!-- HttpComponents -->
  <dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>4.2.6</version>
  </dependency>
  <dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpcore</artifactId>
   <version>4.2.5</version>
  </dependency>


In normal Core Java Project
You can download pre-built jars using this link :-


Now modify and use the code given below as per your Twilio settings



package com.your.package.name;



import java.security.SecureRandom;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;

/**
 * @author kunal
 *
 */

public class SMSClass {
  
 public static final String ACCOUNT_SID = "your account sid";
 public static final String AUTH_TOKEN = "your account auth token";
 
 // For Consumer Login 
 public String login(@RequestParam(value = "mobileNo",required = true)String mobileNo) 
 {
  TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
   
  // generate OTP
  SecureRandom random = new SecureRandom();
     Integer i = random.nextInt((999999 - 100000) + 1);
     String otp = i.toString();
     consumer.setOtp(otp);
               
     // Send SMS via Twilio
  List<NameValuePair> params = new ArrayList<NameValuePair>();
  params.add(new BasicNameValuePair("Body", "This is my service sending OTP to you. OTP is : "+otp));
  params.add(new BasicNameValuePair("To", "+91"+mobileNo));
  
  // From no needs to be pre verified with Twilio
  params.add(new BasicNameValuePair("From", "+12025688524"));

  MessageFactory messageFactory = client.getAccount().getMessageFactory();
  com.twilio.sdk.resource.instance.Message message;
  try 
  {
   message = messageFactory.create(params);   
   return message.getStatus();
  } 
  catch (TwilioRestException e) 
  {
   return e.getMessage();
  }
  
 }
 
}


My Experience With Twilio SMS Service

Pros:- 
- This is international service so it works in many countries. 
- A lot of code help and examples are available on Twilio website in Java, PHP, Node and many more languages.
- Easy to integrate and use.
- Standard and fixed charges for users as per their country.
- You can check log of every message you have sent.
- Work on pay per use basis. So no upfront payment or commitment.
- Service is free for demo implementation.

Cons:-
- In India i have used SMS service and found that it always sends SMS with different sender name. So in case of android where you want to automatically read OTP from SMS, you can not do this as sender name keeps changing.
- You need to change your implementation if you want to send SMS in bulk and fire them in seconds.
- Lot of SMS fired get bounce and blocked as spam in India by Telecoms.
- Sending SMS second time on same number will fail in most of the cases.


Put your queries in comments.