Skip to content

Commit 612618e

Browse files
SoufyanbredaEricSmekens
authored andcommitted
Development (#1)
* Add project * Update readme * Delete unnecessary files + update readme * Add parameters to send text message
1 parent 9052fc8 commit 612618e

27 files changed

+968
-0
lines changed

Diff for: README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## @cmdotcom/text-sdk: A helper library to send messages.
2+
3+
Want to send messages in your Java application? Then you are at the right place.
4+
If you want to get all the functionalities, go to: [CM.com API Docs](https://docs.cmtelecom.com/bulk-sms/v1.0)
5+
6+
7+
## Installing
8+
9+
Add the jar file to your project.
10+
11+
## Instantiate the client
12+
Use your productToken which authorizes you on the CM platform. Get yours on CM.com
13+
14+
```cs
15+
MessagingClient client = new MessagingClient("YourproductToken");
16+
```
17+
18+
## Send a message
19+
By calling `SendTextMessage` and providing message text, sender name, recipient phone number(s).
20+
21+
```cs
22+
MessagingClient client = new MessagingClient("YourProductToken");
23+
client.sendTextMessage("Message Text", "TestSender", new String[] {"00316012345678"});
24+
25+
```
26+
27+
28+
## Sending a rich message
29+
By using the `MessageBuilder` it is possible to create images with media for channels such as WhatsApp and Viber
30+
```cs
31+
MessagingClient client = new MessagingClient("YourProductToken");
32+
33+
MessageBuilder builder = new MessageBuilder("Message Text", "TestSender", new String[] {"00316012345678"});
34+
35+
builder.WithAllowedChannels(new Channel[] {Channel.Viber});
36+
37+
builder.WithRichMessage(new MediaMessage(
38+
"cm.com",
39+
"https://avatars3.githubusercontent.com/u/8234794?s=200&v=4",
40+
"image/png"));
41+
42+
43+
Message message = builder.Build();
44+
45+
client.sendMessage(message);
46+
```

Diff for: pom.xml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>text-sdk-java</groupId>
7+
<artifactId>text-sdk-java</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>org.apache.maven.plugins</groupId>
13+
<artifactId>maven-compiler-plugin</artifactId>
14+
<configuration>
15+
<source>7</source>
16+
<target>7</target>
17+
</configuration>
18+
</plugin>
19+
</plugins>
20+
</build>
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.google.code.gson</groupId>
24+
<artifactId>gson</artifactId>
25+
<version>2.8.5</version>
26+
</dependency>
27+
</dependencies>
28+
</project>

Diff for: src/main/java/Config.java

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class Config {
2+
3+
public static String ApiUrl = "https://gw.cmtelecom.com/v1.0/message";
4+
}

Diff for: src/main/java/MessageBuilder.java

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import models.Body;
2+
import models.Channel;
3+
import models.Message;
4+
import models.Recipient;
5+
import models.multichannel.IRichMessage;
6+
import models.multichannel.RichContent;
7+
import models.multichannel.Suggestion;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
public class MessageBuilder {
14+
15+
private Message message;
16+
private RichContent richContent;
17+
18+
19+
/// <summary>
20+
/// Creates a new MessageBuilder
21+
/// </summary>
22+
/// <param name="messageText"></param>
23+
/// <param name="from"></param>
24+
/// <param name="to"></param>
25+
public MessageBuilder(String messageText, String from,String[] to)
26+
{
27+
List<Recipient> recipientList = new ArrayList<>();
28+
for (String number : to) {
29+
Recipient r = new Recipient();
30+
r.Number = number;
31+
recipientList.add(r);
32+
33+
}
34+
35+
this.message = new Message(new Body(messageText), from, recipientList);
36+
37+
}
38+
39+
40+
/// <summary>
41+
/// Constructs the message.
42+
/// </summary>
43+
/// <returns></returns>
44+
public Message Build()
45+
{
46+
this.message.RichContent = this.richContent;
47+
return this.message;
48+
}
49+
50+
/// <summary>
51+
/// Adds the allowed channels field, which forces a message to only use certain routes.
52+
/// You can define a list of which channels you want your message to use.
53+
/// Not defining any channels will be interpreted as allowing all channels.
54+
/// </summary>
55+
/// <remarks>
56+
/// Note that for channels other than SMS, CM needs to configure the out going flows.
57+
/// For those flows to work, we need to be contacted.
58+
/// </remarks>
59+
public MessageBuilder WithAllowedChannels(Channel[] channels)
60+
{
61+
this.message.AllowedChannels = channels;
62+
return this;
63+
}
64+
65+
/// <summary>
66+
/// Add a reference to the message.
67+
/// </summary>
68+
/// <param name="reference"></param>
69+
/// <returns></returns>
70+
public MessageBuilder WithReference(String reference)
71+
{
72+
this.message.Reference = reference;
73+
return this;
74+
}
75+
76+
/// <summary>
77+
/// Adds a message that replaces the <see cref="Message.Body" /> for channels that support
78+
/// rich content (all channels except <see cref="Channel.SMS" />, <see cref="Channel.Voice" />
79+
/// and <see cref="Channel.Push" /> at this moment)
80+
/// </summary>
81+
/// <param name="richMessage"></param>
82+
/// <returns></returns>
83+
public MessageBuilder WithRichMessage(IRichMessage richMessage)
84+
{
85+
if (this.richContent == null)
86+
this.richContent = new RichContent();
87+
88+
this.richContent.AddConversationPart(richMessage);
89+
return this;
90+
}
91+
92+
/// <summary>
93+
/// Adds suggestions to the message. It is dependent on the channel that is used which
94+
/// suggestions are supported.
95+
/// </summary>
96+
/// <param name="suggestions"></param>
97+
/// <returns></returns>
98+
public MessageBuilder WithSuggestions(Suggestion[] suggestions)
99+
{
100+
if (this.richContent == null)
101+
this.richContent = new RichContent();
102+
103+
this.richContent.Suggestions = suggestions;
104+
return this;
105+
}
106+
107+
}

Diff for: src/main/java/MessagingClient.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import com.google.gson.Gson;
2+
import models.Channel;
3+
import models.Message;
4+
import models.Request;
5+
6+
import utils.HttpHelper;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class MessagingClient {
12+
13+
private String productToken;
14+
15+
public MessagingClient(String productToken){
16+
this.productToken = productToken;
17+
}
18+
19+
20+
public void sendTextMessage(String messageText, String from,String[] to){
21+
22+
try{
23+
MessageBuilder builder = new MessageBuilder(messageText,from, to);
24+
25+
Message message = builder.Build();
26+
27+
String body = GetHttpPostBody(productToken, message);
28+
29+
HttpHelper.post(Config.ApiUrl, body);
30+
}
31+
32+
catch (Exception e){
33+
System.out.println("Please check your request body.");
34+
}
35+
}
36+
37+
38+
public void sendMessage(Message message) {
39+
40+
try{
41+
String body = GetHttpPostBody(productToken, message);
42+
43+
HttpHelper.post(Config.ApiUrl, body);
44+
}
45+
46+
catch (Exception e ){
47+
System.out.println("Please check your request body.");
48+
}
49+
}
50+
51+
52+
/// <summary>
53+
/// Gets the HTTP post body.
54+
/// </summary>
55+
/// <param name="apiKey">The API key.</param>
56+
/// <param name="message">The message to send.</param>
57+
/// <returns></returns>
58+
protected static String GetHttpPostBody(String productToken, Message message)
59+
{
60+
Request.Messages messages = new Request.Messages();
61+
Request.MessagesEnvelope request = new Request.MessagesEnvelope();
62+
request.setAuthentication(new Request.Authentication(productToken));
63+
Message[] msg = new Message[]{message};
64+
request.setMessages(msg);
65+
messages.setMessages(request);
66+
return new Gson().toJson(messages);
67+
}
68+
69+
}

Diff for: src/main/java/models/Body.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package models;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class Body {
6+
7+
/// <summary>
8+
/// The actual text body of the message.
9+
/// By default the CM gateway interprets messages as if sent with the standard 7 bit GSM encoding.
10+
/// If you want to send messages using e.g. Arabic, Cyrillic of Greek characters
11+
/// you will need to use the unicode UCS2 encoding.
12+
/// Set the <see cref="Type" /> to Auto to let the gateway do the encoding detection.
13+
/// Please note that there are a few limitations to using unicode encoded messages:
14+
/// Unicode messages can contain up to 70 characters. In the case of multipart messages, this becomes 66 characters per
15+
/// part.
16+
/// You will need to POST the XML or JSON file. A HTTP GET request cannot handle the Unicode characters
17+
/// Another note is that not all operators in the world are able to handle Unicode messages, so you will need to test
18+
/// for which operators it works.
19+
/// </summary>
20+
@SerializedName("content")
21+
public String Content;
22+
23+
/// <summary>
24+
/// When the type is set to 'auto' then the gateway will do the encoding detection.
25+
/// In case it detects characters that are not part of the GSM character set,
26+
/// the message will be delivered as Unicode.
27+
/// If the message contains more than 70 characters in Unicode format it will be split into a
28+
/// multipart message.
29+
/// You can limit the number of parts by setting the maximum number of message parts.
30+
/// <see cref="Message.MaximumNumberOfMessageParts" />
31+
/// </summary>
32+
@SerializedName("type")
33+
public String Type;
34+
35+
public Body(String content) {
36+
this.Content = content;
37+
}
38+
}

Diff for: src/main/java/models/Channel.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package models;
2+
3+
public enum Channel {
4+
/// <summary>
5+
/// Messages will be sent as SMS text messages
6+
/// </summary>
7+
SMS,
8+
9+
/// <summary>
10+
/// Send messages using WhatsApp for business
11+
/// </summary>
12+
/// <remarks>
13+
/// Note that CM needs to configure this for you to work.
14+
/// </remarks>
15+
WhatsApp,
16+
17+
/// <summary>
18+
/// Sends messages to push using Hybrid messages.
19+
/// See also https://docs.cmtelecom.com/en/hybrid-messaging/v2.0.0
20+
/// </summary>
21+
/// <remarks>Works only when <see cref="Message.HybridAppKey" /> is set</remarks>
22+
Push,
23+
24+
/// <summary>
25+
/// Messages will be sent over RCS.
26+
/// </summary>
27+
/// <remarks>
28+
/// Note that CM needs to configure this for you to work.
29+
/// </remarks>
30+
RCS,
31+
32+
/// <summary>
33+
/// Messages will be sent over Viber.
34+
/// </summary>
35+
/// <remarks>
36+
/// Note that CM needs to configure this for you to work.
37+
/// </remarks>
38+
Viber,
39+
40+
/// <summary>
41+
/// Messages will be sent using text to speech.
42+
/// </summary>
43+
/// <remarks>
44+
/// Note that CM needs to configure this for you to work.
45+
/// </remarks>
46+
Voice,
47+
48+
/// <summary>
49+
/// Messages will be sent over Apple Business Chat.
50+
/// </summary>
51+
/// <remarks>
52+
/// Note that CM needs to configure this for you to work.
53+
/// </remarks>
54+
// ReSharper disable once InconsistentNaming
55+
iMessage,
56+
57+
/// <summary>
58+
/// Messages will be sent over Line.
59+
/// </summary>
60+
/// <remarks>
61+
/// Note that CM needs to configure this for you to work.
62+
/// </remarks>
63+
Line
64+
}

0 commit comments

Comments
 (0)