Documentation ¶
Overview ¶
Package handlers adds typed events, handler registration and dispatching on top of `go.mau.fi/whatsmeow`.
Index ¶
Constants ¶
const ( NoHandlerFound dispatchErrorType HandlerFailed UnknownEvent )
Variables ¶
This section is empty.
Functions ¶
func Register ¶
func Register(t EventType, h handler)
Register registers a handler for an event type. The handler must expose a method
Handle(evt interface{}) error
The passed-in event to the handler method is an opaque pointer to one of the types of `go.mau.fi/whatsmeow/types/events`. The handler must convert it to the true event using a typecast. For example, a handler for the type `Message` would convert as follows:
import "go.mau.fi/whatsmeow/types/events" func (h *myHandler) Handle(ev interface{}) error { messageEvent := ev.(*events.Message) ... }
More than one handlers may be registered for an event. Upon encountering the event, the handlers will be called in-order.
Register(Message, h1) Register(Message, h2) // When a `Message` is seen, first `h1.Handle(ev)` is invoked, then `h2.Handle(ev)`.
Types ¶
type DispatchError ¶
type DispatchError struct { Type dispatchErrorType Err error }
DispatchError enriches the error returned by Dispatch with an error reason, which may be `NoHandlerFound` or `HandlerFailed`. Example:
if err := Dispatch(e); err != nil { if err.Type == NoHandlerFound { // Log but ignore that there is no handler for this event log.Println(err) } else { // A handler ran, but failed log.Fatalln(err) } }
func Dispatch ¶
func Dispatch(evt interface{}) *DispatchError
Dispatch invokes registered handlers for any `EventType`. There is a `nil` error return IFF: - One or more handlers for the event type were registered, - They all executed without returning an error.
The absence of registered handlers is returned with `err.Type == NoHandlerFound` and `err.Error()` stating the event type and payload.
The failure of a registered handler is returned with `err.Type` == HandlerFailed`, `err.Err` being the underlying error, and `err.Error()` stating the handler's error. Invoking handlers stops when a handler returns an error; i.e., a second handler may not run if the first handler fails.
When `Dispatch()` returns `err.Type == UnknownEvent` then the event couldn't be mapped to an existing type. Probably the code of this module is wrong or `go.mau.fi/whatsmeow/types/events` has a new type that `Dispatch()` is not yet aware of.
func (*DispatchError) Error ¶
func (d *DispatchError) Error() string
type EventType ¶
type EventType int
EventType is an enum for whatsmeow events.
const ( AppState EventType AppStateSyncComplete Archive BusinessName CallAccept CallOffer CallOfferNotice CallRelayLatency CallTerminate ChatPresence ClientOutdated Connected ConnectFailure Contact DeleteChat DeleteForMe Disconnected GroupInfo HistorySync IdentityChange JoinedGroup KeepAliveRestored KeepAliveTimeout LoggedOut MarkChatAsRead MediaRetry Message Mute OfflineSyncCompleted OfflineSyncPreview PairError PairSuccess Picture Pin Presence PrivacySettings PushName PushNameSetting QR QRScannedWithoutMultidevice Receipt Star StreamError StreamReplaced TemporaryBan UnarchiveChatSetting UndecryptableMessage UnknownCallEvent )