Documentation
¶
Overview ¶
Package protocol implements the raw iLink Bot API HTTP calls.
Index ¶
- Constants
- Variables
- func BuildCDNUploadURL(cdnBaseURL, uploadParam, filekey string) string
- func BuildMediaMessage(userID, contextToken string, itemList []map[string]interface{}) map[string]interface{}
- func BuildTextMessage(userID, contextToken, text string) map[string]interface{}
- func RandomWechatUIN() string
- func SanitizeBotAgent(raw string) string
- type APIError
- type Client
- func (c *Client) AuthHeaders(token string) http.Header
- func (c *Client) CommonHeaders() http.Header
- func (c *Client) GetConfig(ctx context.Context, baseURL, token, userID, contextToken string) (*GetConfigResponse, error)
- func (c *Client) GetQRCode(ctx context.Context, baseURL string, localTokenList []string) (*QRCodeResponse, error)
- func (c *Client) GetUpdates(ctx context.Context, baseURL, token, cursor string, timeout time.Duration) (*GetUpdatesResponse, error)
- func (c *Client) GetUploadURL(ctx context.Context, baseURL, token string, req GetUploadURLRequest) (*GetUploadURLResponse, error)
- func (c *Client) NotifyStart(ctx context.Context, baseURL, token string) error
- func (c *Client) NotifyStop(ctx context.Context, baseURL, token string) error
- func (c *Client) PollQRStatus(ctx context.Context, baseURL, qrcode, verifyCode 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
- func (c *Client) UploadToCDN(ctx context.Context, cdnURL string, ciphertext []byte) (string, error)
- type GetConfigResponse
- type GetUpdatesResponse
- type GetUploadURLRequest
- type GetUploadURLResponse
- type QRCodeResponse
- type QRStatusResponse
Constants ¶
const ( DefaultBaseURL = "https://ilinkai.weixin.qq.com" ChannelVersion = "0.3.0" )
Variables ¶
var CDNBaseURL = "https://novac2c.cdn.weixin.qq.com/c2c"
CDNBaseURL is the fixed CDN endpoint. It is a package-level var so tests can override it.
Functions ¶
func BuildCDNUploadURL ¶
BuildCDNUploadURL constructs a CDN upload URL from params.
func BuildMediaMessage ¶
func BuildMediaMessage(userID, contextToken string, itemList []map[string]interface{}) map[string]interface{}
BuildMediaMessage creates a media message payload.
func BuildTextMessage ¶
BuildTextMessage creates a text message payload.
func RandomWechatUIN ¶
func RandomWechatUIN() string
RandomWechatUIN generates the X-WECHAT-UIN header value.
func SanitizeBotAgent ¶ added in v0.2.0
SanitizeBotAgent cleans a user-supplied bot_agent value into a wire-safe UA-style string. Invalid tokens are dropped; if nothing remains the default "OpenClaw" is returned. Grammar: bot_agent = product *( SP product ) where each product may be followed by " (comment)".
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 Client ¶
Client wraps HTTP calls to the iLink API.
func NewClient ¶
func NewClient() *Client
NewClient creates a protocol client with sensible defaults.
func (*Client) AuthHeaders ¶ added in v0.2.0
AuthHeaders returns the standard iLink POST headers.
func (*Client) CommonHeaders ¶ added in v0.2.0
CommonHeaders returns headers included in both GET and POST requests.
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, localTokenList []string) (*QRCodeResponse, error)
GetQRCode requests a new QR code for login. localTokenList may contain up to 10 recent bot tokens to speed up re-login.
func (*Client) GetUpdates ¶
func (c *Client) GetUpdates(ctx context.Context, baseURL, token, cursor string, timeout time.Duration) (*GetUpdatesResponse, error)
GetUpdates performs a long-poll for new messages. timeout controls the client-side HTTP timeout for this request.
func (*Client) GetUploadURL ¶
func (c *Client) GetUploadURL(ctx context.Context, baseURL, token string, req GetUploadURLRequest) (*GetUploadURLResponse, error)
GetUploadURL requests an upload URL for CDN media upload.
func (*Client) NotifyStart ¶ added in v0.2.0
NotifyStart tells WeChat that this bot client is starting.
func (*Client) NotifyStop ¶ added in v0.2.0
NotifyStop tells WeChat that this bot client is stopping.
func (*Client) PollQRStatus ¶
func (c *Client) PollQRStatus(ctx context.Context, baseURL, qrcode, verifyCode string) (*QRStatusResponse, error)
PollQRStatus polls the QR code scan status. If verifyCode is non-empty, it is sent on the next poll after a need_verifycode response.
func (*Client) SendMessage ¶
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.
func (*Client) UploadToCDN ¶
UploadToCDN uploads encrypted bytes to the CDN with retry (up to 3 attempts). Returns the download encrypted_query_param from the x-encrypted-param header. Client errors (4xx) abort immediately; server errors retry.
type GetConfigResponse ¶
type GetConfigResponse struct {
TypingTicket string `json:"typing_ticket,omitempty"`
Ret int `json:"ret,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"`
LongPollingTimeoutMs int `json:"longpolling_timeout_ms,omitempty"`
}
GetUpdatesResponse from getupdates.
type GetUploadURLRequest ¶
type GetUploadURLRequest struct {
FileKey string `json:"filekey"`
MediaType int `json:"media_type"`
ToUserID string `json:"to_user_id"`
RawSize int `json:"rawsize"`
RawFileMD5 string `json:"rawfilemd5"`
FileSize int `json:"filesize"`
ThumbRawSize int `json:"thumb_rawsize,omitempty"`
ThumbFileMD5 string `json:"thumb_rawfilemd5,omitempty"`
ThumbFileSize int `json:"thumb_filesize,omitempty"`
NoNeedThumb bool `json:"no_need_thumb,omitempty"`
AESKey string `json:"aeskey,omitempty"`
}
GetUploadURLRequest holds parameters for getuploadurl.
type GetUploadURLResponse ¶
type GetUploadURLResponse struct {
UploadParam string `json:"upload_param"`
ThumbUploadParam string `json:"thumb_upload_param,omitempty"`
// Complete upload URL returned by server; when set, use directly instead of building from UploadParam.
UploadFullURL string `json:"upload_full_url,omitempty"`
}
GetUploadURLResponse from getuploadurl.
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"` // wait, scaned, confirmed, expired, scaned_but_redirect, need_verifycode, verify_code_blocked, binded_redirect
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"` // set when status is scaned_but_redirect
}
QRStatusResponse from get_qrcode_status.