Karta.Messaging.Plugs.Bird.Events (Karta v1.0.0)

Bird's SMS events, read for Karta (bird.com/docs/guides/sms/events). The body is {"type", "timestamp", "data"}; the event's identity is the webhook-id header (the same on every retry). Delivery is unordered and at least once.

  • sms.accepted, sms.sent -> the message was sent;
  • sms.delivered -> delivered (with carrier, mcc_mnc);
  • sms.undelivered -> a temporary non-delivery: noted, the message stays sent (the carrier may still deliver);
  • sms.failed, sms.rejected, sms.expired -> failed, permanent (data.error: code, description); sms.rejected with insufficient_balance pauses the connector;
  • sms.received -> an inbound text: data.from, the text, the operator.

data.sms_id names the message; data.cost is amount, currency_code, transaction_amount, passthrough_amount, merged per component by the latest event time (merge_cost/3). Field names for sms.received's text are not in the documentation: text, body and content.text are read.

Summary

Functions

Merges the cost an event carries into the stored one, per component: a component is replaced only by a value from an event no older than the one it came from (stamps keeps each component's event time). Amounts are decimal text ("0.0455"), added as Decimals: no float touches money. Returns %{"amount", "currency", "transaction_amount", "passthrough_amount", "stamps"}; amount is the sum of the components known so far (the charge so far, not a settled total).

Money as text: Decimal, never a float. A number from Bird's JSON (parsed as a Decimal) or an integer becomes its plain decimal text; anything else is nil.

Verifies the request (Standard Webhooks, Karta.Webhooks.Signature.verify/4, five minutes of tolerance) and reads it. {:error, :bad_signature}, {:error, :ignored} (a type Karta does not use, or no body worth keeping) or {:ok, [event]}.

Functions

merge_cost(stored, incoming, at)

@spec merge_cost(map() | nil, map() | nil, DateTime.t()) :: map() | nil

Merges the cost an event carries into the stored one, per component: a component is replaced only by a value from an event no older than the one it came from (stamps keeps each component's event time). Amounts are decimal text ("0.0455"), added as Decimals: no float touches money. Returns %{"amount", "currency", "transaction_amount", "passthrough_amount", "stamps"}; amount is the sum of the components known so far (the charge so far, not a settled total).

money(amount)

@spec money(term()) :: String.t() | nil

Money as text: Decimal, never a float. A number from Bird's JSON (parsed as a Decimal) or an integer becomes its plain decimal text; anything else is nil.

parse(secret, headers, raw, opts \\ [])

@spec parse(String.t() | nil, [{String.t(), String.t()}], binary(), keyword()) ::
  {:ok, [map()]} | {:error, :bad_signature | :ignored}

Verifies the request (Standard Webhooks, Karta.Webhooks.Signature.verify/4, five minutes of tolerance) and reads it. {:error, :bad_signature}, {:error, :ignored} (a type Karta does not use, or no body worth keeping) or {:ok, [event]}.