Skip to content

malcos/cmdotcom-text-sdk-java

 
 

Repository files navigation

CM Telecom text-sdk for Java

This is a fork with improvements and fixes against the original implementation of this SDK. This page lists the improvements done, and which ones you may already find in the original upstream repository.

Description Status Issue(*) PR(*) Upstream(*)
messageErrorCode should be int Implemented in [0.1.0] #27 #28 -
Create unit tests (JUnit 5, AssertJ, etc) Noted - - -
Make request JSON generated by the client accessible to the developer Noted - - -
Provide a method for using different product tokens with a single client Noted - - -

(*) Link to upstream repository

  • Noted: An idea under consideration for possible planning and implementation
  • Planned: The idea was considered and deemed apt for implementation
  • WIP: Work in progress, link to local branch.
  • Implemented: The feature was implemented and available in the local fork branch, a pull request was created.
  • Accepted: The pull request was accepted and available in the original upstream project.

The following section is a copy of the original README.md with the Installing instructions removed to avoid confusions.

@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

Installing

NOTE: Installing instructions removed to avoiding confusion. See original repository for information on how to install the upstream version.

Instantiate the client

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

MessagingClient client = new MessagingClient("YourCMProductToken");

Send a message

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

   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/

 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

 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

{
	"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

		 
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

    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

    OtpResponse verifyResult = client.verifyOtpRequest(result.getId(), code);
    verifyResult.isVerified(); //represents whether the check was code was correct  

About

Java SDK to send messages with CM.com

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%