Webhooks

Receive SMS as HTTP callbacks and parse them with 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 JSON text (str or bytes) or an already parsed dict.

def parse_webhook(payload: str | bytes | Mapping[str, Any]) -> WebhookPayload

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

from http.server import BaseHTTPRequestHandler, HTTPServer

from onlinesimru import parse_webhook


class Hook(BaseHTTPRequestHandler):
    def do_POST(self) -> None:
        body = self.rfile.read(int(self.headers["Content-Length"]))
        hook = parse_webhook(body)
        if hook["webhook_type"] == "receiving_sms":
            print(hook["operation_id"], hook["code"])
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b"ok")


HTTPServer(("", 8080), Hook).serve_forever()

Payload fields

FieldTypeDescription
webhook_type"receiving_sms" | "rent_sms" | NoneKind of operation.
operation_idint | Nonetzid of the operation or rent.
user_idint | NoneAccount id.
country_codeint | NoneCountry code of the number.
numberstr | NoneNumbers: + and the full number (+79001234567), or hidden. Rent: the full number in digits without + (79001234567).
senderstr | NoneSender of the SMS, e.g. VK.
messagestr | NoneFull SMS text.
codestr | NoneCode extracted from the SMS; "" when none was found.
time_startstr | NoneNumbers: when the operation was ordered. Rent: when the SMS arrived. YYYY-MM-DD HH:MM:SS.
time_leftint | NoneTime left: seconds for numbers, minutes for rent.