Skip to content

Repository files navigation

.NET build & testsNuGetVNuGetDownloads

CM Text SDK

A software development kit to provide ways to interact with CM.com's Text service. API's used:

Usage

Instantiate the client

Using your unique ApiKey (or product token) which authorizes you on the CM platform. Always keep this key secret!

The product token can be found in the Channels application on the platform, under the Gateway section.

varclient=newTextClient(newGuid(ConfigurationManager.AppSettings["ApiKey"]));

Send a message

By calling SendMessageAsync and providing message text, sender name, recipient phone number(s) and a reference (optional).

varresult=awaitclient.SendMessageAsync("Message_Text","Sender_Name",newList<string>{"Recipient_PhoneNumber"},"Your_Reference").ConfigureAwait(false);

Get the result

SendMessageAsync returns an object of type TextClientResult, example:

{"statusMessage":"Created 1 message(s)","statusCode":201,"details":[{"reference":"Example_Reference","status":"Accepted","to":"Example_PhoneNumber","parts":1,"details":null},{"reference":"Example_Reference2","status":"Rejected","to":"Example_PhoneNumber2","parts":0,"details":"A body without content was found"}]}

Sending a rich message

By using the MessageBuilder it is possible to create images with media for channels such as WhatsApp and RCS

varapiKey=newGuid(ConfigurationManager.AppSettings["ApiKey"]);varclient=newTextClient(apiKey);varbuilder=newMessageBuilder("Message Text","Sender_name","Recipient_PhoneNumber");builder.WithAllowedChannels(Channel.WhatsApp).WithRichMessage(newMediaMessage("cm.com","https://avatars3.githubusercontent.com/u/8234794?s=200&v=4","image/png"));varmessage=builder.Build();varresult=awaitclient.SendMessageAsync(message);

Status codes

For all possible status codes, please reference the TextClientStatusCode enum.

Sending a WhatsApp template message

By using the MessageBuilder it is possible to create template messages. Please note that this is WhatsApp only and your template needs to be approved before sending. For more info please check our documentation: https://www.cm.com/en-en/app/docs/api/business-messaging-api/1.0/index#whatsapp-template-message

varapiKey=newGuid(ConfigurationManager.AppSettings["ApiKey"]);varclient=newTextClient(apiKey);varbuilder=newMessageBuilder("Message Text","Sender_name","Recipient_PhoneNumber");builder.WithAllowedChannels(Channel.WhatsApp).WithTemplate(newTemplateMessage(){Content=newTemplateMessageContent(){Whatsapp=newWhatsappTemplate(){Name="template-name",Namespace="the-namespace-of-template",Language=newLanguage(){Code="en",Policy="deterministic"},Components=newTemplateComponents[]{newTemplateComponents(){Type="body",ComponentParameters=newComponentParameters[]{newComponentParameters(){Type="text",Text="firstname"}}},}}}});varmessage=builder.Build();varresult=awaitclient.SendMessageAsync(message);

Sending a rich WhatsApp template message

It is also possible to send a rich template with an image!

varapiKey=newGuid(ConfigurationManager.AppSettings["ApiKey"]);varclient=newTextClient(apiKey);varbuilder=newMessageBuilder("Message Text","Sender_name","Recipient_PhoneNumber");builder.WithAllowedChannels(Channel.WhatsApp).WithTemplate(newTemplateMessage(){Content=newTemplateMessageContent(){Whatsapp=newWhatsappTemplate(){Name="template-name",Namespace="the-namespace-of-template",Language=newLanguage(){Code="en",Policy="deterministic"},Components=newTemplateComponents[]{newTemplateComponents(){Type="header",ComponentParameters=newComponentParameters[]{newComponentParameters(){Type="image",Media=newMediaContent(){MediaName="cm.com",MediaUri="https://avatars3.githubusercontent.com/u/8234794?s=200&v=4"}}}},newTemplateComponents(){Type="body",ComponentParameters=newComponentParameters[]{newComponentParameters(){Type="text",Text="firstname"}}},}}}});varmessage=builder.Build();varresult=awaitclient.SendMessageAsync(message);

Sending a WhatsApp template message with date and Currency

It is also possible to send a rich template with an currency and an date! please note that the timezone is in UTC format

varapiKey=newGuid(ConfigurationManager.AppSettings["ApiKey"]);varclient=newTextClient(apiKey);varbuilder=newMessageBuilder("Message Text","Sender_name","Recipient_PhoneNumber");builder.WithAllowedChannels(Channel.WhatsApp).WithTemplate(newTemplateMessage(){Content=newTemplateMessageContent(){Whatsapp=newWhatsappTemplate(){Name="template-name",Namespace="the-namespace-of-template",Language=newLanguage(){Code="en",Policy="deterministic"},Components=newTemplateComponents[]{newTemplateComponents(){Type="header",ComponentParameters=newComponentParameters[]{newComponentParameters(){Type="image",Media=newMediaContent(){MediaName="cm.com",MediaUri="https://avatars3.githubusercontent.com/u/8234794?s=200&v=4"},}}},newTemplateComponents(){Type="body",ComponentParameters=newComponentParameters[]{newComponentParameters(){Type="currency",Currency=newTemplateCurrency(){FallbackValue="$100.99",Amount=100990,CurrencyCode="USD"}},newComponentParameters(){Type="date_time",DateTime=newTemplateDateTime(DateTime.Now)}}}}}}});varmessage=builder.Build();varresult=awaitclient.SendMessageAsync(message);

Sending interactive template messages

