Skip to content

Development #1

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 4 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## @cmdotcom/text-sdk: A helper library to send messages.

Want to send messages in your Java application? Then you are at the right place.
If you want to get all the functionalities, go to: [CM.com API Docs](https://docs.cmtelecom.com/bulk-sms/v1.0)


## Installing

Add the jar file to your project.

## Instantiate the client
Use your productToken which authorizes you on the CM platform. Get yours on CM.com

```cs
MessagingClient client = new MessagingClient("YourproductToken");
```

## Send a message
By calling `SendTextMessage` and providing message text, sender name, recipient phone number(s).

```cs
MessagingClient client = new MessagingClient("YourProductToken");
client.sendTextMessage("Message Text", "TestSender", new String[] {"00316012345678"});

```


## Sending a rich message
By using the `MessageBuilder` it is possible to create images with media for channels such as WhatsApp and Viber
```cs
MessagingClient client = new MessagingClient("YourProductToken");

MessageBuilder builder = new MessageBuilder("Message Text", "TestSender", new String[] {"00316012345678"});

builder.WithAllowedChannels(new Channel[] {Channel.Viber});

builder.WithRichMessage(new MediaMessage(
"cm.com",
"https://avatars3.githubusercontent.com/u/8234794?s=200&v=4",
"image/png"));


Message message = builder.Build();

client.sendMessage(message);
```
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>text-sdk-java</groupId>
<artifactId>text-sdk-java</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
</project>
4 changes: 4 additions & 0 deletions src/main/java/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class Config {

public static String ApiUrl = "https://gw.cmtelecom.com/v1.0/message";
}
107 changes: 107 additions & 0 deletions src/main/java/MessageBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import models.Body;
import models.Channel;
import models.Message;
import models.Recipient;
import models.multichannel.IRichMessage;
import models.multichannel.RichContent;
import models.multichannel.Suggestion;

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

public class MessageBuilder {

private Message message;
private RichContent richContent;


/// <summary>
/// Creates a new MessageBuilder
/// </summary>
/// <param name="messageText"></param>
/// <param name="from"></param>
/// <param name="to"></param>
public MessageBuilder(String messageText, String from,String[] to)
{
List<Recipient> recipientList = new ArrayList<>();
for (String number : to) {
Recipient r = new Recipient();
r.Number = number;
recipientList.add(r);

}

this.message = new Message(new Body(messageText), from, recipientList);

}


/// <summary>
/// Constructs the message.
/// </summary>
/// <returns></returns>
public Message Build()
{
this.message.RichContent = this.richContent;
return this.message;
}

/// <summary>
/// Adds the allowed channels field, which forces a message to only use certain routes.
/// You can define a list of which channels you want your message to use.
/// Not defining any channels will be interpreted as allowing all channels.
/// </summary>
/// <remarks>
/// Note that for channels other than SMS, CM needs to configure the out going flows.
/// For those flows to work, we need to be contacted.
/// </remarks>
public MessageBuilder WithAllowedChannels(Channel[] channels)
{
this.message.AllowedChannels = channels;
return this;
}

/// <summary>
/// Add a reference to the message.
/// </summary>
/// <param name="reference"></param>
/// <returns></returns>
public MessageBuilder WithReference(String reference)
{
this.message.Reference = reference;
return this;
}

/// <summary>
/// Adds a message that replaces the <see cref="Message.Body" /> for channels that support
/// rich content (all channels except <see cref="Channel.SMS" />, <see cref="Channel.Voice" />
/// and <see cref="Channel.Push" /> at this moment)
/// </summary>
/// <param name="richMessage"></param>
/// <returns></returns>
public MessageBuilder WithRichMessage(IRichMessage richMessage)
{
if (this.richContent == null)
this.richContent = new RichContent();

this.richContent.AddConversationPart(richMessage);
return this;
}

/// <summary>
/// Adds suggestions to the message. It is dependent on the channel that is used which
/// suggestions are supported.
/// </summary>
/// <param name="suggestions"></param>
/// <returns></returns>
public MessageBuilder WithSuggestions(Suggestion[] suggestions)
{
if (this.richContent == null)
this.richContent = new RichContent();

this.richContent.Suggestions = suggestions;
return this;
}

}
69 changes: 69 additions & 0 deletions src/main/java/MessagingClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import com.google.gson.Gson;
import models.Channel;
import models.Message;
import models.Request;

import utils.HttpHelper;

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

public class MessagingClient {

private String productToken;

public MessagingClient(String productToken){
this.productToken = productToken;
}


public void sendTextMessage(String messageText, String from,String[] to){

try{
MessageBuilder builder = new MessageBuilder(messageText,from, to);

Message message = builder.Build();

String body = GetHttpPostBody(productToken, message);

HttpHelper.post(Config.ApiUrl, body);
}

catch (Exception e){
System.out.println("Please check your request body.");
}
}


public void sendMessage(Message message) {

try{
String body = GetHttpPostBody(productToken, message);

HttpHelper.post(Config.ApiUrl, body);
}

catch (Exception e ){
System.out.println("Please check your request body.");
}
}


/// <summary>
/// Gets the HTTP post body.
/// </summary>
/// <param name="apiKey">The API key.</param>
/// <param name="message">The message to send.</param>
/// <returns></returns>
protected static String GetHttpPostBody(String productToken, Message message)
{
Request.Messages messages = new Request.Messages();
Request.MessagesEnvelope request = new Request.MessagesEnvelope();
request.setAuthentication(new Request.Authentication(productToken));
Message[] msg = new Message[]{message};
request.setMessages(msg);
messages.setMessages(request);
return new Gson().toJson(messages);
}

}
38 changes: 38 additions & 0 deletions src/main/java/models/Body.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package models;

import com.google.gson.annotations.SerializedName;

public class Body {

/// <summary>
/// The actual text body of the message.
/// By default the CM gateway interprets messages as if sent with the standard 7 bit GSM encoding.
/// If you want to send messages using e.g. Arabic, Cyrillic of Greek characters
/// you will need to use the unicode UCS2 encoding.
/// Set the <see cref="Type" /> to Auto to let the gateway do the encoding detection.
/// Please note that there are a few limitations to using unicode encoded messages:
/// Unicode messages can contain up to 70 characters. In the case of multipart messages, this becomes 66 characters per
/// part.
/// You will need to POST the XML or JSON file. A HTTP GET request cannot handle the Unicode characters
/// Another note is that not all operators in the world are able to handle Unicode messages, so you will need to test
/// for which operators it works.
/// </summary>
@SerializedName("content")
public String Content;

/// <summary>
/// When the type is set to 'auto' then the gateway will do the encoding detection.
/// In case it detects characters that are not part of the GSM character set,
/// the message will be delivered as Unicode.
/// If the message contains more than 70 characters in Unicode format it will be split into a
/// multipart message.
/// You can limit the number of parts by setting the maximum number of message parts.
/// <see cref="Message.MaximumNumberOfMessageParts" />
/// </summary>
@SerializedName("type")
public String Type;

public Body(String content) {
this.Content = content;
}
}
64 changes: 64 additions & 0 deletions src/main/java/models/Channel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package models;

public enum Channel {
/// <summary>
/// Messages will be sent as SMS text messages
/// </summary>
SMS,

/// <summary>
/// Send messages using WhatsApp for business
/// </summary>
/// <remarks>
/// Note that CM needs to configure this for you to work.
/// </remarks>
WhatsApp,

/// <summary>
/// Sends messages to push using Hybrid messages.
/// See also https://docs.cmtelecom.com/en/hybrid-messaging/v2.0.0
/// </summary>
/// <remarks>Works only when <see cref="Message.HybridAppKey" /> is set</remarks>
Push,

/// <summary>
/// Messages will be sent over RCS.
/// </summary>
/// <remarks>
/// Note that CM needs to configure this for you to work.
/// </remarks>
RCS,

/// <summary>
/// Messages will be sent over Viber.
/// </summary>
/// <remarks>
/// Note that CM needs to configure this for you to work.
/// </remarks>
Viber,

/// <summary>
/// Messages will be sent using text to speech.
/// </summary>
/// <remarks>
/// Note that CM needs to configure this for you to work.
/// </remarks>
Voice,

/// <summary>
/// Messages will be sent over Apple Business Chat.
/// </summary>
/// <remarks>
/// Note that CM needs to configure this for you to work.
/// </remarks>
// ReSharper disable once InconsistentNaming
iMessage,

/// <summary>
/// Messages will be sent over Line.
/// </summary>
/// <remarks>
/// Note that CM needs to configure this for you to work.
/// </remarks>
Line
}
Loading