A dotnet client library for the KMD Logic SMS, which allows to create a provider configurations and send sms.
To use this library in ASP.NET Core application add reference to Kmd.Logic.Sms.Client nuget package, and add a reference to Kmd.Logic.Identity.Authorization.
Next register the services as follows:
services.AddHttpClient(); // https://www.nuget.org/packages/Microsoft.Extensions.Http
services.AddSingleton(new LogicTokenProviderFactory(
new LogicTokenProviderOptions
{
ClientId = "Logic client credentials -> client ID",
ClientSecret = "Logic client credentials -> client secret",
AuthorizationScope = "Logic client credentials -> authorization scope",
}));
services.AddScoped<SmsClient>(c =>
{
var httpClientFactory = c.GetService<IHttpClientFactory>();
var logicTokenProviderFactory = c.GetRequiredService<LogicTokenProviderFactory>();
var client = new SmsClient(new TokenCredentials(logicTokenProviderFactory.GetProvider(httpClientFactory.CreateClient())));
return client;
});
After that in order to use it just inject ISmsClient
into constructor and use it like this:
var subscriptionId = new Guid("your Logic subscription ID");
var providerConfigurationId = new Guid("your Logic SMS configuration ID");
await _smsClient.SendSmsAsync(subscriptionId, new Kmd.Logic.Sms.Client.Models.SendSmsRequest
{
Body = "Hello, world!",
ProviderConfigurationId = providerConfigurationId,
ToPhoneNumber = "put some phone number here",
});
Add a reference to the Kmd.Logic.Sms.Client nuget package.
Create an instance of ISmsClient
like this:
var credentials = new TokenCredentials(token);
ISmsClient client = new SmsClient(credentials)
Before you can use Logic SMS, you will need to create a Logic Subscription and obtain your SubscriptionId
from https://console.kmdlogic.io/subscriptions.
You will need a bearer token issued from Logic Identity and it must have access to your logic subscription.
Typically, in SMS, you will want to use the client credentials grant, and you'll have to request creation of client credentials via the console at https://console.kmdlogic.io/subscriptions/{SubscriptionId}/client-credentials
.
However, there's a few other options we sometimes use for development or testing purposes.
- Ensure you are a member of the subscription, and then acquire a token manually from here
- Ensure you are member fo the subscription, and copy your personal token by spying on API requests made by the logic console.
var fakeConfig = client.CreateFakeSmsProviderConfiguration(
subscriptionId: config.SubscriptionId,
request: new FakeProviderConfigurationRequest(
displayName: "My Fake Config",
configuration: new FakeProviderConfig(
fromPhoneNumber: "+61411000000",
smsServiceWindow: null)));
var linkMobilityConfig = client.CreateLinkMobilityProviderConfiguration(
subscriptionId: subscriptionId,
request: new ProviderConfigurationRequestLinkMobilityProviderConfig(
displayName: "My Link Mobility Provider",
new LinkMobilityProviderConfig(
apiKey: apiKey,
sender: sender),
new SendTestSmsRequest(
toPhoneNumber: toPhoneNumber,
body: "A test to validate the provider config")));
var linkMobilityCgiConfig = client.CreateLinkMobilityCgiProviderConfiguration(
subscriptionId: SubscriptionId,
request: new LinkMobilityCgiProviderConfigProviderConfigurationRequest(
displayName: "My Link Mobility Cgi Provider",
new LinkMobilityCgiProviderConfig(
userName: userName,
password: password,
platformId: platformId,
platformPartnerId: platformPartnerId,
source: source,
smsServiceWindow: null),
new SendTestSmsRequest(
toPhoneNumber: toPhoneNumber,
body: "A test to validate the provider config")));
var logicConfig = client.CreateLogicProviderConfiguration(
subscriptionId: subscriptionId,
request: new LogicProviderConfigurationRequestLogicProviderConfig(
displayName: "My Logic Provider",
configuration: new LogicProviderConfig(
description: "Logic Provider",
smsServiceWindow: null)));
var twilioConfig = client.CreateTwilioProviderConfiguration(
subscriptionId: subscriptionId,
request: new ProviderConfigurationRequestTwilioProviderConfig(
displayName: "My Twilio Provider",
new TwilioProviderConfig(
username: username,
password: password,
accountSid: accountSid,
fromProperty: fromProperty,
smsServiceWindow: null),
new SendTestSmsRequest(
toPhoneNumber: toPhoneNumber,
body: "A test to validate the provider config")));
var sendSmsResult = await _smsClient.SendSmsAsync(subscriptionId, new Kmd.Logic.Sms.Client.Models.SendSmsRequest
{
Body = "Hello, world!",
ProviderConfigurationId = providerConfigurationId,
ToPhoneNumber = "put some phone number here",
callbackUrl = "provide your callback Url",
userData = "Can be anything upto 500 characters. If provided, this custom data will be passed to the callback"
});
var sendBulkSmsResult = await _smsClient.SendBulkSmsAsync(subscriptionId, new Kmd.Logic.Sms.Client.Models.SendBulkSmsRequest
{
ToPhoneNumber = [
{
number = "phone number 1"
},
{
number = "phone number 2"
}
],
Body = "Hello, world!",
ProviderConfigurationId = providerConfigurationId,
callbackUrl = "provide your callback Url",
userData = "string"
});
Contact us at discover@kmdlogic.io for more information.