Code Monkey home page Code Monkey logo

messages-java-sdk's Introduction

MessageMedia Messages Java SDK

Pull Requests Welcome maven

The MessageMedia Messages API provides a number of endpoints for building powerful two-way messaging applications.

Isometric

Table of Contents

๐Ÿ” Authentication

Authentication is done via API keys. Sign up at https://developers.messagemedia.com/register/ to get your API keys.

Requests are authenticated using HTTP Basic Auth or HMAC. Provide your API key as the auth_user_name and API secret as the auth_password.

โ‰๏ธ Errors

Our API returns standard HTTP success or error status codes. For errors, we will also include extra information about what went wrong encoded in the response as JSON. The most common status codes are listed below.

HTTP Status Codes

Code Title Description
400 Invalid Request The request was invalid
401 Unauthorized Your API credentials are invalid
403 Disabled feature Feature not enabled
404 Not Found The resource does not exist
50X Internal Server Error An error occurred with our API

๐Ÿ“ฐ Information

Slack and Mailing List

If you have any questions, comments, or concerns, please join our Slack channel: https://developers.messagemedia.com/collaborate/slack/

Alternatively you can email us at: [email protected]

Bug reports

If you discover a problem with the SDK, we would like to know about it. You can raise an issue or send an email to: [email protected]

Contributing

We welcome your thoughts on how we could best provide you with SDKs that would simplify how you consume our services in your application. You can fork and create pull requests for any features you would like to see or raise an issue

โญ Installation

At present the jars are available from a public maven repository.

Use the following dependency in your project to grab via Maven:

<dependency>
  <groupId>com.messagemedia.sdk</groupId>
  <artifactId>messages</artifactId>
  <version>2.0.0</version>
</dependency>

๐ŸŽฌ Get Started

It's easy to get started. Simply enter the API Key and secret you obtained from the MessageMedia Developers Portal into the code snippet below and a mobile number you wish to send to.

Send an SMS

Destination numbers (destination_number) should be in the E.164 format. For example, +61491570156.

import java.util.ArrayList;
import java.util.List;

import com.messagemedia.messages.MessageMediaMessagesClient;
import com.messagemedia.messages.controllers.MessagesController;
import com.messagemedia.messages.http.client.APICallBack;
import com.messagemedia.messages.http.client.HttpContext;
import com.messagemedia.messages.models.Message;
import com.messagemedia.messages.models.SendMessagesRequest;
import com.messagemedia.messages.models.SendMessagesResponse;

public class App
{
    public static void main( String[] args )
    {

     // Configuration parameters and credentials
        String authUserName = "API_KEY"; // The username to use with basic/HMAC authentication
        String authPassword = "API_SECRET"; // The password to use with basic/HMAC authentication
        boolean useHmacAuth = false; // Change to true if you are using HMAC keys

        MessageMediaMessagesClient client = new MessageMediaMessagesClient(authUserName, authPassword, useHmacAuth);
        MessagesController messages = client.getMessages();

        Message message = new Message();
        message.setContent("My message");
        message.setDestinationNumber("+61466412529");

        List<Message> messagesList = new ArrayList<Message>();
        messagesList.add(message);

        SendMessagesRequest body = new SendMessagesRequest();
        body.setMessages(messagesList);

        messages.sendMessagesAsync(body, new APICallBack<SendMessagesResponse>() {
		        public void onSuccess(HttpContext context, SendMessagesResponse response) {
		            // TODO success callback handler
		        	System.out.println("success");
		        }
		        public void onFailure(HttpContext context, Throwable error) {
		            // TODO failure callback handler
		        	System.out.println("failure");
		        }
			});
    }
}

Send an MMS

  • Destination numbers (destination_number) should be in the E.164 format. For example, +61491570156.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.messagemedia.messages.MessageMediaMessagesClient;
import com.messagemedia.messages.controllers.MessagesController;
import com.messagemedia.messages.http.client.APICallBack;
import com.messagemedia.messages.http.client.HttpContext;
import com.messagemedia.messages.models.FormatEnum;
import com.messagemedia.messages.models.Message;
import com.messagemedia.messages.models.SendMessagesRequest;
import com.messagemedia.messages.models.SendMessagesResponse;

