SMS from Java web application - java

I have SMS server, and I want to send and receive SMS from My java web application.
How I do this?
Thanks,

Normally, you can use SMS servers via HTTP api, e.g. send a request to
http://your-server-name/sendSms?nr=55534563&msg=hello+world
You should look for exact information in your server's documentation.
For general examples on how to send HTTP requests, see for instance this answer (GET) and this answer (POST).

Depending on your sms gateway API specifications, you'll have to:
Call a http url to send a SMS
Get your Java app called through http to receive a SMS
Take a look at this example of SMS api specifications, it also includes several examples of codes in different programming languages.

There is Ogham library. The code to send SMS is easy to write (it automatically handles character encoding and message splitting). The real SMS is sent either using SMPP protocol (standard SMS protocol) or through a provider API.
You can even test your code locally with a SMPP server to check the result of your SMS before paying for real SMS sending.
As it uses SMPP standard protocol, many providers can be used.
package fr.sii.ogham.sample.standard.sms;
import java.util.Properties;
import fr.sii.ogham.core.builder.MessagingBuilder;
import fr.sii.ogham.core.exception.MessagingException;
import fr.sii.ogham.core.service.MessagingService;
import fr.sii.ogham.sms.message.Sms;
public class BasicSample {
public static void main(String[] args) throws MessagingException {
// [PREPARATION] Just do it once at startup of your application
// configure properties (could be stored in a properties file or defined
// in System properties)
Properties properties = new Properties();
properties.setProperty("ogham.sms.smpp.host", "<server host given by the provider>"); // <1>
properties.setProperty("ogham.sms.smpp.port", "<server port given by the provider>"); // <2>
properties.setProperty("ogham.sms.smpp.system-id", "<system ID given by the provider>"); // <3>
properties.setProperty("ogham.sms.smpp.password", "<password given by the provider>"); // <4>
properties.setProperty("ogham.sms.from.default-value", "<phone number to display for the sender>"); // <5>
// Instantiate the messaging service using default behavior and
// provided properties
MessagingService service = MessagingBuilder.standard() // <6>
.environment()
.properties(properties) // <7>
.and()
.build(); // <8>
// [/PREPARATION]
// [SEND A SMS]
// send the sms using fluent API
service.send(new Sms() // <9>
.message().string("sms content")
.to("+33752962193"));
// [/SEND A SMS]
}
}
There are many other features and samples / spring samples.

Related

Unclear how to connect Plivo to OpenTok Java SDK SIP

I'm trying to use Plivo with the OpenTok Java SDK to dial out. There is an example for javascript where Plivo is used.
I'm able to invoke the method Openok.dial() and get a positive response that I then send to my react client.
There are no errors but I'm not calling the targeted number.
I'm not understanding how to use the uris or if they are still necessary.
So is it still necessary to create the uri's as stated in the JS example (https://github.com/opentok/opentok-sip-samples/tree/master/Plivo-SIP-Dial-Out)? And how do i then use those uri's?
Or is there an example i can peek to get a rough idea?
TokBox Developer Evangelist here.
The OpenTok SIP Interconnect feature allows you dial out to a SIP address (uri). With the Plivo sample, you would have to create an application on their website and configure the Plivo application with the appropriate webhooks so when that when you dial out to the Plivo SIP uri from OpenTok, you will get events on the webhook which will allow you to connect the OpenTok session with the PSTN user.
You can also leverage Nexmo or other SIP providers to dial out and connect an OpenTok session with a PSTN user. For example, if you use Nexmo, you can dial directly to a phone number by constructing the SIP properties in the OpenTok Java SDK like so:
String nexmoApiKey = "";
String nexmoApiSecret = "";
String sessionId = "";
String token = "";
SipProperties properties = new SipProperties.Builder()
.sipUri("sip:15555555555#sip.nexmo.com")
.from("from#example.com")
.headersJsonStartingWithXDash(headerJson)
.userName(nexmoApiKey)
.password(nexmoApiSecret)
.secure(false)
.build();
Sip sip = opentok.dial(sessionId, token, properties);
Please note that you would have to configure the phoneNumber, sessionId, token, and credentials - I've just added a sample number along with empty strings as the credentials.

How to send Notification Message with gcm-server.jar

