Help us shape the future of Go! Take the 2026 Go Developer Survey

engine

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package engine is the WhatsApp engine without the protocol library: the client boundary, plain-data events, the supervisor, the durable ingest pipeline and the history worker.

Index

Constants

View Source
const (
	DefaultHistoryMaxBytes   = 32 << 20
	MaxHistoryMaxBytes       = 256 << 20
	DefaultMediaRefRetention = 30 * 24 * time.Hour
)
View Source
const (
	VariantGIF       = "gif"
	VariantVideoNote = "video_note"
	VariantVoiceNote = "voice_note"
	VariantAnimated  = "animated"
	VariantLottie    = "lottie"
)
View Source
const (
	MaxRecentStarts = 5
)
View Source
const SyncOnDemand = "ON_DEMAND"

Variables

View Source
var (
	ErrMediaUnavailable = errors.New("engine: the attachment is not available")
	ErrMediaExpired     = errors.New("engine: the attachment has expired")
	ErrMediaTooLarge    = errors.New("engine: the attachment is larger than allowed")
	ErrMediaFailed      = errors.New("engine: fetching the attachment failed")
	ErrMediaThrottled   = errors.New("engine: attachment fetches are throttled")
)
View Source
var (
	ErrNotConnected         = errors.New("engine: not connected, nothing was sent")
	ErrRecipientUnavailable = errors.New("engine: the recipient cannot be reached from this account")
	ErrRejected             = errors.New("engine: WhatsApp rejected the message")
	ErrOutcomeUnknown       = errors.New("engine: whether WhatsApp received the message is unknown")
	ErrSendBusy             = errors.New("engine: another send is in progress")
)
View Source
var (
	ErrAlreadyPaired     = errors.New("engine: a device is already paired")
	ErrOwnerPhoneMissing = errors.New("engine: the owner's phone number is not configured")
	ErrPairRateLimited   = errors.New("engine: three pairing attempts were made in the last hour")
	ErrPairFailed        = errors.New("engine: pairing failed")
	ErrNotPaired         = errors.New("engine: no device is paired")
	ErrOwnerMismatch     = errors.New("engine: the stored device belongs to another number than the owner's")
	ErrAlreadyConnected  = errors.New("engine: already connected")
	ErrStopped           = errors.New("engine: stopped")
)

Functions

func RejectionCode added in v1.0.0

func RejectionCode(err error) int

func RetryAfter added in v1.2.0

func RetryAfter(err error) time.Duration

Types

type BackfillRequest added in v1.0.0

type BackfillRequest struct {
	Chat         string
	AnchorID     string
	AnchorFromMe bool
	AnchorTime   time.Time
	Count        int
}

type CATRefreshFailed

type CATRefreshFailed struct{}

type Canonicaliser added in v1.2.0

type Canonicaliser interface {
	Canonical(ctx context.Context, c policy.CanonicalChat) (policy.CanonicalChat, error)
}

type Client

type Client interface {
	Connect(ctx context.Context) error
	Disconnect()
	Paired() bool
	Account() string
	PairPhone(ctx context.Context, digits string) (code string, err error)
	Logout(ctx context.Context) error
	Version() Version
	SetVersion(Version)
	DownloadHistory(ctx context.Context, ref HistoryRef, dst io.Writer) error
	AckHistory(ctx context.Context, ref HistoryRef) error
	OnEvent(handler func(Event) bool) (cancel func())
	NewMessageID() string
	Send(ctx context.Context, req SendRequest) (Sent, error)
	Backfill(ctx context.Context, req BackfillRequest) (requestID string, err error)
	FetchMedia(ctx context.Context, req MediaRequest) (Staged, error)
}

type ClientOutdated

type ClientOutdated struct{}

type Clock

type Clock interface {
	Now() time.Time
	After(d time.Duration) <-chan time.Time
}

type ConnectFailure

type ConnectFailure struct{ Code int }

type Connected

type Connected struct{}

type Contact

type Contact struct {
	User     string
	PushName string
}

type ContactName added in v0.3.0

type ContactName struct {
	User      string `json:"user"`
	FullName  string `json:"full_name,omitempty"`
	FirstName string `json:"first_name,omitempty"`
}

type Conversation

type Conversation struct {
	Chat     string
	Subject  string
	Members  []Participant
	Messages []Message
}

type Disconnected

type Disconnected struct{}

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

func New

func New(opts Options) (*Engine, error)

func (*Engine) Backfill added in v1.0.0

func (e *Engine) Backfill(ctx context.Context, req BackfillRequest) (string, error)