Interactive templates allows you to send templates that include buttons. For more info please visit https://www.cm.com/app/docs/en/api/business-messaging-api/1.0/index#/whatsapp-template-message

varapiKey=newGuid(ConfigurationManager.AppSettings["ApiKey"]);varclient=newTextClient(apiKey);varbuilder=newMessageBuilder("Message Text","Sender_name","Recipient_PhoneNumber");builder.WithAllowedChannels(Channel.WhatsApp).WithTemplate(newTemplateMessage(){Content=newTemplateMessageContent(){Whatsapp=newWhatsappTemplate(){Name="Template name",Namespace="whatsapp template id",Language=newLanguage(){Code="en",Policy="deterministic"},Components=newTemplateComponents[]{newTemplateComponents(){Type="body",ComponentParameters=newComponentParameters[]{newComponentParameters(){Type="text",Text="your message here"}}},newTemplateComponents(){Type="button",SubType="quick_reply",Index=0,ComponentParameters=newComponentParameters[]{newComponentParameters(){Type="payload",Payload="developer defined payload"}}}}}}});varmessage=builder.Build();varresult=awaitclient.SendMessageAsync(message);

Sending an Apple Pay Request

It is now possible to send an apple pay request only possible in Apple Business Chat

varapiKey=newGuid(ConfigurationManager.AppSettings["ApiKey"]);varclient=newTextClient(apiKey);varbuilder=newMessageBuilder("Message Text","Sender_name","Recipient_PhoneNumber");builder.WithAllowedChannels(Channel.iMessage).WithApplePay(newApplePayRequest(){ApplePayConfiguration=newApplePayConfiguration(){Total=1,RecipientCountryCode="recipient-country-code",CurrencyCode="currency-code",Description="product-description",RecipientEmail="recipient-email",languageCountryCode="language-country-code",OrderReference="unique-order-guid",MerchantName="merchant-name",LineItems=newLineItem[]{newLineItem(){Amount=1,Label="product-name",Type="final-or-pending"},}}});varmessage=builder.Build();varresult=awaitclient.SendMessageAsync(message);

Sending WhatsApp interactive messages

It is now possible to send list messages and reply buttons without using templates only supported in WhatsApp

varapiKey=newGuid(ConfigurationManager.AppSettings["ApiKey"]);varclient=newTextClient(apiKey);varbuilder=newMessageBuilder("Message Text","Sender_name","Recipient_PhoneNumber");builder.WithAllowedChannels(Channel.WhatsApp).WithInteractive(newCM.Text.BusinessMessaging.Model.MultiChannel.WhatsAppInteractiveMessage(){whatsAppInteractiveContent=newCM.Text.BusinessMessaging.Model.MultiChannel.WhatsAppInteractiveContent(){Type="list",Header=newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveHeader(){Type="text",Text="List message example"},Body=newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveBody(){Text="checkout our list message demo"},Action=newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveAction(){Button="button text",Sections=newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveSection[]{newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveSection(){Title="Select an option",Rows=newCM.Text.BusinessMessaging.Model.MultiChannel.Rows[]{newCM.Text.BusinessMessaging.Model.MultiChannel.Rows(){Id="unique Id",Title="unique title1",Description="description text"},newCM.Text.BusinessMessaging.Model.MultiChannel.Rows(){Id="unique Id2",Title="unique title2",Description="description text"},}}}},Footer=newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveFooter(){Text="footer text"}}});varmessage=builder.Build();varresult=awaitclient.SendMessageAsync(message);

Only with Reply buttons you can send media like image,video or document see following example.

varapiKey=newGuid(ConfigurationManager.AppSettings["ApiKey"]);varclient=newTextClient(apiKey);varbuilder=newMessageBuilder("Message Text","Sender_name","Recipient_PhoneNumber");builder.WithAllowedChannels(Channel.WhatsApp).WithInteractive(newCM.Text.BusinessMessaging.Model.MultiChannel.WhatsAppInteractiveMessage(){whatsAppInteractiveContent=newCM.Text.BusinessMessaging.Model.MultiChannel.WhatsAppInteractiveContent(){Type="button",Header=newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveHeader(){Type="image",Media=newCM.Text.BusinessMessaging.Model.MultiChannel.MediaContent(){MediaUri="https://www.cm.com/cdn/web/blog/content/logo-cmcom.png"}},Body=newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveBody(){Text="checkout our reply message demo"},Action=newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveAction(){Buttons=newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveButton[]{newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveButton(){Type="reply",Reply=newCM.Text.BusinessMessaging.Model.MultiChannel.ReplyMessage(){Id="unique-postback-id1",Title="First Button"}},newCM.Text.BusinessMessaging.Model.MultiChannel.InteractiveButton(){Type="reply",Reply=newCM.Text.BusinessMessaging.Model.MultiChannel.ReplyMessage(){Id="unique-postback-id2",Title="Second Button "}}}}}});varmessage=builder.Build();varresult=awaitclient.SendMessageAsync(message);

Using the OTP API

Send a simple OTP code

varclient=newTextClient(newGuid(ConfigurationManager.AppSettings["ApiKey"]));varotpBuilder=newOtpRequestBuilder("Sender_name","Recipient_PhoneNumber");otpBuilder.WithMessage("Your otp code is {code}.");varresult=awaittextClient.SendOtpAsync(otpBuilder.Build());

Verify the response code

varverifyResult=client.VerifyOtp("OTP-ID","code");boolisValid=verifyResult.Verified;

For more advanced scenarios see also https://developers.cm.com/identity/docs/one-time-password-create