Liferay SMS Portlet

Generally in our real time developments we may need to send SMS from our applications same way we can get requirement to send SMS from Portlet in Liferay development.

When we send SMS we need following things
  • We need to purchase BULK SMS from SMS vendors
  • We need BULK SMS Vendor SMS API to use in our application.

BULK SMS vendor

There are many bulk SMS vendors in the market we can register with them and we can purchase BULK SMS.

Each vendor is responsible to provide username and passwordso that we will use these credentials to send SMS in our Portlet development

The following are Bulk SMS Vendors


BULK SMS Vendor SMS API

Bulk SMS vendors are responsible to provide SMS sender API so that we will use SMS sender API to send SMS from our application.

SMS sender API Types

  • WEB Service API (SOAP/REST)
  • Language Specific API
  • HTTP API

WEB Service API (SOAP/REST)

SMS vendors will provide SOAP/REST API to send SMS from our applications and it will provide set of classes or some request URL so that we can use those to send SMS.

Language Specific API

Based on language they will provide Language specific implementation  so that we can use those packages in our application.

Here we are going to use JAVA specific implementation because Liferay portal developed by java.
Generally they will provide some jar file and it consist some set of interfaces and classes so that we can use their API classes to send SMS from our Portlet.

HTTP API

This is one of most used API to send SMS in applications. SMS vendors will provide request URL and its consist request parameters and its values as query string .Once we prepared request URL we can  ping that URL then SMS will be send it to destination.

We just prepare URL and hit in browser address bar then we can send SMS. In application we will use Apache Http Client or java.net package to ping URL in java code.

Example URL to Send SMS


URL Consist following things

Vendor Host Name: Main Host name of Vendor (smsc.vianett.no/v3/send.ashx )

Common Request Parameters

Source:  It’s like Alpha numeric String visible to receiver when they received SMS

Destination mobile Number: Actual receiver mobile number

Message: Text Message

User Name: SMS Vendor register user name

Password: SMS vendor register user password

Note:

Request parameter names will be changed based on the SMS vendor

SMS Portlet Implementation

To test SMS Portlet we need one SMS vendor and “vianett” is vendor they provide 5 sample SMS for testing purpose

We should register with vianettso that we can get 5 free SMS so we can use this account in code implementation to send SMS


Once we register we get password to our mail and we can change later. Vianett register username and password will be used to send SMS

In the example we use Apache HTTP API/Java NET Package to send SMS we have different request URL parameter we need pass appropriate values to each parameter so that we can send SMS. We can have URLs like send SMS URL and to know delivery status we have other Delivery Status Request URL such a way they provided different URL for different purpose.



Please have look into API document to know more information.


Download Portlet Source Code.


Note:

Once you downloaded Portlet you need to modify code in the Portlet action class with respect to SMS vendor HTTP URL and Its request parameters then you can test.

Portlet  Screen

VaiNett Login Page


VaiNett Customer Landing Page


Complete Code Implementation

Portlet View JSP page (/html/jsps/view.jsp)


<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%>
<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<style>
.aui .control-group {
margin-bottom: 0px;
}
</style>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<portlet:actionURL var="sendSMSActionURL" windowState="normal"
name="sendSMS">
</portlet:actionURL>
<% if(SessionMessages.contains(renderRequest.getPortletSession(),"SMS-send-success")){%>
<liferay-ui:success key="SMS-send-success" message="SMS has been sent successfully." />
<%} %>
<% if(SessionErrors.contains(renderRequest.getPortletSession(),"SMS-send-error")){%>
<liferay-ui:error key="SMS-send-error" message="There is problem in Request URL te send SMS." />
<%} %>
<aui:form action="<%=sendSMSActionURL%>" method="post" name="smsForm">
<aui:input name="mobileNumber" id="mobileNumber" label="Mobile Number">
<aui:validator name="required" />
<aui:validator name="digits"></aui:validator>
<aui:validator name="minLength">10</aui:validator>
<aui:validator name="maxLength">10</aui:validator>
</aui:input>
<aui:input name="textMessage" id="textMessage" label="SMS Text Message" type="textarea">
<aui:validator name="required" />
</aui:input>
<span style=" float: left;">Characters Left&nbsp;</span><p id="<portlet:namespace/>textCounter"></p>
<aui:button type="submit" value="Send SMS"></aui:button>
</aui:form>

