https://github.com/cmdotcom/text-sdk-java
Java SDK to send messages with CM.com
https://github.com/cmdotcom/text-sdk-java
cm imessage java sdk sms telegram whatsapp
Last synced: 8 months ago
JSON representation
Java SDK to send messages with CM.com
- Host: GitHub
- URL: https://github.com/cmdotcom/text-sdk-java
- Owner: cmdotcom
- License: mit
- Created: 2019-03-21T09:18:25.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-06-14T22:05:03.000Z (over 1 year ago)
- Last Synced: 2025-04-04T09:36:07.288Z (9 months ago)
- Topics: cm, imessage, java, sdk, sms, telegram, whatsapp
- Language: Java
- Homepage:
- Size: 121 KB
- Stars: 4
- Watchers: 15
- Forks: 10
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## @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://developers.cm.com/messaging)
## Installing
Warning: Namespace has been changed between 1.3 and 2.0
### Above 2.0
You can find our SDK [here](https://mvnrepository.com/artifact/com.cm/text-sdk), or below incase you are using a generic pom.xml.
```xml
com.cm
text-sdk
3.0.0
```
### Version 1.3
Version 1.3 is available as JAR under the [1.3 release here](https://github.com/cmdotcom/text-sdk-java/releases/tag/v1.3-snapshot)
You will have to add GSON manually: https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5
## Instantiate the client
Use your productToken which authorizes you on the CM platform. Get yours on CM.com
```java
MessagingClient client = new MessagingClient("YourCMProductToken");
```
## Send a message
By calling `SendTextMessage` and providing message text, sender name, recipient phone number(s).
```java
MessagingClient client = new MessagingClient("YourProductToken");
client.sendTextMessage("Message Text", "TestSender", new String[] {"00316012345678"});
```
## Sending a message with auto detect encoding
By using the `MessageBuilder` it is possible to send messages with auto detect encoding,
It is possible to let our gateway detect the encoding for you by including the type: auto setting.
In case it detects characters that are not part of the GSM character set, the message will be delivered as Unicode.
see our API docs for more info https://developers.cm.com/messaging/
```java
MessagingClient client = new MessagingClient("YourProductToken");
MessageBuilder builder = new MessageBuilder("Message Text", "auto", "TestSender", new String[] {"00316012345678"});
Message message = builder.Build();
client.sendMessage(message);
```
## Sending a rich message
By using the `MessageBuilder` it is possible to create images with media for channels such as WhatsApp and Viber
```java
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);
```
## Get the result
Sending an message returns the response body
```java
{
"details": "Created 1 message(s)",
"errorCode": 0,
"messages": [{
"to": "00316012345678",
"status": "Accepted",
"reference": null,
"parts": 1,
"messageDetails": null,
"messageErrorCode": 0
}]
}
```
## Whatsapp Templates
Send WhatsApp template messages using the message builder please take a look at our documentation in the [Whatsapp templates section](https://developers.cm.com/messaging/docs/whatsapp#template)
```java
MessagingClient client = new MessagingClient("YourProductToken");
MessageBuilder builder = new MessageBuilder("Template Test", "CM.COM", new String[] {"0031636170815"});
builder.WithAllowedChannels(new Channel[] {Channel.WhatsApp});
TemplateMessage template = new TemplateMessage();
template.Content = new TemplateMessageContent();
template.Content.WhatsAppTemplate = new WhatsAppTemplate();
template.Content.WhatsAppTemplate.Name = "template-name";
template.Content.WhatsAppTemplate.Namespace = "the-namespace-of-template";
template.Content.WhatsAppTemplate.Language = new TemplateLanguage("CountryCode", "deterministic");
template.Content.WhatsAppTemplate.LocalizableParams = new LocalizableParam[] {};
template.Content.WhatsAppTemplate.Components = new TemplateComponents[] {new TemplateComponents("header",
new TemplateParameters[] { new TemplateParameters("image", new MediaContent("cm.com"",
"https://avatars3.githubusercontent.com/u/8234794?s=200&v=4",
"image/png"))}),
new TemplateComponents("body",
new TemplateParameters[] { new TemplateParameters("text", "TestMessage"),
new TemplateParameters("text", "Dutch GP")})};
builder.WithTemplate(template);
Message message = builder.Build();
client.sendMessage(message);
```
## Using the OTP API
Send an OTP code
```java
MessagingClient client = new MessagingClient(yourProductToken);
OtpRequest request = new OtpRequestBuilder(senderName, recipientNumber)
.withMessage("Your OTP code is {code}")
.withChannel("sms")
.build();
OtpResponse result = client.sendOtpRequest(request);
```
Verify the response code
```java
OtpResponse verifyResult = client.verifyOtpRequest(result.getId(), code);
verifyResult.isVerified(); //represents whether the check was code was correct
```