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:
| Property | Type | Description |
|---|---|---|
WebhookType | string? | Webhook.ReceivingSms, Webhook.RentSms, or null. |
OperationId | long? | tzid of the operation or rent. |
UserId | long? | Account id. |
CountryCode | int? | Country code of the number. |
Number | string? | Numbers: + and the full number (+79001234567), or hidden. Rent: the full number in digits without + (79001234567). |
Sender | string? | Sender of the SMS, e.g. VK. |
Message | string? | Full SMS text. |
Code | string? | Code extracted from the SMS; "" when none was found. |
TimeStart | string? | Numbers: when the operation was ordered. Rent: when the SMS arrived. YYYY-MM-DD HH:MM:SS. |
TimeLeft | long? | Time left: seconds for numbers, minutes for rent. |