public class App
{
    public static void main( String[] args )
    {

     // Configuration parameters and credentials
        String authUserName = "API_KEY"; // The username to use with basic/HMAC authentication
        String authPassword = "API_SECRET"; // The password to use with basic/HMAC authentication
        boolean useHmacAuth = false; // Change to true if you are using HMAC keys

        MessageMediaMessagesClient client = new MessageMediaMessagesClient(authUserName, authPassword, useHmacAuth);
        MessagesController messages = client.getMessages();

        Message message = new Message();
        message.setContent("My message");
        message.setDestinationNumber("+61466412529");
        message.setFormat(FormatEnum.MMS);
        message.setSubject("This is an MMS");
        message.setMedia(new ArrayList<String>(Arrays.asList("https://upload.wikimedia.org/wikipedia/commons/6/6a/L80385-flash-superhero-logo-1544.png")));


        List<Message> messagesList = new ArrayList<Message>();
        messagesList.add(message);

        SendMessagesRequest body = new SendMessagesRequest();
        body.setMessages(messagesList);

        messages.sendMessagesAsync(body, new APICallBack<SendMessagesResponse>() {
		        public void onSuccess(HttpContext context, SendMessagesResponse response) {
		            // TODO success callback handler
		        	System.out.println("success");
		        }
		        public void onFailure(HttpContext context, Throwable error) {
		            // TODO failure callback handler
		        	System.out.println("failure");
		        }
			});
    }
}

Get Status of a Message

You can get a messsage ID from a sent message by looking at the message_id from the response of the above example.

import com.messagemedia.messages.MessageMediaMessagesClient;
import com.messagemedia.messages.controllers.MessagesController;
import com.messagemedia.messages.http.client.APICallBack;
import com.messagemedia.messages.http.client.HttpContext;
import com.messagemedia.messages.models.GetMessageStatusResponse;

public class App {

    public static void main(String[] args) throws Throwable {
        // Configuration parameters and credentials
        String authUserName = "API_KEY"; // The username to use with basic/HMAC authentication
        String authPassword = "API_SECRET"; // The password to use with basic/HMAC authentication
        boolean useHmacAuth = false; // Change to true if you are using HMAC keys

        MessageMediaMessagesClient client = new MessageMediaMessagesClient(authUserName, authPassword, useHmacAuth);
        MessagesController messages = client.getMessages();

        String messageId = "YOUR_MESSAGE_ID";
        messages.getMessageStatusAsync(messageId, new APICallBack<GetMessageStatusResponse>() {
	        public void onSuccess(HttpContext context, GetMessageStatusResponse response) {
	            // TODO success callback handler
	        	System.out.println("success");
	        }
	        public void onFailure(HttpContext context, Throwable error) {
	            // TODO failure callback handler
	        	System.out.println("failure");
	        }
		});
    }

}

Get replies to a message

You can check for replies that are sent to your messages

import com.messagemedia.messages.MessageMediaMessagesClient;
import com.messagemedia.messages.controllers.MessagesController;
import com.messagemedia.messages.controllers.RepliesController;
import com.messagemedia.messages.http.client.APICallBack;
import com.messagemedia.messages.http.client.HttpContext;
import com.messagemedia.messages.models.CheckRepliesResponse;
import com.messagemedia.messages.models.GetMessageStatusResponse;

public class App {

    public static void main(String[] args) throws Throwable {
        // Configuration parameters and credentials
        String authUserName = "API_KEY"; // The username to use with basic/HMAC authentication
        String authPassword = "API_SECRET"; // The password to use with basic/HMAC authentication
        boolean useHmacAuth = false; // Change to true if you are using HMAC keys

        MessageMediaMessagesClient client = new MessageMediaMessagesClient(authUserName, authPassword, useHmacAuth);
        RepliesController replies = client.getReplies();

        replies.checkRepliesAsync(new APICallBack<CheckRepliesResponse>() {
	        public void onSuccess(HttpContext context, CheckRepliesResponse response) {
	            // TODO success callback handler
	        	System.out.println("success");
	        }
	        public void onFailure(HttpContext context, Throwable error) {
	            // TODO failure callback handler
	        	System.out.println("failure");
	        }
		});  

    }

}

Check Delivery Reports

This endpoint allows you to check for delivery reports to inbound and outbound messages.

