The optional-capability pattern had media and delete but no interactive UX: a channel could ask a human to approve an action through ApprovalRequester, but there was no domain contract for posing a clarifying question or presenting a single-choice picker, and no capability flag a caller could read before attempting one. Add ClarifyRequester and PickerRequester to internal/domain/messaging alongside ApprovalRequester, with Clarify/Picker/Approval flags on AdapterCapabilities and ClarifierOf/PickerOf/ApproverOf lookups that treat the capability as the contract rather than the method set, so a mis-reporting adapter degrades instead of panicking. TextFallback is the shared text degrade: a channel with no native question or selection UI still carries the interaction (numbered prompt, parsed reply) instead of failing or silently dropping it. FormatClarifyText/FormatPickerText/ParsePickerReply hold the rendering and matching logic once. The approval half reuses the existing decision model: Telegram's telegramApprover already renders a native prompt, so it now reports the Approval capability and deliberately does not claim Clarify/Picker it has no native UI for. Gateway re-exports the contract. Red-first: internal/domain/messaging/interactive_test.go drives the test double adapters (native, degrade, mis-reporting, over-claiming) and the text rendering/parsing; telegram/approval_test.go pins the honest capability report.
59 lines
2.0 KiB
Go
59 lines
2.0 KiB
Go
package messaging
|
|
|
|
import "context"
|
|
|
|
// SendResult captures the outcome of a platform media delivery.
|
|
type SendResult struct {
|
|
MessageID string
|
|
Success bool
|
|
Retryable bool
|
|
Error error
|
|
ErrorCode string
|
|
}
|
|
|
|
// AdapterCapabilities reports which optional delivery behaviors a sender
|
|
// implements.
|
|
type AdapterCapabilities struct {
|
|
Media bool
|
|
// Delete reports whether the sender can remove a message it
|
|
// previously sent (MessageDeleter), which is what lets an
|
|
// EphemeralReply be retracted once its TTL elapses. A sender that
|
|
// cannot delete still delivers ephemeral replies, it just leaves them
|
|
// in place.
|
|
Delete bool
|
|
// Clarify reports whether the sender can pose a clarifying question and
|
|
// carry the human's answer back (ClarifyRequester). The capability is
|
|
// about carrying the interaction, not about native widgets: an adapter
|
|
// with no button UI fulfils it through the text fallback rather than
|
|
// reporting false and stranding the question.
|
|
Clarify bool
|
|
// Picker reports whether the sender can present a single-choice
|
|
// selection and carry the chosen option back (PickerRequester), on the
|
|
// same native-or-fallback rule as Clarify.
|
|
Picker bool
|
|
// Approval reports whether the sender renders approval prompts through
|
|
// the ApprovalRequester contract, so a caller can know a sender can
|
|
// present an approve/deny prompt before a gated action needs one.
|
|
Approval bool
|
|
}
|
|
|
|
// MediaSender delivers a MessageEvent carrying MediaAttachments through a
|
|
// platform-specific media API.
|
|
type MediaSender interface {
|
|
SendMedia(ctx context.Context, event MessageEvent) (SendResult, error)
|
|
}
|
|
|
|
// CapabilityReporter is the optional interface a sender implements to
|
|
// self-report which optional behaviors it supports.
|
|
type CapabilityReporter interface {
|
|
Capabilities() AdapterCapabilities
|
|
}
|
|
|
|
// CapabilitiesOf reports which optional behaviors sender supports.
|
|
func CapabilitiesOf(sender any) AdapterCapabilities {
|
|
if reporter, ok := sender.(CapabilityReporter); ok {
|
|
return reporter.Capabilities()
|
|
}
|
|
return AdapterCapabilities{}
|
|
}
|