tap tap! who’s there?

hello there! today, we shall do something new. let’s send someone a sms! wait. i already can do that with my phone. well, let’s see how some mobile operators, some service providers send you those automated messages when you send them a message!

how? we shall use hSenid TAP platform to create a simple sms application which will respond to a message.

cool stuff ahead! πŸ˜€

first up, what is TAP?

it stands for “telco application platform”. haha, yeah i didn’t get it at first either. the thing is, not everyone is a developer, and still, people need more an more applications for their products/businesses each day. more over to training to be an expert in development, tap provides you with a no-so-complicated platform to build your apps. this may help you even as a developer, non developer or as a company as a whole. many companies are adapting this system to crowd source the apps so they will have a comprehensive collection and of course, this helps the developer community as a whole. to read more on this, you can get the e-book and other extras at tap at www.hsenidmobile.com.

got the gist of it? cool! πŸ˜€ you can see more on this on hSenid youtube channel, so urm.. go.. subscribe! πŸ˜›

what do you need to do.

first up, get yourself registered at devspace.hsenidmobile.com. there you’ll be able to get your hands on the devkit, some sample applications, and the simulator. (what’s that? wait lemme get into that! πŸ™‚ )

to continue press 5.. i mean, go to this link, and download the simulator and the java sample app package. we’ll need them to build and deploy our application. devspace.hsenidmobile.com/download

there you may also download some sample applications created with tap on different development environments.

here, we’re going to create an application using java. i’ll be using intellij idea as my ide, and also i’ll be using maven as well. the server i’ll be using to deploy will be tomcat.

to start, go to your location where you’re going to create the project, and run the following maven command to create a maven web application.

mvn archetype:generate -DgroupId=com.apps.hellothere -DartifactId=hellothere -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

buildnow, we have our maven web app ready. okay then, now fire up intellij idea, go to File > Import Project. then select the pom file from the location you created the project.

import project

import project

follow the wizard and (selecting the jdk version.. etc) and create the project. then you will be prompted to enable import. so, make it happen!

enable auto import

now we have to set somethings up.

  1. create a directory named “java” under src > main
  2. right click on the java directory > mark directory as > sources root | this is where we’re going to keep our java files
  3. create a directory named “lib” inside src | this is where our 3rd party libraries will be stored
  4. copy “sdp.app.api-1.0.11-SNAPSHOT.jar” from the downloaded java sample application. it’ll be in localpath/tap-java-applicaitons/sdp.hello.world.ant-1.0.11-SNAPSHOT/lib

okay. now, open up your pom file. under <dependencies> you might see the junit dependency. as it’s not required in this demo, you can go ahead and remove it. and then, you’ll have to add 2 dependencies. one from the maven repository and, the other one we added to the lib directory.

<dependency>
      <groupId>com.hms</groupId>
      <artifactId>sdp.api</artifactId>
      <version>1.0.11</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/src/lib/sdp.app.api-1.0.11-SNAPSHOT.jar</systemPath>
</dependency>

<dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>1.7.2</version>
</dependency>

first dependency added will be from the lib, you can see it from the system path included. the 2nd dependency is from the maven repository. you can get the latest at gson on maven. i go with the 1.7.2 version but you can try with a later version.

depe

dependencies

we’ll have to do one more update to our pom file now. the local library file, sdp.app.api-1.0.11-SNAPSHOT.jar is still hasn’t been added to the program as a resource. if we don’t do this, when compile and package the program, it won’t carry this file, which means it won’t run eventually. so, we’ll have to add it as a build time resource.

see that little bit of code with <build> </build> surrounding it?Β  yeah, we have to update that.

