Git Product home page Git Product logo

text-sdk-dotnet's Introduction

.NET build & tests NuGetV NuGetDownloads

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.

var client = new TextClient(new Guid(ConfigurationManager.AppSettings["ApiKey"]));

Send a message

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

var result = await client.SendMessageAsync("Message_Text", "Sender_Name", new List<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

var apiKey = new Guid(ConfigurationManager.AppSettings["ApiKey"]);
var client = new TextClient(apiKey);
var builder = new MessageBuilder("Message Text", "Sender_name", "Recipient_PhoneNumber");
builder
    .WithAllowedChannels(Channel.WhatsApp)
    .WithRichMessage(
        new MediaMessage(
            "cm.com",
            "https://avatars3.githubusercontent.com/u/8234794?s=200&v=4",
            "image/png"
        )
    );
var message = builder.Build();
var result = await client.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

var apiKey = new Guid(ConfigurationManager.AppSettings["ApiKey"]);
var client = new TextClient(apiKey);
var builder = new MessageBuilder("Message Text", "Sender_name", "Recipient_PhoneNumber");
builder
 .WithAllowedChannels(Channel.WhatsApp)
 .WithTemplate(new TemplateMessage() {
  Content = new TemplateMessageContent() {
   Whatsapp = new WhatsappTemplate() {
    Name = "template-name",
     Namespace = "the-namespace-of-template",
     Language = new Language() {
      Code = "en",
       Policy = "deterministic"
     },
     Components = new TemplateComponents[] {
      new TemplateComponents() {
       Type = "body",
        ComponentParameters = new ComponentParameters[] {
         new ComponentParameters() {
          Type = "text",
           Text = "firstname"
         }
        }
      },
     }
   }
  }
 });

var message = builder.Build();
var result = await client.SendMessageAsync(message);

Sending a rich WhatsApp template message

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

var apiKey = new Guid(ConfigurationManager.AppSettings["ApiKey"]);
var client = new TextClient(apiKey);
var builder = new MessageBuilder("Message Text", "Sender_name", "Recipient_PhoneNumber");
builder
 .WithAllowedChannels(Channel.WhatsApp)
 .WithTemplate(new TemplateMessage() {
  Content = new TemplateMessageContent() {
   Whatsapp = new WhatsappTemplate() {
    Name = "template-name",
     Namespace = "the-namespace-of-template",
     Language = new Language() {
      Code = "en",
       Policy = "deterministic"
     },
     Components = new TemplateComponents[] {
      new TemplateComponents() {
        Type = "header",
         ComponentParameters = new ComponentParameters[] {
          new ComponentParameters() {
           Type = "image",
            Media = new MediaContent() {
             MediaName = "cm.com",
              MediaUri = "https://avatars3.githubusercontent.com/u/8234794?s=200&v=4"
            }
          }
         }
       },
       new TemplateComponents() {
        Type = "body",
         ComponentParameters = new ComponentParameters[] {
          new ComponentParameters() {
           Type = "text",
            Text = "firstname"
          }
         }
       },
     }
   }
  }
 });

var message = builder.Build();
var result = await client.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

var apiKey = new Guid(ConfigurationManager.AppSettings["ApiKey"]);
var client = new TextClient(apiKey);
var builder = new MessageBuilder("Message Text", "Sender_name", "Recipient_PhoneNumber");
 builder
 .WithAllowedChannels(Channel.WhatsApp)
 .WithTemplate(new TemplateMessage() {
  Content = new TemplateMessageContent() {
   Whatsapp = new WhatsappTemplate() {
                            Name = "template-name",
                            Namespace = "the-namespace-of-template",
                            Language = new Language()
                            {
                                Code = "en",
                                Policy = "deterministic"
                            },
                            Components = new TemplateComponents[] {
                                new TemplateComponents() {
                                    Type = "header",
                                    ComponentParameters = new ComponentParameters[] {
                                        new ComponentParameters() {
                                            Type = "image",
                                            Media = new MediaContent() {
                                                MediaName = "cm.com",
                                                MediaUri = "https://avatars3.githubusercontent.com/u/8234794?s=200&v=4"
                                            },
                                        }
                                    }
                                },
                                new TemplateComponents()
                                {
                                    Type = "body",
                                    ComponentParameters = new ComponentParameters[]
                                    {
                                        new ComponentParameters()
                                       {
                                           Type = "currency",
                                           Currency = new TemplateCurrency()
                                           {
                                               FallbackValue = "$100.99",
                                               Amount = 100990,
                                               CurrencyCode = "USD"
                                           }
                                       },
                                       new ComponentParameters()
                                       {
                                           Type = "date_time",
                                           DateTime = new TemplateDateTime(DateTime.Now)
                                       }
                                    }}
                            }
                        }
                    }
                });

   var message = builder.Build();
   var result = await client.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

var apiKey = new Guid(ConfigurationManager.AppSettings["ApiKey"]);
var client = new TextClient(apiKey);
var builder = new MessageBuilder("Message Text", "Sender_name", "Recipient_PhoneNumber");
builder.WithAllowedChannels(Channel.WhatsApp).WithTemplate(new TemplateMessage() {
	Content = new TemplateMessageContent() {
		Whatsapp = new WhatsappTemplate() {
			Name = "Template name",
			Namespace = "whatsapp template id",
			Language = new Language() {
				Code = "en",
				Policy = "deterministic"
			},
			Components = new TemplateComponents[] {
				new TemplateComponents() {
					Type = "body",
					ComponentParameters = new ComponentParameters[] {
						new ComponentParameters() {
							Type = "text",
							Text = "your message here"
						}
					}
				},
				new TemplateComponents() {
					Type = "button",
					SubType = "quick_reply",
					Index = 0,
					ComponentParameters = new ComponentParameters[] {
						new ComponentParameters() {
							Type = "payload",
							Payload = "developer defined payload"
						}
					}
				}
			}
		}
	}
});

var message = builder.Build();
var result = await client.SendMessageAsync(message);

Sending an Apple Pay Request

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

var apiKey = new Guid(ConfigurationManager.AppSettings["ApiKey"]);
var client = new TextClient(apiKey);
var builder = new MessageBuilder("Message Text", "Sender_name", "Recipient_PhoneNumber");
 builder
        .WithAllowedChannels(Channel.iMessage)
        .WithApplePay(new ApplePayRequest()
           {
             ApplePayConfiguration = new ApplePayConfiguration()
                 {
                        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 = new LineItem[]
                        {
                            new LineItem()
                            {
                                Amount = 1,
                                Label = "product-name",
                                Type = "final-or-pending"
                            },
                        }
                    }
                });
            
var message = builder.Build();
var result = await client.SendMessageAsync(message);

Sending WhatsApp interactive messages

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

var apiKey = new Guid(ConfigurationManager.AppSettings["ApiKey"]);
var client = new TextClient(apiKey);
 var builder = new MessageBuilder("Message Text", "Sender_name", "Recipient_PhoneNumber");
     builder.WithAllowedChannels(Channel.WhatsApp).WithInteractive(new CM.Text.BusinessMessaging.Model.MultiChannel.WhatsAppInteractiveMessage()
            {
                whatsAppInteractiveContent = new CM.Text.BusinessMessaging.Model.MultiChannel.WhatsAppInteractiveContent()
                {
                    Type = "list",
                    Header = new CM.Text.BusinessMessaging.Model.MultiChannel.InteractiveHeader()
                    {
                        Type = "text",
                        Text = "List message example"
                    },
                    Body = new CM.Text.BusinessMessaging.Model.MultiChannel.InteractiveBody()
                    {
                        Text = "checkout our list message demo"
                    },
                    Action = new CM.Text.BusinessMessaging.Model.MultiChannel.InteractiveAction()
                    {
                        Button = "button text",
                        Sections = new CM.Text.BusinessMessaging.Model.MultiChannel.InteractiveSection[]
                         {
                             new CM.Text.BusinessMessaging.Model.MultiChannel.InteractiveSection()
                             {
                                 Title = "Select an option",
                                 Rows = new CM.Text.BusinessMessaging.Model.MultiChannel.Rows[]
                                 {
                                     new CM.Text.BusinessMessaging.Model.MultiChannel.Rows()
                                     {
                                         Id = "unique Id",
                                         Title = "unique title1",
                                         Description = "description text"
                                     },
                                     new CM.Text.BusinessMessaging.Model.MultiChannel.Rows()
                                     {
                                         Id = "unique Id2",
                                         Title = "unique title2",
                                         Description = "description text"
                                     },
                                 }
                             }
                         }
                    },
                    Footer = new CM.Text.BusinessMessaging.Model.MultiChannel.InteractiveFooter()
                    {
                        Text = "footer text"
                    }
                }
        });
            
var message = builder.Build();
var result = await client.SendMessageAsync(message);

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

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

Using the OTP API

Send a simple OTP code

    var client = new TextClient(new Guid(ConfigurationManager.AppSettings["ApiKey"]));
    var otpBuilder = new OtpRequestBuilder("Sender_name", "Recipient_PhoneNumber");
    otpBuilder.WithMessage("Your otp code is {code}.");
    var result = await textClient.SendOtpAsync(otpBuilder.Build());

Verify the response code

    var verifyResult = client.VerifyOtp("OTP-ID", "code");
    bool isValid = verifyResult.Verified;

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

text-sdk-dotnet's People

Contributors

alkiimista avatar bgijzen avatar cc3241 avatar defcon84 avatar enessene avatar ericsmekens avatar hexonthebeach avatar joepb avatar michaelvdnet avatar mohammed-kamal-cm avatar mvesign avatar soufyanbreda avatar stanniev avatar tomodutch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

text-sdk-dotnet's Issues

Add support for the "tag" property inside IRichMessag

According to the documentation it's possible to send a tag along with the message which could affect the time window in channels like facebook. My project would benefit from this feature

In the example below we send a simple text message with a message tag "POST_PURCHASE_UPDATE" applied. This tag is used to notify the user of an update on a recent purchase.

{
    "messages": {
        "authentication": {
            "producttoken": "Your product token"
        },
        "msg": [
            {
                "from": "Your Page ID",
                "to": [
                    {
                        "number": "Recipients PSID"
                    }
                ],
                "body": {
                    "type": "auto",
                    "content": "Fallback Text"
                },
                "allowedChannels": ["Facebook Messenger"],
                "richContent": {
                    "conversation": [
                        {
                            "text": "Dear customer, your order with id 1234xyz has been shipped.",
                            "tag": "POST_PURCHASE_UPDATE"
                        }
                    ]
                }
            }
        ]
    }
}

Not possible to set suggestions inside a msg

Given the following example from the documentation:
https://docs.cmtelecom.com/en/api/business-messaging-api/1.0/index#abc-suggestions

{
    "messages": {
        "authentication": {
            "productToken": "your product token"
        },
        "msg": [
            {
                "richContent": {
                    "conversation": [{
                        "text": "CM.com - Be part of it.",
                        "suggestions": [
                            {
                                "action": "OpenUrl",
                                "label": "CM.com - Be part of it.",
                                "url": "https://www.cm.com/products/talk/",
                                "media": {
                                    "mediaName": "cm-com.png",
                                    "mediaUri": "https://www.cm.com/cdn/cm/cm-com.png",
                                    "mimeType": "image/png"
                                }
                            }
                        ]
                    }]
                },
                "body": { "content": "CM.com - Be part of it." },
                "from": "CM Test",
                "to": [
                    { "number": "ABC-identifier" }
                ]
            }
        ]
    }
}

I don't see a possibility to set the suggestions on the msg object itself using the MessageBuilder

Add interface to TextClient class

Greetings,

To make it possible to make a mock during unit testing, please add an interface to the TextClient class.

Thanks in advance,
Stan Verdiesen

Add support for custom end points

Both for testing and to use specific regional endpoints, it would be very useful to have the ability to configure the URI used by the client.

Body.type=null causes an error on the Gateway

When sending a custom built message with the body.type not set to auto, results in failure:

...
"body": {
  "content": "Testing",
  "type": null
},
...

results in the response:

{
  "details": "Created 0 message(s)",
  "errorCode": 201,
  "messages": [
    {
      "to": "00xxxxxxxxxx99",
      "status": "Rejected",
      "reference": null,
      "parts": 0,
      "messageDetails": "Could not send message",
      "messageErrorCode": 999
    }
  ]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.