import com.messagemedia.messages.MessageMediaMessagesClient;
import com.messagemedia.messages.controllers.DeliveryReportsController;
import com.messagemedia.messages.http.client.APICallBack;
import com.messagemedia.messages.http.client.HttpContext;
import com.messagemedia.messages.models.CheckDeliveryReportsResponse;


public class App
{
    public static void main( String[] args )
    {

     // Configuration parameters and credentials
        String authUserName = "API_KEY"; // The username to use with basic/HMAC authentication
        String authPassword = "API_SECRET"; // The password to use with basic/HMAC authentication
        boolean useHmacAuth = false; // Change to true if you are using HMAC keys

        MessageMediaMessagesClient client = new MessageMediaMessagesClient(authUserName, authPassword, useHmacAuth);
        DeliveryReportsController deliveryReports = client.getDeliveryReports();

        deliveryReports.checkDeliveryReportsAsync(new APICallBack<CheckDeliveryReportsResponse>() {
		        public void onSuccess(HttpContext context, CheckDeliveryReportsResponse response) {
		            // TODO success callback handler
		        	System.out.println("success");
		        }
		        public void onFailure(HttpContext context, Throwable error) {
		            // TODO failure callback handler
		        	System.out.println("failure");
		        }
			});
    }
}

Confirm Delivery Reports

This endpoint allows you to mark delivery reports as confirmed so they're no longer returned by the Check Delivery Reports function.

import java.util.ArrayList;
import java.util.List;

import com.messagemedia.messages.MessageMediaMessagesClient;
import com.messagemedia.messages.controllers.DeliveryReportsController;
import com.messagemedia.messages.http.client.APICallBack;
import com.messagemedia.messages.http.client.HttpContext;
import com.messagemedia.messages.models.CheckDeliveryReportsResponse;
import com.messagemedia.messages.models.ConfirmDeliveryReportsAsReceivedRequest;
import com.messagemedia.messages.models.DynamicResponse;


public class App
{
    public static void main( String[] args )
    {

     // Configuration parameters and credentials
        String authUserName = "API_KEY"; // The username to use with basic/HMAC authentication
        String authPassword = "API_SECRET"; // The password to use with basic/HMAC authentication
        boolean useHmacAuth = false; // Change to true if you are using HMAC keys

        MessageMediaMessagesClient client = new MessageMediaMessagesClient(authUserName, authPassword, useHmacAuth);
        DeliveryReportsController deliveryReports = client.getDeliveryReports();

        ConfirmDeliveryReportsAsReceivedRequest body = new ConfirmDeliveryReportsAsReceivedRequest();
        List<String> reportIds = new ArrayList<String>();
        reportIds.add("delivery-report-id");

        deliveryReports.confirmDeliveryReportsAsReceivedAsync(body, new APICallBack<DynamicResponse>() {
		        public void onSuccess(HttpContext context, DynamicResponse response) {
		            // TODO success callback handler
		        	System.out.println("success");
		        }
		        public void onFailure(HttpContext context, Throwable error) {
		            // TODO failure callback handler
		        	System.out.println("failure");
		        }
			});
    }
}

Check credits remaining (Prepaid accounts only)

This endpoint allows you to check for credits remaining on your prepaid account.

import com.messagemedia.messages.MessageMediaMessagesClient;
import com.messagemedia.messages.controllers.MessagesController;
import com.messagemedia.messages.http.client.APICallBack;
import com.messagemedia.messages.http.client.HttpContext;
import com.messagemedia.messages.models.DynamicResponse;


public class App
{
    public static void main( String[] args )
    {

     // Configuration parameters and credentials
        String authUserName = "API_KEY"; // The username to use with basic/HMAC authentication
        String authPassword = "API_SECRET"; // The password to use with basic/HMAC authentication
        boolean useHmacAuth = false; // Change to true if you are using HMAC keys

        MessageMediaMessagesClient client = new MessageMediaMessagesClient(authUserName, authPassword, useHmacAuth);
        MessagesController messages = client.getMessages();
        
        messages.checkCreditsRemainingAsync(new APICallBack<DynamicResponse>() {
		        public void onSuccess(HttpContext context, DynamicResponse response) {
		            // TODO success callback handler
		        	System.out.println(response.toString());
		        }
		        public void onFailure(HttpContext context, Throwable error) {
		            // TODO failure callback handler
		        	System.out.println("failure");
		        }
			});
    }
}

