maxclient

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CallsAPIURL              = "https://calls.okcdn.ru/fb.do"
	CallsAppKey              = "CNHIJPLGDIHBABABA"
	CallsClientType          = "SDK_JS"
	CallsClientVersion       = "1.1"
	CallsProtocolVersion     = "5"
	CallsCapabilities        = "603F"
	CallsDeviceType          = "browser"
	CallsPlatform            = "WEB"
	CallsClientTypeSignaling = "ONE_ME"
)

Call API constants

View Source
const (
	WSURL      = "wss://ws-api.oneme.ru/websocket"
	RPCVersion = 11
	AppVersion = "26.2.2"

	DefaultUserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36"
	DefaultOrigin    = "https://web.max.ru"
)
View Source
const (
	OpcodeKeepalive         = 1
	OpcodeHello             = 6
	OpcodeChangeProfile     = 16
	OpcodeSendCode          = 17
	OpcodeSignIn            = 18
	OpcodeLoginByToken      = 19
	OpcodeSetSettings       = 22
	OpcodeResolveUsers      = 32
	OpcodeAddContact        = 34
	OpcodeAddContactByPhone = 41
	OpcodeResolveChannel    = 48
	OpcodeGetHistory        = 49
	OpcodePinMessage        = 55
	OpcodeJoinChannel       = 57
	OpcodeGetMembers        = 59
	OpcodeSendMessage       = 64
	OpcodePrepareAttach     = 65
	OpcodeDeleteMessage     = 66
	OpcodeEditMessage       = 67
	OpcodeSubscribeChat     = 75
	OpcodeGroupOps          = 77
	OpcodeFastStartCall     = 78
	OpcodePhotoUploadURL    = 80
	OpcodeVideoUploadURL    = 82
	OpcodeVideoDownloadURL  = 83
	OpcodeFileUploadURL     = 87
	OpcodeFileDownloadURL   = 88
	OpcodeResolveByLink     = 89
	OpcodeMessageEvent      = 128
	OpcodeUploadComplete    = 136
	OpcodeIncomingCall      = 137
	OpcodeCallToken         = 158
	OpcodeReactMessage      = 178
	OpcodeGetReactions      = 181
	OpcodeGetQR             = 288
	OpcodeGetQRStatus       = 289
	OpcodeLoginByQR         = 291
)

Opcodes for the MAX protocol.

Variables

View Source
var (
	ErrDisconnected = errors.New("maxclient: disconnected")
	ErrReconnecting = errors.New("maxclient: reconnecting")
	ErrReconnected  = errors.New("maxclient: reconnected")
	ErrAuthRequired = errors.New("maxclient: authentication required")
	ErrQRExpired    = errors.New("maxclient: QR code expired")
	ErrNotConnected = errors.New("maxclient: not connected")
)

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Type       string      `json:"_type"`
	PhotoToken string      `json:"photoToken,omitempty"`
	VideoID    json.Number `json:"videoId,omitempty"`
	FileID     json.Number `json:"fileId,omitempty"`
	Token      string      `json:"token,omitempty"`
}

Attachment represents a media attachment in a message. VideoID and FileID use json.Number so they serialize as numbers (server requirement).

type CallSession added in v0.3.0

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

CallSession represents an established call with a bidirectional data stream.

func (*CallSession) Close added in v0.3.0

func (s *CallSession) Close() error

func (*CallSession) Read added in v0.3.0

func (s *CallSession) Read(b []byte) (int, error)

func (*CallSession) Write added in v0.3.0

func (s *CallSession) Write(b []byte) (int, error)

type Channel

type Channel struct {
	ID    int64  `json:"id"`
	Title string `json:"title,omitempty"`
}

Channel represents a MAX channel.

type Client

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

Client is a MAX messenger API client.

func New

func New(opts ...Option) *Client

New creates a new Client with the given options.

func (*Client) AuthToken

func (c *Client) AuthToken(ctx context.Context, token, deviceID string) error

AuthToken authenticates using a previously obtained token and device ID. Sends hello -> login_by_token -> starts keepalive.

func (*Client) Call added in v0.3.0

func (c *Client) Call(ctx context.Context, calleeExternalID int64, forceRelay bool) (*CallSession, error)

