Unofficial API client implementation for the linkmobility websms Messaging API 1.0.0. See API Doc.
With a special thanks to LINK Mobility Austria GmbH for the handy messaging service. See websms website.
- WebSmsNet: Client service to interact with the Messaging API 1.0.0
- WebSmsNet.Abstractions: Models, Views and Enums used for the API
- WebSmsNet.AspNetCore: Dependency injection in ASP.NET Core
For every type of setup, you need WebSmsApiOptions and you can choose how to authenticate...
- with access token:
new WebSmsApiOptions() { BaseUrl = "https://api.linkmobility.eu/"; AuthenticationType = AuthenticationType.Bearer; AccessToken = "YOUR ACCESS_TOKEN"; });
- or basic login:
new WebSmsApiOptions() { BaseUrl = "https://api.linkmobility.eu/"; AuthenticationType = AuthenticationType.Basic; Username = "YOUR USER_NAME"; Password = "YOUR PASSWORD"; });
The WebSmsApiClient can be construction by passing either WebSmsApiOptions, WebSmsApiConnectionHandler or HttpClient.
- Simplest approach with options:
var webSmsApiClient = new WebSmsApiClient(/* your options */);
- Or you can pass a custom connection handler (must derive from
WebSmsApiConnectionHandler):var webSmsApiClient = new WebSmsApiClient(/* you connection handler */);
- Or if you need to adjust something on the
HttpClientyou can prepare it with the options:var httpClient = new HttpClient().ApplyWebSmsApiOptions(/* your options */); /* Adjust the http client as needed */ var webSmsApiClient = new WebSmsApiClient(httpClient);
- Register the client
builder.Services.AddWebSmsApiClient(options => { options.BaseUrl = "https://api.linkmobility.eu/"; options.AuthenticationType = AuthenticationType.Bearer; options.AccessToken = "YOUR ACCESS_TOKEN"; });
- Then inject
IWebSmsApiClientit into your services.public YourService(IWebSmsApiClient webSmsApiClient) { // your code }
Call the client with wither SendTextMessage or SendBinaryMessage.
webSmsApiClient.Messaging.SendTextMessage(new()
{
RecipientAddressList =
[
"YOUR recipient's MSISDN"
],
MessageContent = "hi there! this is a test message."
});Use WebSmsWebhook helper class to parse the webhook request.
There are two options, both returning the base type which can be checked manually or matched with the extension method as follows.
Match supports simple actions and functions with a return value as well.
- Parse json string
var result = WebSmsWebhook.Parse(json).Match( onText: _ => /* your code */, onBinary: _ => /* your code */, onDeliveryReport: _ => /* your code */);
- Or parse http request body
var result = (await WebSmsWebhook.Parse(Request.Body)).Match( onText: _ => /* your code */, onBinary: _ => /* your code */, onDeliveryReport: _ => /* your code */);
You can implement a custom websms API connection handler by deriving from WebSmsApiConnectionHandler.
By doing this you can either decorate or intercept the Post method with the provided virtual properties i.e. for auditing or other use cases.
public class CustomWebSmsApiConnectionHandler(HttpClient httpClient) : WebSmsApiConnectionHandler(httpClient)
{
protected override JsonSerializerOptions SerializerOptions => /* Your custom serializer options, or use and manipulate from base */;
protected override Func<string, object, CancellationToken, Task> OnBeforePost =>
(endpoint, data, cancellationToken) => /* Your function */;
protected override Func<HttpResponseMessage, CancellationToken, Task<HttpResponseMessage>> OnResponseReceived =>
(response, cancellationToken) => /* Your function (must return the http response message) */;
protected override Action<HttpResponseMessage> EnsureSuccess =>
(response) => /* Your custom validation */;
public override async Task<T> Post<T>(string endpoint, object data, [Optional] CancellationToken cancellationToken)
{
/* Do something before */
// Call the base function
var response = await base.Post<T>(endpoint, data, cancellationToken);
/* Do something after */
return response;
}
}