Documentation
¶
Index ¶
- Variables
- type APIError
- type CDNError
- type Client
- func (c *Client[T]) CancelTyping(ctx context.Context, to string) error
- func (c *Client[T]) ClientID() string
- func (c *Client[T]) HasCredentials() bool
- func (c *Client[T]) Login(ctx context.Context) (*LoginSession, error)
- func (c *Client[T]) SendFile(ctx context.Context, to string, data []byte, filename, caption string) error
- func (c *Client[T]) SendImage(ctx context.Context, to string, data []byte, caption string) error
- func (c *Client[T]) SendText(ctx context.Context, to, text string) error
- func (c *Client[T]) SendTyping(ctx context.Context, to string) error
- func (c *Client[T]) SendVideo(ctx context.Context, to string, data []byte, caption string) error
- func (c *Client[T]) Start(ctx context.Context) error
- func (c *Client[T]) State() ClientState
- func (c *Client[T]) Stop()
- func (c *Client[T]) UserState() T
- type ClientState
- type DefaultClient
- type DefaultEventHooks
- type DefaultOption
- type EventHooks
- type LoginSession
- type MediaAttachment
- type Message
- type Option
- func WithBaseURL[T any](url string) Option[T]
- func WithCDNBaseURL[T any](url string) Option[T]
- func WithEventHooks[T any](h EventHooks[T]) Option[T]
- func WithHTTPClient[T any](hc *http.Client) Option[T]
- func WithLogger[T any](l *slog.Logger) Option[T]
- func WithMessageBufferSize[T any](n int) Option[T]
- func WithRouteTag[T any](tag string) Option[T]
- func WithVersion[T any](version string) Option[T]
- type RefMessage
- type SessionExpiredError
- type VoiceAttachment
Constants ¶
This section is empty.
Variables ¶
var ErrNotLoggedIn = errors.New("wechat: not logged in, call Login() first")
ErrNotLoggedIn is returned by Start when no credentials are available.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client[T any] struct { // contains filtered or unexported fields }
Client[T] is the main entry point for the WeChat iLink Bot SDK. T is a user-defined state type for carrying business context.
func New ¶
New creates a new Client.
- clientID: business identifier (e.g. "user-123"), used as Store key and in EventHook callbacks
- state: user-defined state accessible via UserState()
- s: storage backend, nil uses in-memory store
func (*Client[T]) CancelTyping ¶
CancelTyping cancels the typing indicator for a user.
func (*Client[T]) HasCredentials ¶
HasCredentials checks if the Store has credentials for this clientID.
func (*Client[T]) Login ¶
func (c *Client[T]) Login(ctx context.Context) (*LoginSession, error)
Login initiates a QR code login. Returns a LoginSession whose QRCodeURL() can be displayed immediately. Call session.Wait() to block until completion.
func (*Client[T]) SendFile ¶
func (c *Client[T]) SendFile(ctx context.Context, to string, data []byte, filename, caption string) error
SendFile uploads and sends a file attachment.
func (*Client[T]) SendTyping ¶
SendTyping sends a typing indicator to a user.
func (*Client[T]) Start ¶
Start begins the long-poll monitor loop. Blocks until ctx is cancelled. If Login() was not called, Start tries to load credentials from Store. Returns ErrNotLoggedIn if no credentials are available.
func (*Client[T]) State ¶
func (c *Client[T]) State() ClientState
State returns the current lifecycle state.
type ClientState ¶
type ClientState int
ClientState represents the lifecycle state of a Client.
const ( StateNew ClientState = iota // Just created StateLoggingIn // Login() in progress StateReady // Has credentials, can Start StateRunning // Start() running, long-polling StateSessionExpired // Server returned errcode -14 StateStopped // Start() ended )
func (ClientState) String ¶
func (s ClientState) String() string
type DefaultClient ¶
type DefaultClient = Client[struct{}]
DefaultClient is a Client without custom state, for the common case.
func NewDefault ¶
func NewDefault(clientID string, s store.Store, opts ...DefaultOption) *DefaultClient
NewDefault creates a Client without custom state.
client := wechat.NewDefault("my-bot", nil)
client := wechat.NewDefault("my-bot", store.NewFileStore("./data"),
wechat.WithDefaultEventHooks(wechat.DefaultEventHooks{
OnMessage: func(id string, msg *wechat.Message) { ... },
OnConnected: func(id string) { ... },
}),
)
type DefaultEventHooks ¶
type DefaultEventHooks struct {
OnMessage func(clientID string, msg *Message)
OnQRCode func(clientID string, qrCodeURL string)
OnQRScanned func(clientID string)
OnQRExpired func(clientID string, refreshCount int)
OnConnected func(clientID string)
OnSessionExpired func(clientID string)
OnDisconnected func(clientID string, err error)
OnError func(clientID string, err error)
}
DefaultEventHooks provides event callbacks without the state parameter, for use with DefaultClient.
type DefaultOption ¶
type DefaultOption = Option[struct{}]
DefaultOption is an Option without custom state.
func WithDefaultEventHooks ¶
func WithDefaultEventHooks(h DefaultEventHooks) DefaultOption
WithDefaultEventHooks sets event hooks for a DefaultClient. It wraps each callback to match the EventHooks[struct{}] signature.
type EventHooks ¶
type EventHooks[T any] struct { OnMessage func(client *Client[T], msg *Message) OnQRCode func(client *Client[T], qrCodeURL string) OnQRScanned func(client *Client[T]) OnQRExpired func(client *Client[T], refreshCount int) OnConnected func(client *Client[T]) OnSessionExpired func(client *Client[T]) OnDisconnected func(client *Client[T], err error) OnError func(client *Client[T], err error) }
EventHooks receives lifecycle events. All callbacks receive *Client[T] as the first parameter, giving access to the client, its ID, and user state.
type LoginSession ¶
type LoginSession struct {
// contains filtered or unexported fields
}
LoginSession represents an in-progress QR login.
func (*LoginSession) QRCodeURL ¶
func (s *LoginSession) QRCodeURL() string
QRCodeURL returns the URL for displaying the QR code.
type MediaAttachment ¶
type MediaAttachment = monitor.MediaAttachment
MediaAttachment holds decoded media data.
type Option ¶
type Option[T any] func(*clientConfig[T])
Option configures a Client. Use With* functions to create options.
func WithBaseURL ¶
WithBaseURL overrides the default API base URL.
func WithCDNBaseURL ¶
WithCDNBaseURL overrides the default CDN base URL.
func WithEventHooks ¶
func WithEventHooks[T any](h EventHooks[T]) Option[T]
WithEventHooks sets lifecycle event callbacks.
func WithHTTPClient ¶
WithHTTPClient sets a custom http.Client for all requests.
func WithLogger ¶
WithLogger sets a structured logger.
func WithMessageBufferSize ¶
WithMessageBufferSize sets the OnMessage internal buffer size.
func WithRouteTag ¶
WithRouteTag sets the SKRouteTag header value.
func WithVersion ¶
WithVersion sets the channel version string.
type SessionExpiredError ¶
type SessionExpiredError struct {
AccountID string
}
SessionExpiredError indicates the server returned errcode -14.
func (*SessionExpiredError) Error ¶
func (e *SessionExpiredError) Error() string
type VoiceAttachment ¶
type VoiceAttachment = monitor.VoiceAttachment
VoiceAttachment holds decoded voice data.