func (*Engine) FetchMedia added in v1.2.0

func (e *Engine) FetchMedia(ctx context.Context, req MediaRequest) (Staged, error)

func (*Engine) NewMessageID added in v1.0.0

func (e *Engine) NewMessageID() string

func (*Engine) Pair

func (e *Engine) Pair(ctx context.Context) (string, error)

func (*Engine) Ready added in v1.0.0

func (e *Engine) Ready() bool

func (*Engine) Reconnect

func (e *Engine) Reconnect() error

func (*Engine) Send added in v1.0.0

func (e *Engine) Send(ctx context.Context, req SendRequest) (Sent, error)

func (*Engine) Start

func (e *Engine) Start(ctx context.Context, recentStarts int)

func (*Engine) Status

func (e *Engine) Status() Status

func (*Engine) Stop

func (e *Engine) Stop(ctx context.Context) error

type Event

type Event interface {
	// contains filtered or unexported methods
}

type Group

type Group struct {
	Chat      string        `json:"chat"`
	Subject   string        `json:"subject,omitempty"`
	Members   []Participant `json:"members,omitempty"`
	Joined    []Participant `json:"joined,omitempty"`
	Left      []string      `json:"left,omitempty"`
	Timestamp time.Time     `json:"ts"`
}

type History

type History struct {
	Conversations []Conversation
	LIDMappings   []LIDMapping
	Contacts      []Contact
}

type HistoryDecoder

type HistoryDecoder interface {
	Decode(blob []byte) (History, error)
}

type HistoryNotification

type HistoryNotification struct {
	Sender string
	FromMe bool
	Ref    HistoryRef
}

type HistoryRef

type HistoryRef struct {
	ID            string `json:"id"`
	DirectPath    string `json:"direct_path,omitempty"`
	MediaKey      []byte `json:"media_key,omitempty"`
	FileSHA256    []byte `json:"file_sha256,omitempty"`
	FileEncSHA256 []byte `json:"file_enc_sha256,omitempty"`
	FileLength    uint64 `json:"file_length,omitempty"`
	Inline        []byte `json:"inline,omitempty"`
	SyncType      string `json:"sync_type,omitempty"`
	RequestID     string `json:"request_id,omitempty"`
}

type Identity added in v1.0.0

type Identity struct {
	PN  string
	LID string
}

type Key

type Key struct {
	RemoteJID   string `json:"remote_jid,omitempty"`
	FromMe      bool   `json:"from_me,omitempty"`
	ID          string `json:"id"`
	Participant string `json:"participant,omitempty"`
}

type Kind

type Kind string
const (
	KindText       Kind = "text"
	KindMedia      Kind = "media"
	KindReaction   Kind = "reaction"
	KindPollUpdate Kind = "poll_update"
	KindEdit       Kind = "edit"
	KindRevoke     Kind = "revoke"
	KindOther      Kind = "other"
)

type LIDMapping

type LIDMapping struct {
	PN  string
	LID string
}

type LoggedOut

type LoggedOut struct{}

type MediaKind added in v1.2.0

type MediaKind string
const (
	MediaImage    MediaKind = "image"
	MediaVideo    MediaKind = "video"
	MediaAudio    MediaKind = "audio"
	MediaDocument MediaKind = "document"
	MediaSticker  MediaKind = "sticker"
)

type MediaRequest added in v1.2.0

type MediaRequest struct {
	ID        string
	Ref       string
	Kind      MediaKind
	Declared  int64
	Deadline  time.Time
	Chat      policy.CanonicalChat
	Sender    policy.CanonicalChat
	Canonical Canonicaliser
}

type Message

type Message struct {
	Chat         string        `json:"chat"`
	ID           string        `json:"id"`
	Sender       string        `json:"sender"`
	SenderAlt    string        `json:"sender_alt,omitempty"`
	RecipientAlt string        `json:"recipient_alt,omitempty"`
	Addressing   string        `json:"addressing,omitempty"`
	FromMe       bool          `json:"from_me,omitempty"`
	Timestamp    time.Time     `json:"ts"`
	PushName     string        `json:"push_name,omitempty"`
	Kind         Kind          `json:"kind,omitempty"`
	Text         string        `json:"text,omitempty"`
	MediaType    string        `json:"media_type,omitempty"`
	MediaRef     string        `json:"media_ref,omitempty"`
	MediaKind    MediaKind     `json:"media_kind,omitempty"`
	MediaVariant string        `json:"media_variant,omitempty"`
	MediaBytes   int64         `json:"media_bytes,omitempty"`
	Target       *Key          `json:"target,omitempty"`
	Reply        *Reply        `json:"reply,omitempty"`
	Expiration   time.Duration `json:"expiration,omitempty"`
}

