Skip to content

kit230986/gatewayapi-dotnet

 
 

Repository files navigation

📧 GatewayAPI .NET

GatewayAPI .NET is a wrapper for the GatewayApi RestAPI. For full description of the GatewayAPI see: https://gatewayapi.com/docs/

🔧 Installation

Using the .NET Core command-line interface (CLI) tools:

$ dotnet add package GatewayAPI

or with the Package Manager Console:

$ Install-Package GatewayAPI

👷‍♀️ Usage

The API wrapper is build with the mindset of been simple to use. The package utilities fluent princip.

For further documentation visit the /Sample folder

Sending a SMS

// Construct handler
GatewayAPIHandler handler = new GatewayAPIHandler("apiToken");

// Create message
SMSMessage message = new SMSMessage()
                .SetMessage("Hello World")                
                .SetRecipient(new SMSRecipient(4512345678));

// Send message
try
{
    Result result = gatewayAPIHandler.SendMessage(message);
    Console.WriteLine(result.Ids); // Returns a list of ids .
} catch (UnauthorizedException e)
{
    Console.WriteLine(e.Message);
}
catch (ServerException e)
{
    Console.WriteLine(e.Message);
}
catch (MessageException e)
{
    Console.WriteLine(e.Message);
} catch (Exception e)
{
    Console.WriteLine(e.Message);
}


// You can send multiple message with the following function
Result result = gatewayAPIHandler.SendMessage(message);
gatewayAPIHandler.SendMessages(new List<SMSMessage> {
                    message,
                    message2
                });

Get Account balance

Get the current account balance from GatewayAPI

GatewayAPIHandler gatewayAPIHandler = new GatewayAPIHandler("apiToken");            
AccountBalance accountBalance = gatewayAPIHandler.GetAccountBalance();

Get Prices

Fetch current prices from GatewayAPI.

GatewayAPIHandler gatewayAPIHandler = new GatewayAPIHandler("apiToken");            
Prices prices = gatewayAPIHandler.GetPricesAsJson();

Cancel a scheduled SMS

You can cancel a scheduled message from the returned ID, when sending the message

GatewayAPIHandler gatewayAPIHandler = new GatewayAPIHandler("apiToken");            

// Construct a schduled message
DateTime dt = DateTime.Now;
SMSMessage message = new SMSMessage()
                        .SetMessage("Tester")
                        .SetSender("Test")
                        .SetSendTime(dt.AddHours(1))                                    
                        .SetRecipient(new SMSRecipient(4512345678));

// Send message
Result result = gatewayAPIHandler.SendMessage(message);

// Cancel the first message
CancelResult cancelResult = gatewayAPIHandler.CancelScheduledMessage(result.Ids[0]);
string result = cancelResult.Status // (Can either return failed or success)

Parsing webhooks

You can easily parse webhooks sent from GatewayAPI to your server using the Webhook class.

[HttpPost]
public IActionResult Catch()
{
    var webhookHandler = new GatewayAPIWebhookHandler("secret");
    var webhook = webhookHandler.ParseFromRequest(Request);

    if (webhook is WebhookDeliveryStatus)
    {
        WebhookDeliveryStatus test = (WebhookDeliveryStatus)webhook;
        Console.WriteLine(test.status);
    } else if (webhook is WebhookIncomingMessage) {
        WebhookIncomingMessage test = (WebhookIncomingMessage)webhook;
        Console.WriteLine(test.message);
    }

    return Ok();
}

All full example can be seen in "/samples/Webhook"

📎 Contact

If you find and issue, please create a issue. You can at any time reach out to me on twitter

About

.NET wrapper for the GatewayAPI.com

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%