Webhooks

Receive SMS as HTTP callbacks and parse them with Webhook.Parse.

Set a URL with client.User.SetWebhookUrlAsync. For each SMS the server POSTs a JSON body to it: webhook_type receiving_sms for numbers, rent_sms for rents. Delivery attempts are listed by client.User.WebhookLogsAsync.

Webhook.Parse

Parse an inbound webhook body from a JSON string or an already parsed JsonElement.

static WebhookPayload Parse(string payload)
static WebhookPayload Parse(JsonElement payload)

Lenient: numeric fields may arrive as strings, missing or malformed fields become null, unknown fields are dropped. It throws OnlineSimException only when the input is not a JSON object.

Any web framework works: pass it the raw request body. With the BCL's HttpListener (also on .NET Framework):

using System.IO;
using System.Net;
using OnlineSim.Api;

var listener = new HttpListener();
listener.Prefixes.Add("http://+:8080/onlinesim-hook/");
listener.Start();
while (true)
{
    var context = await listener.GetContextAsync();
    using (var reader = new StreamReader(context.Request.InputStream))
    {
        var hook = Webhook.Parse(await reader.ReadToEndAsync());
        if (hook.WebhookType == Webhook.ReceivingSms) Console.WriteLine($"{hook.OperationId} {hook.Code}");
    }
    context.Response.Close();
}

Payload fields

WebhookPayload:

PropertyTypeDescription
WebhookTypestring?Webhook.ReceivingSms, Webhook.RentSms, or null.
OperationIdlong?tzid of the operation or rent.
UserIdlong?Account id.
CountryCodeint?Country code of the number.
Numberstring?Numbers: + and the full number (+79001234567), or hidden. Rent: the full number in digits without + (79001234567).
Senderstring?Sender of the SMS, e.g. VK.
Messagestring?Full SMS text.
Codestring?Code extracted from the SMS; "" when none was found.
TimeStartstring?Numbers: when the operation was ordered. Rent: when the SMS arrived. YYYY-MM-DD HH:MM:SS.
TimeLeftlong?Time left: seconds for numbers, minutes for rent.