wechat

package
v1.1.66 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package wechat implements the WeChat iLink Bot messaging platform adapter. Protocol implementation is based on the iLink Bot API specification. Zero external dependencies — uses only Go standard library.

Index

Constants

View Source
const (
	DefaultBaseURL = "https://ilinkai.weixin.qq.com"
	CDNBaseURL     = "https://novac2c.cdn.weixin.qq.com/c2c"
	ChannelVersion = "0.1.0"
)

Variables

This section is empty.

Functions

func AuthHeaders

func AuthHeaders(token string) http.Header

AuthHeaders returns the standard iLink POST headers.

func BuildTextMessage

func BuildTextMessage(fromUserID, toUserID, contextToken, text string) map[string]interface{}

BuildTextMessage creates a text message payload.

func ClearCredentials

func ClearCredentials(path string) error

ClearCredentials removes stored credentials.

func CommonHeaders

func CommonHeaders() http.Header

CommonHeaders returns headers for iLink API requests.

func DecodeAESKey

func DecodeAESKey(encoded string) ([]byte, error)

DecodeAESKey decodes an aes_key from the protocol. Handles: direct hex (32 chars), base64(raw 16 bytes), base64(hex string 32 chars).

func DecryptAESECB

func DecryptAESECB(ciphertext, key []byte) ([]byte, error)

DecryptAESECB decrypts AES-128-ECB ciphertext and removes PKCS7 padding.

func EncodeAESKeyBase64

func EncodeAESKeyBase64(key []byte) string

EncodeAESKeyBase64 returns base64(hex) for CDNMedia.aes_key.

func EncodeAESKeyHex

func EncodeAESKeyHex(key []byte) string

EncodeAESKeyHex returns the hex string of a key.

func EncryptAESECB

func EncryptAESECB(plaintext, key []byte) ([]byte, error)

EncryptAESECB encrypts plaintext with AES-128-ECB and PKCS7 padding.

func GenerateAESKey

func GenerateAESKey() ([]byte, error)

GenerateAESKey generates a random 16-byte AES key.

func SaveCredentials

func SaveCredentials(creds *Credentials, path string) error

SaveCredentials persists credentials to disk.

Types

type APIError

type APIError struct {
	Message    string
	HTTPStatus int
	ErrCode    int
}

APIError is returned when the iLink API returns a non-zero ret or HTTP error.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) IsSessionExpired

func (e *APIError) IsSessionExpired() bool

IsSessionExpired returns true if this error indicates session timeout.

type Bot

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

Bot implements messaging.Platform for WeChat via the iLink protocol.

func NewBot

func NewBot(opts BotOptions) *Bot

NewBot creates a new WeChat bot.

func (*Bot) IsConnected

func (b *Bot) IsConnected() bool

func (*Bot) Name

func (b *Bot) Name() string

func (*Bot) SendMessage

func (b *Bot) SendMessage(ctx context.Context, chatID string, text string) error

SendMessage sends a text message to a user.

func (*Bot) Start

func (b *Bot) Start(ctx context.Context, handler messaging.MessageHandler) error

Start begins long-poll message receiving. Blocks until ctx is cancelled.

func (*Bot) Stop

func (b *Bot) Stop() error

Stop gracefully stops the bot.

type BotOptions

type BotOptions struct {
	CredPath   string
	AutoTyping bool
}

BotOptions configures a WeChat Bot.

type Client

type Client struct {
	HTTP *http.Client
}

Client wraps HTTP calls to the iLink API.

func NewClient

func NewClient() *Client

NewClient creates a protocol client.

func (*Client) GetConfig

func (c *Client) GetConfig(ctx context.Context, baseURL, token, userID, contextToken string) (*GetConfigResponse, error)

GetConfig gets the typing ticket for a user.

func (*Client) GetQRCode

func (c *Client) GetQRCode(ctx context.Context, baseURL string) (*QRCodeResponse, error)

GetQRCode requests a new QR code for login.

