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.

func ParseWebhook(data []byte) (WebhookPayload, error)

Lenient: numeric fields may arrive as strings, missing or malformed fields become zero values ("", 0), unknown fields are ignored. It returns an error only when the input is not a JSON object.

package main

import (
    "io"
    "log"
    "net/http"

    onlinesim "github.com/s00d/onlinesim-go-api/v3"
)

func main() {
    http.HandleFunc("/onlinesim-hook", func(w http.ResponseWriter, r *http.Request) {
        body, err := io.ReadAll(r.Body)
        if err != nil {
            http.Error(w, err.Error(), http.StatusBadRequest)
            return
        }
        hook, err := onlinesim.ParseWebhook(body)
        if err != nil {
            http.Error(w, err.Error(), http.StatusBadRequest)
            return
        }
        if hook.WebhookType == onlinesim.WebhookReceivingSMS {
            log.Println(hook.OperationID, hook.Code)
        }
        _, _ = io.WriteString(w, "ok")
    })
    log.Fatal(http.ListenAndServe(":8080", nil))
}

Payload fields

FieldJSONTypeDescription
WebhookTypewebhook_typeWebhookTypeWebhookReceivingSMS, WebhookRentSMS, or "" for anything else.
OperationIDoperation_idint64tzid of the operation or rent.
UserIDuser_idint64Account id.
CountryCodecountry_codeintCountry code of the number.
NumbernumberstringNumbers: + and the full number (+79001234567), or hidden. Rent: the full number in digits without + (79001234567).
SendersenderstringSender of the SMS, e.g. VK.
MessagemessagestringFull SMS text.
CodecodestringCode extracted from the SMS; "" when none was found.
TimeStarttime_startstringNumbers: when the operation was ordered. Rent: when the SMS arrived. YYYY-MM-DD HH:MM:SS.
TimeLefttime_leftint64Time left: seconds for numbers, minutes for rent.