Webhooks

Receive SMS as HTTP callbacks and parse them with parseWebhook.

Set a URL with client.user.setWebhookURL. 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.webhookLogs.

parseWebhook

Parse an inbound webhook body from the raw request body or a JSON string.

func parseWebhook(_ payload: Data) throws -> WebhookPayload
func parseWebhook(_ payload: String) throws -> WebhookPayload

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

Call it from the request handler of your HTTP server (Vapor, Hummingbird, …) with the body bytes:

import Foundation
import OnlineSim

func handleOnlineSimHook(body: Data) throws {
    let hook = try parseWebhook(body)
    if hook.webhookType == .receivingSms, let tzid = hook.operationID {
        print(tzid, hook.code ?? "no code")
    }
}

Payload fields

FieldJSON keyTypeDescription
webhookTypewebhook_typeWebhookType?.receivingSms or .rentSms.
operationIDoperation_idInt?tzid of the operation or rent.
userIDuser_idInt?Account id.
countryCodecountry_codeInt?Country code of the number.
numbernumberString?Numbers: + and the full number (+79001234567), or hidden. Rent: the full number in digits without + (79001234567).
sendersenderString?Sender of the SMS, e.g. VK.
messagemessageString?Full SMS text.
codecodeString?Code extracted from the SMS; "" when none was found.
timeStarttime_startString?Numbers: when the operation was ordered. Rent: when the SMS arrived. YYYY-MM-DD HH:MM:SS.
timeLefttime_leftInt?Time left: seconds for numbers, minutes for rent.