weixin

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package weixin implements the WeChat iLink Bot API client. It provides a pure HTTP SDK for QR login, long-poll message receiving, and text sending. No jcode-specific dependencies.

Package weixin is the channel.Channel implementation for WeChat. It wraps the iLink API (internal/pkg/weixin/api.go) with credential persistence, poll loop lifecycle, and the jcode channel interface.

Index

Constants

View Source
const (
	ItemTypeText  = 1
	ItemTypeImage = 2
	ItemTypeVoice = 3
	ItemTypeFile  = 4
	ItemTypeVideo = 5
)

Item type constants.

View Source
const (
	DefaultBaseURL = "https://ilinkai.weixin.qq.com"
)

Variables

This section is empty.

Functions

func FetchQRCode

func FetchQRCode(baseURL string) (qrContent, sessionKey string, err error)

FetchQRCode fetches a login QR code from the iLink service. Returns QRCodeImgContent (the URL to encode as QR) and QRCode (the session polling key).

func IsSessionExpired

func IsSessionExpired(err error) bool

IsSessionExpired returns true if the error indicates the bot token is invalid.

func IsSessionNotReady

func IsSessionNotReady(err error) bool

IsSessionNotReady returns true if the error indicates the session has not been activated yet (getupdates has not completed at least once).

func SendText

func SendText(baseURL, token, toUserID, text, contextToken string) error

SendText sends a text message to toUserID. contextToken should be echoed from the inbound message when replying; pass "" for proactive sends.

Types

type Client

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

Client implements channel.Channel for WeChat via the iLink Bot API.

func NewClient

func NewClient() *Client

NewClient creates a new WeChat channel client, loading any saved credentials.

func (*Client) Disable

func (c *Client) Disable() error

Disable stops polling but keeps credentials.

func (*Client) Enable

func (c *Client) Enable() error

Enable starts the inbound message poll loop.

func (*Client) ID

func (c *Client) ID() string

func (*Client) Login

func (c *Client) Login() (*channel.LoginSession, error)

Login initiates a QR code login flow and returns a LoginSession.

func (*Client) Logout

func (c *Client) Logout() error

Logout clears credentials and stops polling.

func (*Client) SendText

func (c *Client) SendText(text string) error

SendText sends a text message to the connected user. Retries for up to 45 s if the session is not yet activated (ret=-2).

func (*Client) SetOnMessage

func (c *Client) SetOnMessage(fn func(from, text string))

SetOnMessage registers a callback for inbound messages.

func (*Client) State

func (c *Client) State() channel.State

type Credentials

type Credentials struct {
	Token   string `json:"token"`
	BaseURL string `json:"base_url"`
	UserID  string `json:"user_id"`
	SyncBuf string `json:"sync_buf,omitempty"`
	SavedAt string `json:"saved_at"`
}

Credentials holds the persisted login state.

type GetUpdatesResponse

type GetUpdatesResponse struct {
	Ret           int       `json:"ret"`
	ErrCode       int       `json:"errcode"`
	ErrMsg        string    `json:"errmsg"`
	Msgs          []Message `json:"msgs"`
	GetUpdatesBuf string    `json:"get_updates_buf"`
}

GetUpdatesResponse is the parsed response from /ilink/bot/getupdates.

func GetUpdates

func GetUpdates(ctx context.Context, baseURL, token, syncBuf string) (*GetUpdatesResponse, error)

GetUpdates performs one long-poll for inbound messages. The context controls the HTTP request lifetime; cancel it to stop the poll.

type Item

type Item struct {
	Type     int       `json:"type"`
	TextItem *TextItem `json:"text_item,omitempty"`
}

Item is a single content item within a message.

type LoginResult

type LoginResult struct {
	Connected bool
	Token     string
	BaseURL   string
	UserID    string
	Message   string
}

LoginResult is the outcome of a QR login flow.

func PollLoginUntilDone

func PollLoginUntilDone(baseURL, sessionKey string, timeout time.Duration) *LoginResult

PollLoginUntilDone polls the QR scan status until confirmed, expired, or timeout. It handles IDC redirects (scaned_but_redirect) transparently.

type Message

type Message struct {
	Seq          int    `json:"seq"`
	MessageID    int    `json:"message_id"`
	FromUserID   string `json:"from_user_id"`
	ToUserID     string `json:"to_user_id"`
	ClientID     string `json:"client_id"`
	CreateTimeMs int64  `json:"create_time_ms"`
	MessageType  int    `json:"message_type"`
	MessageState int    `json:"message_state"`
	ItemList     []Item `json:"item_list"`
	ContextToken string `json:"context_token"`
}

Message is a single inbound WeChat message.

func (Message) TextBody

func (m Message) TextBody() string

TextBody extracts the text content from the message, or returns empty string.

type TextItem

type TextItem struct {
	Text string `json:"text"`
}

TextItem holds the text payload.

Jump to

Keyboard shortcuts

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