Call initiates a call to the given user and returns a CallSession with a bidirectional data stream over ICE. calleeExternalID is the callee's externalId in OneMe (obtain via FindUserByPhone — it's the User.ExternalID field). forceRelay=true restricts to TURN-only candidates (no P2P).

func (*Client) Close

func (c *Client) Close() error

Close gracefully shuts down the client. This is terminal — the client cannot be reused after Close.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect establishes a WebSocket connection to the MAX server. Does not authenticate — call AuthToken or StartQRAuth/WaitQRAuth after connecting.

func (*Client) CreateGroup

func (c *Client) CreateGroup(ctx context.Context, name string, memberIDs []int64) (*Packet, error)

CreateGroup creates a new group chat with the given name and member IDs.

func (*Client) DeleteMessage

func (c *Client) DeleteMessage(ctx context.Context, chatID int64, messageIDs []string, forMe bool) error

DeleteMessage deletes messages from a chat.

func (*Client) EditMessage

func (c *Client) EditMessage(ctx context.Context, chatID int64, messageID string, text string, attaches ...Attachment) error

EditMessage edits a message in a chat. Optional attaches can be provided.

func (*Client) Errors

func (c *Client) Errors() <-chan error

Errors returns a channel of connection errors and status notifications. Status notifications include ErrReconnecting and ErrReconnected.

func (*Client) FindUserByPhone added in v0.2.0

func (c *Client) FindUserByPhone(ctx context.Context, phone string) (*User, error)

FindUserByPhone looks up a user by phone number. Calls addContactByPhone to get the user's externalId (contact.id), then resolves the chatID from chat participants via ResolveChannel.

func (*Client) GetGroupMembers

func (c *Client) GetGroupMembers(ctx context.Context, groupID int64) (*Packet, error)

GetGroupMembers retrieves the member list of a group.

func (*Client) GetHistory

func (c *Client) GetHistory(ctx context.Context, chatID int64, count int) (*Packet, error)

GetHistory retrieves chat message history.

func (*Client) InvokeMethod

func (c *Client) InvokeMethod(ctx context.Context, opcode int, payload any) (*Packet, error)

InvokeMethod sends a request and waits for the matching response. If auto-reconnect is in progress, blocks until reconnection completes (respecting ctx).

func (*Client) JoinGroup

func (c *Client) JoinGroup(ctx context.Context, groupID int64) error

JoinGroup joins a group chat.

func (*Client) LeaveGroup

func (c *Client) LeaveGroup(ctx context.Context, groupID int64) error

LeaveGroup leaves a group chat.

func (*Client) Packets

func (c *Client) Packets() <-chan *Packet

Packets returns a channel of incoming unsolicited packets.

func (*Client) PinMessage

func (c *Client) PinMessage(ctx context.Context, chatID int64, messageID string, notify bool) error

PinMessage pins a message in a chat.

func (c *Client) ResolveByLink(ctx context.Context, link string) (*Packet, error)

ResolveByLink resolves a channel or group by its invite link.

func (*Client) ResolveChannel

func (c *Client) ResolveChannel(ctx context.Context, channelID int64) (*Packet, error)

ResolveChannel resolves a channel by its ID.

func (*Client) ResolveUsers

func (c *Client) ResolveUsers(ctx context.Context, userIDs []int64) (*Packet, error)

ResolveUsers resolves users by their IDs.

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, chatID int64, text string, opts ...SendMessageOpts) (*Packet, error)

SendMessage sends a text message to a chat.

func (*Client) SetProfile

func (c *Client) SetProfile(ctx context.Context, opts ProfileOpts) error

SetProfile updates the current user's profile fields. Only non-nil fields are sent.

func (*Client) SetSettings

func (c *Client) SetSettings(ctx context.Context, opts SettingsOpts) error

SetSettings updates the current user's settings.

func (*Client) StartQRAuth

func (c *Client) StartQRAuth(ctx context.Context) (string, error)

StartQRAuth begins QR-code authentication. Sends hello -> requests QR code (opcode 288) -> stores trackId internally -> returns the QR link. Consumer should display the link as a QR code, then call WaitQRAuth.

func (*Client) SubscribeChat added in v0.2.1

func (c *Client) SubscribeChat(ctx context.Context, chatID int64) (*Packet, error)