type Notifier

type Notifier interface {
	Unpaired()
	Disconnected(reason string)
	PairRejected(stage string)
	LogoutFailed(attempt int, errorType string)
	Quarantine(queue string, attempts int)
	RekeyConflict(conflict string)
	IngestPaused(freeBytes, floorBytes uint64)
}

type Options

type Options struct {
	Client            Client
	Versions          VersionSource
	Decoder           HistoryDecoder
	Archive           *ingest.Store
	DataDir           string
	OwnerPhone        string
	HistoryMaxBytes   int64
	MediaOn           bool
	MediaRefRetention time.Duration
	Logger            *slog.Logger
	Notify            Notifier
	Metrics           *metrics.Registry
	Clock             Clock
	Jitter            func(time.Duration) time.Duration
}

type PairRejected

type PairRejected struct{}

type Paired

type Paired struct{ JID string }

type Participant

type Participant struct {
	User  string `json:"user"`
	Admin bool   `json:"admin,omitempty"`
}

type Reason

type Reason string
const (
	ReasonOutdated       Reason = "outdated"
	ReasonReplaced       Reason = "replaced"
	ReasonLoggedOut      Reason = "logged_out"
	ReasonTemporaryBan   Reason = "temporary_ban"
	ReasonCATRefresh     Reason = "cat_refresh"
	ReasonConnectFailure Reason = "connect_failure"
	ReasonRestartBudget  Reason = "restart_budget"
	ReasonOwnerMismatch  Reason = "owner_mismatch"
	ReasonShutdown       Reason = "shutdown"
)

type RejectedError added in v1.0.0

type RejectedError struct{ Code int }

func (*RejectedError) Error added in v1.0.0

func (e *RejectedError) Error() string

func (*RejectedError) Unwrap added in v1.0.0

func (e *RejectedError) Unwrap() error

type Reply

type Reply struct {
	ID          string `json:"id"`
	Participant string `json:"participant,omitempty"`
	RemoteJID   string `json:"remote_jid,omitempty"`
	Text        string `json:"text,omitempty"`
}

type SendQuote added in v1.0.0

type SendQuote struct {
	ID          string
	Participant string
	Text        string
}

type SendRequest added in v1.0.0

type SendRequest struct {
	ID         string
	Chat       string
	Text       string
	Quote      *SendQuote
	Expiration time.Duration
}

type Sent added in v1.0.0

type Sent struct {
	ID        string
	Chat      string
	Sender    string
	Timestamp time.Time
}

type Staged added in v1.2.0

type Staged interface {
	Size() int64
	ReadAt(p []byte, off int64) (int, error)
	WriteAt(p []byte, off int64) (int, error)
	Truncate(size int64) error
	Erase()
}

type State

type State string
const (
	StateUnpaired     State = "unpaired"
	StateConnecting   State = "connecting"
	StateConnected    State = "connected"
	StateDisconnected State = "disconnected"
)

type Status

type Status struct {
	State  State
	Reason Reason
	Paired bool
}

type StreamReplaced

type StreamReplaced struct{}

type TemporaryBan

type TemporaryBan struct{ Expire time.Duration }

type ThrottledError added in v1.2.0

type ThrottledError struct{ RetryAfter time.Duration }

func (*ThrottledError) Error added in v1.2.0

func (e *ThrottledError) Error() string

func (*ThrottledError) Unwrap added in v1.2.0

func (e *ThrottledError) Unwrap() error

type Version

type Version [3]uint32

func (Version) IsZero

func (v Version) IsZero() bool

func (Version) Less

func (v Version) Less(o Version) bool

type VersionSource

type VersionSource interface {
	Latest(ctx context.Context) (Version, error)
}

Directories

Path Synopsis
wa
Package wa is the only user of the WhatsApp protocol library: it implements the engine's client boundary over a whatsmeow client, translates the library's events into the engine's plain data, guards pairing and caps downloads.
Package wa is the only user of the WhatsApp protocol library: it implements the engine's client boundary over a whatsmeow client, translates the library's events into the engine's plain data, guards pairing and caps downloads.
internal/mediasrc
Package mediasrc declares the attachment source the adapter's media download takes: only Wrap puts a message in one, and the zero value carries none.
Package mediasrc declares the attachment source the adapter's media download takes: only Wrap puts a message in one, and the zero value carries none.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL