Prerequisites:
Step 1: Set up a Project
For this project, we’re going to use Maven. If you are unfamiliar with this build tool, you will need to include the files or a .jar file in your project. However, the SDK has been published to Maven to make this easier for you. Start a new Maven project by selecting File > New > Other… > Maven > Maven Project. Select “create a simple project” to skip archetype selection. Give it a group ID and an artefact ID. For this example, we are making "MessageMedia" and "TestProject". You will need to add a name and a unique description. Next, close any loading screens and open up the pom.xml file in the package explorer.
Step 2: Install the SDK
To keep things simple, we will import java dependencies via Maven. To do so, paste the following xml into your pom.xml file within the main tags:
<dependencies>
<dependency>
<groupId>com.messagemedia.sdk</groupId>
<artifactId>messages</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
Step 3: Add the Code
Right-click on src/main/java and a class. Select the “public static void main option for the method stub” and call the class "App".
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("+614<Your number here>");
List<Message> messagesList = new ArrayList<>();
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");
}
});
}
}
Step 4: Send
In Eclipse, press the Run Green arrow button. If it all works, “success” will print in the console and you will receive the message.
Note: To include details in the response, remember to edit the console message with the relevant information.
Congratulations you just sent your first message with Java!
Check out the API documentation or GitHub repository to see what else you can do next.
Comments
0 comments
Article is closed for comments.