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
- func AuthHeaders(token string) http.Header
- func BuildTextMessage(fromUserID, toUserID, contextToken, text string) map[string]interface{}
- func ClearCredentials(path string) error
- func CommonHeaders() http.Header
- func DecodeAESKey(encoded string) ([]byte, error)
- func DecryptAESECB(ciphertext, key []byte) ([]byte, error)
- func EncodeAESKeyBase64(key []byte) string
- func EncodeAESKeyHex(key []byte) string
- func EncryptAESECB(plaintext, key []byte) ([]byte, error)
- func GenerateAESKey() ([]byte, error)
- func SaveCredentials(creds *Credentials, path string) error
- type APIError
- type Bot
- type BotOptions
- type Client
- func (c *Client) GetConfig(ctx context.Context, baseURL, token, userID, contextToken string) (*GetConfigResponse, error)
- func (c *Client) GetQRCode(ctx context.Context, baseURL string) (*QRCodeResponse, error)
- func (c *Client) GetUpdates(ctx context.Context, baseURL, token, cursor string) (*GetUpdatesResponse, error)
- func (c *Client) PollQRStatus(ctx context.Context, baseURL, qrcode string) (*QRStatusResponse, error)
- func (c *Client) SendMessage(ctx context.Context, baseURL, token string, msg interface{}) error
- func (c *Client) SendTyping(ctx context.Context, baseURL, token, userID, ticket string, status int) error
- type Credentials
- type GetConfigResponse
- type GetUpdatesResponse
- type IncomingMessage
- type LoginOptions
- type MessageItem
- type MessageItemType
- type MessageType
- type QRCodeResponse
- type QRStatusResponse
- type TextItem
- type WireMessage
Constants ¶
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 ¶
AuthHeaders returns the standard iLink POST headers.
func BuildTextMessage ¶
BuildTextMessage creates a text message payload.
func ClearCredentials ¶
ClearCredentials removes stored credentials.
func CommonHeaders ¶
CommonHeaders returns headers for iLink API requests.
func DecodeAESKey ¶
DecodeAESKey decodes an aes_key from the protocol. Handles: direct hex (32 chars), base64(raw 16 bytes), base64(hex string 32 chars).
func DecryptAESECB ¶
DecryptAESECB decrypts AES-128-ECB ciphertext and removes PKCS7 padding.
func EncodeAESKeyBase64 ¶
EncodeAESKeyBase64 returns base64(hex) for CDNMedia.aes_key.
func EncodeAESKeyHex ¶
EncodeAESKeyHex returns the hex string of a key.
func EncryptAESECB ¶
EncryptAESECB encrypts plaintext with AES-128-ECB and PKCS7 padding.
func GenerateAESKey ¶
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 ¶
APIError is returned when the iLink API returns a non-zero ret or HTTP error.
func (*APIError) IsSessionExpired ¶
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 (*Bot) IsConnected ¶
func (*Bot) SendMessage ¶
SendMessage sends a text message to a user.
type BotOptions ¶
BotOptions configures a WeChat Bot.
type Client ¶
Client wraps HTTP calls to the iLink API.
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) 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 ¶
SendMessage sends a message through the iLink API.
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 ¶
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 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.