Skip to content

Commit

Permalink
Merge pull request #26 from cmdotcom/feature/otp_api
Browse files Browse the repository at this point in the history
Feature/otp api
  • Loading branch information
bgijzen committed Jan 11, 2024
2 parents 246539c + 9b3030f commit c9b0695
Show file tree
Hide file tree
Showing 41 changed files with 2,420 additions and 746 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.0.0] - 2024-01-11
### Added
- OTP API
### Changed
- Updated comments (made them javadoc style)
- Use Javabeans style getters and setters instead of public fields

## [2.1.0] - 2023-07-07
### Changed
- Implement DCS parameter (#23)
Expand All @@ -19,4 +26,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Pushed to Maven Central
### Changed
- Changed namespace from com.cmtelecom to com.cm
- Changed namespace from com.cmtelecom to com.cm
34 changes: 26 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You can find our SDK [here](https://mvnrepository.com/artifact/com.cm/text-sdk),
<dependency>
<groupId>com.cm</groupId>
<artifactId>text-sdk</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
</dependency>
```

Expand All @@ -25,16 +25,16 @@ You will have to add GSON manually: https://mvnrepository.com/artifact/com.googl
## Instantiate the client
Use your productToken which authorizes you on the CM platform. Get yours on CM.com

```cs
```java
MessagingClient client = new MessagingClient("YourCMProductToken");
```

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

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

```
## Sending a message with auto detect encoding
Expand All @@ -45,7 +45,7 @@ In case it detects characters that are not part of the GSM character set, the me

see our API docs for more info https://developers.cm.com/messaging/

```cs
```java
MessagingClient client = new MessagingClient("YourProductToken");
MessageBuilder builder = new MessageBuilder("Message Text", "auto", "TestSender", new String[] {"00316012345678"});

Expand All @@ -56,7 +56,7 @@ see our API docs for more info https://developers.cm.com/messaging/

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

MessageBuilder builder = new MessageBuilder("Message Text", "TestSender", new String[] {"00316012345678"});
Expand All @@ -76,7 +76,7 @@ By using the `MessageBuilder` it is possible to create images with media for cha

## Get the result
Sending an message returns the response body
```cs
```java
{
"details": "Created 1 message(s)",
"errorCode": 0,
Expand All @@ -93,7 +93,7 @@ Sending an message returns the response body

## 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)
```cs
```java

MessagingClient client = new MessagingClient("YourProductToken");

Expand Down Expand Up @@ -127,3 +127,21 @@ MessagingClient client = new MessagingClient("YourProductToken");
```


## 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
```
9 changes: 6 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.cm</groupId>
<artifactId>text-sdk</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<packaging>jar</packaging>
<build>
<plugins>
Expand All @@ -14,8 +14,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -57,6 +57,9 @@
<goals>
<goal>jar</goal>
</goals>
<configuration>
<failOnError>false</failOnError>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/cm/text/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package com.cm.text;

public class Config {
/**
* Api url for the business messaging api
*/
public static String BusinessMessagingApiUrl = "https://gw.messaging.cm.com/v1.0/message";

public static String ApiUrl = "https://gw.cmtelecom.com/v1.0/message";
/**
* Url used to request a new OTP
*/
public static String OtpRequestEndpoint = "https://api.cm.com/otp/v2/otp";
/**
* Url formatter used to get the link to verify OTP code
*/
public static String OtpVerifyEndpointFormatter = "https://api.cm.com/otp/v2/otp/%s/verify";
}
134 changes: 73 additions & 61 deletions src/main/java/com/cm/text/MessageBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@
import java.util.ArrayList;
import java.util.List;

/**
* Builder class to construct messages
*/
public class MessageBuilder {

private Message message;
private final Message message;
private RichContent richContent;


/// <summary>
/// Creates a new MessageBuilder
/// </summary>
/// <param name="messageText"></param>
/// <param name="from"></param>
/// <param name="to"></param>
/**
* Creates a new MessageBuilder
* @param messageText text to send in the message
* @param from sender id
* @param to recipients to send the messages towards
*/
public MessageBuilder(String messageText, String from,String[] to)
{
List<Recipient> recipientList = new ArrayList<>();
for (String number : to) {
Recipient r = new Recipient();
r.Number = number;
r.setNumber(number);
recipientList.add(r);

}

this.message = new Message(new Body(messageText), from, recipientList);
Expand All @@ -44,63 +45,69 @@ public MessageBuilder(String messageText, String from,String[] to)
/// <param name="messageText"></param>
/// <param name="from"></param>
/// <param name="to"></param>
public MessageBuilder(String messageText, String type, String from,String[] to)

/**
* Creates a new MessageBuilder
* @param messageText text to send in the message
* @param type encoding to use (use auto for auto detect encoding)
* @param from sender id
* @param to recipients to send the messages towards
*/
public MessageBuilder(String messageText, String type, String from,String[] to)
{
List<Recipient> recipientList = new ArrayList<>();
for (String number : to) {
Recipient r = new Recipient();
r.Number = number;
r.setNumber(number);
recipientList.add(r);

}

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

/// <summary>
/// Constructs the message.
/// </summary>
/// <returns></returns>
public Message Build()
/**
* Constructs the message
* @return the message to send
*/
public Message build()
{
this.message.RichContent = this.richContent;
this.message.setRichContent(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)
/**
* 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.
* 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.
* @param channels Define the list of which channels you want your message to use
* @return this builder, for chaining
*/
public MessageBuilder withAllowedChannels(Channel[] channels)
{
this.message.AllowedChannels = channels;
this.message.setAllowedChannels(channels);
return this;
}

/// <summary>
/// Add a reference to the message.
/// </summary>
/// <param name="reference"></param>
/// <returns></returns>
public MessageBuilder WithReference(String reference)
/**
* Add a reference to the message.
* @param reference the reference to use
* @return this builder, for chaining
*/
public MessageBuilder withReference(String reference)
{
this.message.Reference = reference;
this.message.setReference(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)

/**
* Adds a message that replaces the body for channels that support rich content
* @param richMessage the rich message to add
* @return this builder, for chaining
*/
public MessageBuilder withRichMessage(IRichMessage richMessage)
{
if (this.richContent == null)
this.richContent = new RichContent();
Expand All @@ -109,37 +116,42 @@ public MessageBuilder WithRichMessage(IRichMessage 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)
/**
* Adds suggestions to the message. It is dependent on the channel that is used which suggestions are supported.
* @param suggestions suggestions to add
* @return this builder, for chaining
*/
public MessageBuilder withSuggestions(Suggestion[] suggestions)
{
if (this.richContent == null)
this.richContent = new RichContent();

this.richContent.Suggestions = suggestions;
this.richContent.setSuggestions(suggestions);
return this;
}

public MessageBuilder WithTemplate(TemplateMessage template){

/**
* Adds a WhatsApp template message that replaces the body for WhatsApp messages
* please note that you need to have approved wa templates.
* @param template the template to add
* @return this builder, for chaining
*/
public MessageBuilder withTemplate(TemplateMessage template){
if (this.richContent == null)
this.richContent = new RichContent();

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

/// <summary>
/// Add a custom DCS
/// </summary>
/// <param name="dcs"></param>
/// <returns></returns>
public MessageBuilder WithDcs(int dcs)
/**
* Adds a custom DCS
* @param dcs data coding scheme see <a href="https://developers.cm.com/messaging/docs/sms">Developer docs</a> for more information
* @return this builder, for chaining
*/
public MessageBuilder withDcs(int dcs)
{
this.message.Dcs = dcs;
this.message.setDcs(dcs);
return this;
}
}

0 comments on commit c9b0695

Please sign in to comment.