func (*Client) GetUpdates

func (c *Client) GetUpdates(ctx context.Context, baseURL, token, cursor string) (*GetUpdatesResponse, error)

GetUpdates performs a long-poll for new messages.

func (*Client) PollQRStatus

func (c *Client) PollQRStatus(ctx context.Context, baseURL, qrcode string) (*QRStatusResponse, error)

PollQRStatus polls the QR code scan status.

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, baseURL, token string, msg interface{}) error

SendMessage sends a message through the iLink API.

func (*Client) SendTyping

func (c *Client) SendTyping(ctx context.Context, baseURL, token, userID, ticket string, status int) error

SendTyping sends or cancels the typing indicator.

type Credentials

type Credentials struct {
	Token     string `json:"token"`
	BaseURL   string `json:"baseUrl"`
	AccountID string `json:"accountId"`
	UserID    string `json:"userId"`
	SavedAt   string `json:"savedAt,omitempty"`
}

Credentials holds login credentials.

func LoadCredentials

func LoadCredentials(path string) (*Credentials, error)

LoadCredentials loads stored credentials from disk.

func Login

func Login(ctx context.Context, client *Client, opts LoginOptions) (*Credentials, error)

Login performs QR code login, returning credentials. If stored credentials exist and Force is false, returns them directly.

type GetConfigResponse

type GetConfigResponse struct {
	TypingTicket string `json:"typing_ticket,omitempty"`
}

GetConfigResponse from getconfig.

type GetUpdatesResponse

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

GetUpdatesResponse from getupdates.

type IncomingMessage

type IncomingMessage struct {
	UserID       string
	Text         string
	Timestamp    time.Time
	ContextToken string
}

IncomingMessage is a parsed incoming user message.

type LoginOptions

type LoginOptions struct {
	BaseURL   string
	CredPath  string
	Force     bool
	OnQRURL   func(url string)
	OnScanned func()
	OnExpired func()
}

LoginOptions configures the login flow.

type MessageItem

type MessageItem struct {
	Type     MessageItemType `json:"type"`
	TextItem *TextItem       `json:"text_item,omitempty"`
}

MessageItem is a single content item within a message.

type MessageItemType

type MessageItemType int

MessageItemType indicates the content type.

const (
	ItemText  MessageItemType = 1
	ItemImage MessageItemType = 2
	ItemVoice MessageItemType = 3
	ItemFile  MessageItemType = 4
	ItemVideo MessageItemType = 5
)

type MessageType

type MessageType int

MessageType indicates who sent the message.

const (
	MessageTypeUser MessageType = 1
	MessageTypeBot  MessageType = 2
)

type QRCodeResponse

type QRCodeResponse struct {
	QRCode       string `json:"qrcode"`
	QRCodeImgURL string `json:"qrcode_img_content"`
}

QRCodeResponse from get_bot_qrcode.

type QRStatusResponse

type QRStatusResponse struct {
	Status       string `json:"status"`
	BotToken     string `json:"bot_token,omitempty"`
	BotID        string `json:"ilink_bot_id,omitempty"`
	UserID       string `json:"ilink_user_id,omitempty"`
	BaseURL      string `json:"baseurl,omitempty"`
	RedirectHost string `json:"redirect_host,omitempty"`
}

QRStatusResponse from get_qrcode_status.

type TextItem

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

TextItem holds text content.

type WireMessage

type WireMessage struct {
	Seq          int64         `json:"seq,omitempty"`
	MessageID    int64         `json:"message_id,omitempty"`
	FromUserID   string        `json:"from_user_id"`
	ToUserID     string        `json:"to_user_id"`
	ClientID     string        `json:"client_id"`
	CreateTimeMs int64         `json:"create_time_ms"`
	MessageType  MessageType   `json:"message_type"`
	ContextToken string        `json:"context_token"`
	ItemList     []MessageItem `json:"item_list"`
}

WireMessage is the raw message from the iLink API.

Jump to

Keyboard shortcuts

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