<aui:script>
AUI().use('aui-char-counter', function(A) {
new A.CharCounter({
counter : '#<portlet:namespace/>textCounter',
input : '#<portlet:namespace/>textMessage',
maxLength : 140,
on : {
maxLength : function(event) {
alert('The max length limit was reached');
}
}
});
});
</aui:script>


Portlet Action Class (LiferaySMS.java)


package com.meera.liferay.sms;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
* Portlet implementation class LiferaySMS
*/
public class LiferaySMS extends MVCPortlet {
public static final String VENDOR_HOST_URL= "http://smsc.vianett.no/V3/CPA/MT/MT.ashx";
public static final String VENDOR_USERNAME_RQUEST_PARAM_VALUE= "meera.success@gmail.com";
public static final String 
VENDOR_PASSWORD_RQUEST_PARAM_VALUE= "your sms vendor password";
public static final String COUNTRY_CODE= "91";
public void sendSMS(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException, SystemException {
String textMessage = ParamUtil.getString(actionRequest,"textMessage");
String mobileNumber = ParamUtil.getString(actionRequest,"mobileNumber");
long messageId = CounterLocalServiceUtil.increment();
HttpURLConnection connection = null;
URL completeSenderURL = null;
String conncetionResponse = null;
StringBuilder smsSenderURLQueryString = new StringBuilder();
try {
//Prepare URL query String with required Parameters.
smsSenderURLQueryString.append("username=" + URLEncoder.encode(VENDOR_USERNAME_RQUEST_PARAM_VALUE, "UTF-8"));
smsSenderURLQueryString.append("&password=" + URLEncoder.encode(VENDOR_PASSWORD_RQUEST_PARAM_VALUE, "UTF-8"));
smsSenderURLQueryString.append("&msgid=" + URLEncoder.encode(String.valueOf(messageId), "UTF-8"));
smsSenderURLQueryString.append("&tel=" + URLEncoder.encode(COUNTRY_CODE+mobileNumber, "UTF-8"));
smsSenderURLQueryString.append("&msg=" + URLEncoder.encode(textMessage, "UTF-8"));

//Complete SMS Sender Request URL
String completeSenderURLString=
VENDOR_HOST_URL+"?"+smsSenderURLQueryString.toString();
_log.info("SMS Sender URL"+completeSenderURLString);

//Create JAVA NET URL from URL String
completeSenderURL = new URL(completeSenderURLString);
connection = (HttpURLConnection) completeSenderURL.openConnection();
connection.setDoOutput(false);
connection.setDoInput(true);
conncetionResponse = connection.getResponseMessage();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
connection.disconnect();
SessionMessages.add(actionRequest.getPortletSession(),"SMS-send-success");
_log.info("SMS has been sent successfully"+responseCode);
}else{
SessionErrors.add(actionRequest.getPortletSession(),"SMS-send-error");
_log.info("There is problem in Request URL to send SMS"+responseCode);
}
_log.info("URL Respond Data"+conncetionResponse);
}catch (UnsupportedEncodingException e) {
_log.error(e.getMessage());
}
catch(MalformedURLException mue){
_log.error(mue.getMessage());
}catch (Exception e) {
_log.error(e.getMessage());
}


}
private static Log _log = LogFactoryUtil.getLog(LiferaySMS.class);
}



Share on Google Plus

About Meera Prince

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

1 comments:

  1. nice blog. Bulk SMS Company - Best & Fast Bulk SMS Service Provider In India. For marketing purposes, Bulk SMS plays an Important Role
    Bulk sms

    ReplyDelete