Skip to navigation

Refuse

View as MarkdownOpen in Claude

Raise Refuse from an operation’s prepare(), a lookup, an identify() find function, or a transfer function when the request breaks a rule. Nothing changes. The model is told why, and what can be done instead.

from signalwire.operations import Refuse

Parameters

why
strRequired

What is true now: that nothing was changed, and why.

then
str | NoneDefaults to None

What to do next. Defaults to telling the caller and asking how they’d like to go on.

keep
boolDefaults to False

Keyword-only. True keeps the refusal in the model’s view however long it is, for a refusal that lists choices the caller picks from later.

Example

from signalwire.operations import Change, Field, Operation, Refuse
ORDERS = {"#W1": "delivered"}
class Cancel(Operation):
"""Cancel a pending order."""
order_id = Field(str, "The order to cancel.")
def prepare(self, call, f):
if ORDERS.get(f.order_id) != "pending":
raise Refuse(
f"{f.order_id} can't be cancelled: only pending orders can.",
"Offer a return once it's delivered.",
)
return Change(read_back=f"Cancel order {f.order_id}. Shall I?", touches=[f"order:{f.order_id}"])