<resources>
      <resource>
            <directory>${project.basedir}/src/lib</directory>
            <includes>
                  <include>**/*</include>
            </includes>
            <targetPath>../hellothere/WEB-INF/lib</targetPath>
      </resource>
</resources>

now what this snippet does is, add all the files in the lib directory to the specified target path. so, whatever you want to package with the final outcome, place it in the lib and it’ll be done.

pom file

pom file

alright. our pom is set to go. yay! πŸ˜€

we have to configure our servlet but, let’s do it in due time. πŸ˜›

right. now, let’s get into coding. so, you have created the java directory already right? cool. so there, add a new class. i’m going to create mine as “FeedbackListener“. this class will be receiving a sms and will be sending a sms as a reply.

cool? so create the class. and then, implement MoSmsListener interface. that interface is included in the jar file we added into the lib directory (import hms.kite.samples.api.sms.MoSmsListener). once you implement this, the ide will prompt you to implement the methods from the interface. so.. go ahead and do that as well.

FeedbackListener class implemented with MoSmsListener

FeedbackListener class implemented with MoSmsListener

when an user sends a sms to the system, the method onRecievedSms() will get triggered with a MoSmsReq object. this object carries all the details from the user, like the senders message, the telephone number etc. you can read more about this from the devspace.hsenidmobile.com/guide/sms-api.

so let’s capture what the user sent and his phone number with 2 strings.

String message = moSmsReq.getMessage();
String number = moSmsReq.getSourceAddress();

so where do we store all these messages coming from the users? well, let’s create a class called “FeedbackService” which will do the data storage. for now, let’s just use a hashmap. but later, if you want, you can add database support to record the messages. for now, let’s go with a hashmap. there will two methods, addMessage() and getMessages() to handle the records.

public class FeedbackService {

      private static final Map<String, String> userMessages = new HashMap<String, String>();

      public static void addMessage(String number, String message) {
            userMessages.put(number, message);
      }

      public static Map<String, String> getMessages() {
            return userMessages;
      }

}

alright? clear up to now? great! now let’s get back to our FeedbackListener class. remember we added 2 variables to record the message and the number. now let’s utilize that new class’s (FeedbackService) addMessage() method.

the two method calls will be like following.

FeedbackService.addMessage(number, message);
sendResponse(moSmsReq);

you can call the addMessage() method and pass the recorded message and number directly since we created the method as static, so no object creation is needed. but sendResponse() will be local, so once you add that bit of code, the ide will prompt you to create the method accordingly. so yeah.. go on.. do it! πŸ˜›

afteraddingmethods

adding methods

okay? now let’s create our response message. so inside sendResponse() method, do the following changes.

private void sendResponse(MoSmsReq moSmsReq) throws MalformedURLException, SdpException {

      SmsRequestSender smsRequestSender = new SmsRequestSender(new URL("http://localhost:7000/sms/send"));

      MtSmsReq mtSmsReq = new MtSmsReq();
      mtSmsReq.setMessage("Hello " + moSmsReq.getSourceAddress() + ". Thank you for your reply.");
      mtSmsReq.setApplicationId(moSmsReq.getApplicationId());
      mtSmsReq.setPassword("password");

      mtSmsReq.setDestinationAddresses(Arrays.asList(moSmsReq.getSourceAddress()));

      smsRequestSender.sendSmsRequest(mtSmsReq);

}

so what does these methods do? well..

setMessage()Β Β  – as the method name suggests, this where we set the response message. and in our program, we’re saying hello to the user, and we’re mentioning his phone number as a referral as well.

setApplicationID() – this sets an application id which will be given to it when it’s launched.

setPassword() – sets a password to access the application, we’re just keeping it simple here.

setDestinationAddresses() – here, we have to pass an array of addresses to send the response to, even if it is just one number we have to add it as an array. and here, that line of code will extract the source addresses from the moSmsReq object and store it in the array and pass it as the destination address.

the smsRequestSender will store the sender url. this will be used to send our created mtSmsReq object which carries the data of our response.

and finally smsRequestSender.sendSmsRequest(mtSmsReq); will send the message.

**the exceptions MalformedURLException, SdpException will be thrown. make sure you handle them accordingly!

FeedbackListener class

FeedbackListener class

to read more on this, you can go onΒ devspace.hsenidmobile.com/guide/sms-api.php and read more. so make sure you do that! πŸ˜€

alrighty then! our code is ready! only thing we now have to do on the program end is to configure our servlet.

okay, to do that let’s configure the web.xml now. this is inside src > main > webapp

i said we’re going to deploy this on tomcat before right? meow yeah! so open up the web.xml and do the following changes.

<servlet>
      <servlet-name>moReceiver</servlet-name>
      <servlet-class>hms.kite.samples.api.sms.MoSmsReceiver</servlet-class>
      <init-param>
            <param-name>smsReceiver</param-name>
            <param-value>FeedbackListener</param-value>
      </init-param>
</servlet>

<servlet-mapping>
      <servlet-name>moReceiver</servlet-name>
      <url-pattern>/mo-receiver</url-pattern>
</servlet-mapping>

this should come after the already existing <display-name> tag. what this does is enabling our class FeedbackListener and the api’s MoSmsListener to receive the sms through the servlet. and the mapping creates the url for the servlet.

web.xml file

web.xml file

wel.. urm… our program is ready to go! Yipee! πŸ˜›

now to execute this, we’ll have to create the .war file using maven and set up our simulator we downloaded before.

so first, let’s create the war file. so head on to your project directory using your console, and run the following. (mind you, the ide create a directory in the same name of the project we’ve started so, you’ll have to move another directory in ex: /home/nimila/Documents/HelloThere/hellothere)

mvn clean install
build success

build success

now if you check the project directory, there will be new directory called target. inside there, you’ll find hellothere.war file. of course the name will change according to your project name.

now copy that war file to the webapps directory inside your tomcat installation. (ex: /home/nimila/installs/tomcat/webapps). so once you start the tomcat server, it’ll deploy our application in the server.

now let’s set up our simulator. first, extract the content from the archive you downloaded. go inside the bin directory of that extracted content. (ex: /Documents/SelfLearning/DevSpace-Simulator/bin) and run the following command.

./sdp-simulator console

if this fails, check the read+write privileges of the directory that it was extracted to. once you give the correct permission, you’ll be able toΒ  run that command. to read more about the simulator, go to devspace.hsenidmobile.com/guide/sms-sdk

simulator

simulator

now, if you go to http://localhost:10001/ in your browser, you’ll see the simulator is up and running.

simulator on the browser

simulator on the browser

we have a pretty cool old school n95 to play around with. πŸ˜›

now before we resume, go ahead and start the tomcat server. it’ll deploy our war file as well. πŸ˜€

you can read about tomcat server, how to get it running etc on the previous post, what does the cat say?.

alright? everything ready? now don’t don’t close any of the terminal instances with tomcat, or the simulator okay! yeesh!

okay, now head on over to your simulator on the browser, and edit the url.

http://localhost:8080/hellothere/mo-receiver

this is because, tomcat by default uses the port 8080 and our application is at the hellothere directory. you can check this at the webapps folder, our war file is now deployed and is a directory now! boom!Β πŸ’£ oh and also, notice the /mo-reciever? yeah, that’s the servlet url we fixed in the web.xml file. all coming together now isn’t it? πŸ˜€

okay now, add any text you want as the message and hit send! if you see “SUCCESS” in the messages sent to application section, you message was received by the application. wait a few seconds… and here it is! yay, we have our response and messages sent to customer section and is displayed on our vintage n95! awesomeness! πŸ˜€

simulator message sent

messages sent and recieved

messages sent and recieved

yay! woohoo! πŸŽ‰πŸŽ‰πŸŽ‰ we sent our message and we got a reply. you can check my project at github. TAP-HelloThere-SMSApp on GitHub

now you can play around with the application, add database support etc, and well, let me know how it goes!

so, get your coffee on and turn that into some awesome code! πŸ˜€ β˜• until next time! bbye!

Advertisements

spill a bit of coffee?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s