Webhooks

Receive SMS as HTTP callbacks and parse them with OnlineSim.parse_webhook.

Set a URL with client.user.set_webhook_url. 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.webhook_logs.

parse_webhook

Parse an inbound webhook body from a JSON string or an already parsed Hash (string or symbol keys).

OnlineSim.parse_webhook(payload) # => Hash

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

require "onlinesim"

# Rack app (config.ru): run WebhookApp
WebhookApp = lambda do |env|
  hook = OnlineSim.parse_webhook(env["rack.input"].read)
  puts "#{hook['operation_id']} #{hook['code']}" if hook["webhook_type"] == "receiving_sms"
  [200, { "content-type" => "text/plain" }, ["ok"]]
end

Payload fields

FieldTypeDescription
webhook_type"receiving_sms", "rent_sms", nilKind of operation.
operation_idInteger, niltzid of the operation or rent.
user_idInteger, nilAccount id.
country_codeInteger, nilCountry code of the number.
numberString, nilNumbers: + and the full number (+79001234567), or hidden. Rent: the full number in digits without + (79001234567).
senderString, nilSender of the SMS, e.g. VK.
messageString, nilFull SMS text.
codeString, nilCode extracted from the SMS; "" when none was found.
time_startString, nilNumbers: when the operation was ordered. Rent: when the SMS arrived. YYYY-MM-DD HH:MM:SS.
time_leftInteger, nilTime left: seconds for numbers, minutes for rent.