SubscribeChat subscribes to events for the given chat ID. Without subscribing, the WebSocket does not receive message events for a chat.

func (*Client) UploadFile

func (c *Client) UploadFile(ctx context.Context, filename string, data io.Reader) (*Attachment, error)

UploadFile uploads a file and waits for server-side processing completion (opcode 136).

func (*Client) UploadPhoto

func (c *Client) UploadPhoto(ctx context.Context, filename string, data io.Reader) (*Attachment, error)

UploadPhoto uploads a photo and returns an Attachment for use in SendMessage.

func (*Client) UploadVideo

func (c *Client) UploadVideo(ctx context.Context, filename string, data io.Reader) (*Attachment, error)

UploadVideo uploads a video and waits for server-side processing completion (opcode 136).

func (*Client) WaitForCall added in v0.3.0

func (c *Client) WaitForCall(ctx context.Context, forceRelay bool) (*CallSession, error)

WaitForCall blocks until an incoming call arrives and establishes the ICE connection. Returns a CallSession with a bidirectional data stream.

func (*Client) WaitQRAuth

func (c *Client) WaitQRAuth(ctx context.Context) (*QRAuth, error)

WaitQRAuth waits for the QR code to be scanned, then exchanges for a token. Blocks until the QR code is scanned or the context is cancelled. Must be called after StartQRAuth.

type Group

type Group struct {
	ID    int64  `json:"id"`
	Title string `json:"title,omitempty"`
}

Group represents a MAX group chat.

type Message

type Message struct {
	ID     string `json:"id"`
	ChatID int64  `json:"chatId,omitempty"`
	Text   string `json:"text"`
	Status string `json:"status,omitempty"`
	Sender int64  `json:"sender,omitempty"`
}

Message represents a chat message.

type MessageEvent

type MessageEvent struct {
	ChatID  int64    `json:"chatId"`
	Message *Message `json:"message"`
}

MessageEvent is the payload of opcode 128 packets.

func ParseMessageEvent

func ParseMessageEvent(p *Packet) (*MessageEvent, error)

ParseMessageEvent extracts a MessageEvent from a Packet with opcode 128.

type Option

type Option func(*Client)

Option configures a Client.

func WithAutoReconnect

func WithAutoReconnect(enabled bool) Option

WithAutoReconnect enables automatic reconnection on disconnect.

func WithHTTPClient added in v0.3.0

func WithHTTPClient(hc *http.Client) Option

WithHTTPClient sets a custom HTTP client for file uploads.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets a custom logger.

func WithPacketBufferSize

func WithPacketBufferSize(n int) Option

WithPacketBufferSize sets the buffer size for the Packets channel. Panics if n < 0.

func WithReconnectBackoff

func WithReconnectBackoff(min, max time.Duration) Option

WithReconnectBackoff sets the min and max backoff durations for reconnection.

func WithUserAgent

func WithUserAgent(ua string) Option

WithUserAgent sets a custom User-Agent header.

type Packet

type Packet struct {
	Ver     int             `json:"ver"`
	Cmd     int             `json:"cmd"`
	Seq     int             `json:"seq"`
	Opcode  int             `json:"opcode"`
	Payload json.RawMessage `json:"payload"`
}

Packet is the wire-level MAX protocol message.

type ProfileOpts

type ProfileOpts struct {
	FirstName *string
	LastName  *string
	Bio       *string
}

ProfileOpts contains optional parameters for SetProfile.

type QRAuth

type QRAuth struct {
	Token    string
	DeviceID string
	ChatID   int64
}

QRAuth contains the result of a successful QR authentication.

type SendMessageOpts

type SendMessageOpts struct {
	ReplyTo  string // empty = no reply
	Attaches []Attachment
}

SendMessageOpts contains optional parameters for SendMessage.

type SettingsOpts

type SettingsOpts struct {
	Settings map[string]any
}

SettingsOpts contains optional parameters for SetSettings.

type User

type User struct {
	ID         int64  `json:"id"`
	ExternalID int64  `json:"externalId,omitempty"`
	FirstName  string `json:"firstName,omitempty"`
	LastName   string `json:"lastName,omitempty"`
	Phone      string `json:"phone,omitempty"`
}

User represents a MAX user.

Jump to

Keyboard shortcuts

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