I am using the gcm-server.jar to send gcm Messages from Server because its easy to use. (http://www.java2s.com/Code/Jar/g/Downloadgcmserverjar.htm).
Messages are sent with this code. This works fine:
Message msg = new Message.Builder().addData("message", message).build();
Sender sender = new Sender();
Result result = sender.send(msg, token, 5);
...
How can I send a GCM Message with Notification Payload like in this JSON:
{"to":"token" ,
"notification":{
"sound":"default",
"badge":"1",
"title":"this is the title",
"body":"this is the body"}}
You need to have a server set up where you parse these messages in JSON format so that GCM can process it accordingly.
Usually this depends on what server technology you are using. Also, you might want to check the validity of the library you referenced as GCM framework has been updated substantially.
Here's a good place to start.
And another good tutorial here. (although this one is older too but gives you understanding of server side implementation)
Hope this helps!

Web Socket - Spring : Confirm of message received

I am sending a message through WebSocket with Spring from Tomcat Server to a SockJSClient with the method:
WebSocketSession.sendMessage(WebSocketMessage<?> message)
and I would like to know when the message has been received (eventually with complementary information, for example whether the logic on client successfully processed), then go for next message.
This is an Activity diagram that explains the use case.
How can I receive confirmation of reception or result from client?
As Erwin pointed, you can adopt some higher protocol providing such feature like STOMP. However, if you are afraid to adopt it only for that feature, you can implement that feature by yourself.
The first thing is to give each message id to identify each message, type to recognize the purpose of each message, data to transport a message's content and reply which is a flag to see whether or not ACK is required and to use a format like JSON to serialize/deserialize an object containing these data into/from WebSocket message.
When sending a message, it creates an object by issuing a new id for that message, setting type to message and data to given message and reply to true if ACK is required or false if not. And it serializes it into JSON and sends it as a WebSocket message. - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L88-L110
When receiving a message, it deserializes JSON to the above object. If reply is true, it sends a special message whose type is reply setting data to id of that message. Then, the counterpart can confirm that its counterpart has received a message whose id is id. - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L46-L76
The above links point similar implementation in Cettia which is a real-time web application framework I wrote. Though that implementation is a little bit complex as it is designed to allow for user to handle callbacks with result, you are likely to get the basic idea.
API implemented by that link looks like the following.
A server or client which requires a result of event processing.
// Using Java server with lambda
socket.send("foo", "bar", result -> /* resolved */, reason -> /* rejected */);
The corresponding client or server which has a responsibility to submit the result.
// Using JavaScript client with arrow functions
socket.on("foo", (data, reply) => {
// data is 'bar'
// 'reply.resolve(result)' if it successes
// 'reply.reject(reason)' if it fails
});

how to create an intranet mailing system using java

i want to create an intranet mailing system using java.so suggest me which API and what classes to use.
Without doubts use Apache Commons Email - it's an industry standard.
Commons Email aims to provide a API for sending email. It is built on top of the Java Mail API, which it aims to simplify.
Some of the mail classes that are provided are as follows:
SimpleEmail - This class is used to send basic text based emails.
MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
HtmlEmail - This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.
EmailAttachment - This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.
Use the JavaMail API
Take a look at http://java.sun.com/products/javamail/
Try this library: http://github.com/masukomi/aspirin
It can actually send email (some kind of embedded MTA):
public class Main {
public static void main(String[] args) throws MessagingException {
MailQue que = new MailQue();
MimeMessage mes = SimpleMimeMessageGenerator.getNewMimeMessage();
mes.setText("test body");
mes.setSubject("test subject");
mes.setFrom(new InternetAddress("my#local.com"));
mes.setRecipients(Message.RecipientType.TO, "foo#bar.com");
que.queMail(mes);
}
}

implementing GAE XMPP service as an external component to an existing XMPP server (e.g. ejabberd or OpenFire)

may i know what integration technique that you folks use to implement external component to an existing XMPP server (e.g. ejabberd or OpenFire) . Is it through sending xmpp message to another user#externaldomain directly or using mechanism like urlfetch?
Google app engine (Gae) does support XMPP just as CLIENT.
With XMPP Gae JAVA client feature you can:
SEND MESSAGE
JID jid = new JID("youraccount#jabber.org");
Message msg = new MessageBuilder()
.withRecipientJids(jid)
.withBody("Hello i'm a fancy GAE app, how are you?")
.build();
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
if (xmpp.getPresence(jid).isAvailable()) {
SendResponse status = xmpp.sendMessage(msg);
}
RECEIVE MESSAGE
public class XMPPReceiverServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException {
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message message = xmpp.parseMessage(req);
JID fromJid = message.getFromJid();
String body = message.getBody();
//Save to Big Table
}
}
Remember that JIDs can just be yourappid#appspot.com OR foo#yourappid.appspotchat.com
because Google domains are not supported yet.
For example, you could craft a toy Gae application with a simple page with:
An html form to send text
An html table that display the list of messages received and stored to big table.
To test your application:
Create an account on jabber.org
Download Smack
Try to send a message from Smack to yourappid#appspot.com
Try to send a message from Gae App to youraccount#jabber.org
In case you have your personal XMPP server (openfire) up and running, simply skip step 1 and use your domain account to receive message from your fancy Gae App.
Have a look to XMPP message delivery to understand how this works.
App Engine supports a very limited subset of XMPP. Basically, you can send messages (through the API), and you can receive messages (they come in as HTTP requests).
Java API
Python API
You could rig up an external component on your existing XMPP server, to send and receive messages with your app engine code. That component would have to keep track of whatever it is you want to send and receive from your app.

Categories

Resources