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
- func FetchQRCode(baseURL string) (qrContent, sessionKey string, err error)
- func IsSessionExpired(err error) bool
- func IsSessionNotReady(err error) bool
- func SendText(baseURL, token, toUserID, text, contextToken string) error
- type Client
- func (c *Client) Disable() error
- func (c *Client) Enable() error
- func (c *Client) ID() string
- func (c *Client) Login() (*channel.LoginSession, error)
- func (c *Client) Logout() error
- func (c *Client) SendText(text string) error
- func (c *Client) SetOnMessage(fn func(from, text string))
- func (c *Client) State() channel.State
- type Credentials
- type GetUpdatesResponse
- type Item
- type LoginResult
- type Message
- type TextItem
Constants ¶
const ( ItemTypeText = 1 ItemTypeImage = 2 ItemTypeVoice = 3 ItemTypeFile = 4 ItemTypeVideo = 5 )
Item type constants.
const (
DefaultBaseURL = "https://ilinkai.weixin.qq.com"
)
Variables ¶
This section is empty.
Functions ¶
func FetchQRCode ¶
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 ¶
IsSessionExpired returns true if the error indicates the bot token is invalid.
func IsSessionNotReady ¶
IsSessionNotReady returns true if the error indicates the session has not been activated yet (getupdates has not completed at least once).
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) Login ¶
func (c *Client) Login() (*channel.LoginSession, error)
Login initiates a QR code login flow and returns a LoginSession.
func (*Client) SendText ¶
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 ¶
SetOnMessage registers a callback for inbound messages.
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 LoginResult ¶
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.