๐Ÿ“• API Reference Documentation

Check out the full API documentation for more detailed information.

๐Ÿ˜• Need help?

Please contact developer support at [email protected] or check out the developer portal at developers.messagemedia.com

๐Ÿ“ƒ License

Apache License. See the LICENSE file.

messages-java-sdk's People

Contributors

bmostafa87 avatar coderbec avatar gtremoyne avatar ibrahimtareq avatar jamesdale avatar lachlanodonnell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

messages-java-sdk's Issues

How to use internet proxy with messages sdk

We're running in a corporate network behind a proxy.

I'm trying to test the messagemedia API.

Using the first example for sending an SMS, I get the following:

failure
com.messagemedia.messages.exceptions.APIException: Failed to connect to api.messagemedia.com/54.153.174.199:443`

I tried adding these lines to the example (ip's/port redacted) :

System.setProperty("http.proxyHost", "xx.xx.xx.xxx");
System.setProperty("http.proxyPort", "xxxx");
System.setProperty("https.proxyHost", "xx.xx.xx.xxx");
System.setProperty("https.proxyPort", "xxxx");

, which worked for the following example to show an https page

package org.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class testNetworkConnection {
    public static void main(String args[]) {
        System.setProperty("http.proxyHost", "xx.xx.xx.xxx");
        System.setProperty("http.proxyPort", "xxxx");
        System.setProperty("https.proxyHost", "xx.xx.xx.xxx");
        System.setProperty("https.proxyPort", "xxxx");
        try {
            download("https://www.google.com");
        } catch (IOException e) {
            System.out.println(e);
        }
    }
    public static void download(String urlString) throws IOException {
        URL url = new URL(urlString);
        try(
                BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
        ) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        }
    }
}

, but it still gives the same error.

I'm not 100% sure our proxy isn't blocking the connection, but I think it isn't?

Any ideas on how to get it to connect?

getMessageStatus returns the following error

java.util.regex.PatternSyntaxException: Illegal repetition
{messageId}
at java.util.regex.Pattern.error(Pattern.java:1957)
at java.util.regex.Pattern.closure(Pattern.java:3159)
at java.util.regex.Pattern.sequence(Pattern.java:2136)
at java.util.regex.Pattern.expr(Pattern.java:1998)
at java.util.regex.Pattern.compile(Pattern.java:1698)
at java.util.regex.Pattern.(Pattern.java:1351)
at java.util.regex.Pattern.compile(Pattern.java:1028)
at java.lang.String.replaceAll(String.java:2223)
at com.messagemedia.messages.APIHelper.appendUrlWithTemplateParameters(APIHelper.java:232)
at com.messagemedia.messages.controllers.MessagesController.getMessageStatusAsync(MessagesController.java:367)
at com.messagemedia.messages.controllers.MessagesController.getMessageStatus(MessagesController.java:322)
at com.messagemedia.messages.controllers.MessagesController.getMessageStatus(MessagesController.java:283)

Java Client is not good to use as client library

String bodyValue = "{"messages":" +
"[{"content":"My first message", " +
""destination_number":"YOUR_MOBILE_NUMBER"" +
"}]}";

Who want to create an json manually? I thinks we need to create an object Message, and just:
Messages messages = Messages.builder()
.addMessage(
Message.builder().withContent("My First Message")
.withDestinationNumber("+xxxxxx").build())
.build()

MessageClient client = MessageClient(apikey, secretkey);

client.send(messages)
Do you think it more simple?

Missing dependency - com.mashape.unirest:unirest-java

Was trying to have a go with your API after having a chat with the lovely folk at your YOW! booth in Melbourne and immediately ran into this error using v1.2.0

Exception in thread "main" java.lang.NoClassDefFoundError: com/mashape/unirest/request/HttpRequest
	at com.messagemedia.messages.controllers.BaseController.getClientInstance(BaseController.java:81)

This com.mashape.unirest package is used in

  • src/main/java/com/messagemedia/messages/http/client/UnirestClient.java, and
  • src/main/java/com/messagemedia/messages/APIHelper.java

but the package isn't part of this project or any of its dependencies.

Cheers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.