Documentation
¶
Overview ¶
Adapter implements im.Adapter and im.FileDownloader for WeChat personal accounts via the Tencent iLink Bot API.
WeChat iLink uses HTTP long-polling for receiving messages (no WebSocket, no Webhook). Sending is done via REST API.
API base: https://ilinkai.weixin.qq.com API paths: /ilink/bot/getupdates, /ilink/bot/sendmessage, etc. Auth: Bearer token obtained via QR code login flow.
Package wechat implements the WeChat personal account IM adapter for WeKnora via the Tencent iLink Bot API (ilinkai.weixin.qq.com).
Long-polling client for the WeChat iLink Bot API.
Flow:
- POST /ilink/bot/getupdates with get_updates_buf + base_info
- Parse response msgs[] into IncomingMessage
- Call msgHandler for each message
- Update cursor (get_updates_buf) for next poll
- On error, exponential backoff retry
Token expiry: errcode -14 signals the token is no longer valid.
QR code login flow for WeChat iLink Bot API.
API endpoints (all relative to the iLink base URL):
GET /ilink/bot/get_bot_qrcode?bot_type=3 → returns {qrcode, qrcode_img_content}
GET /ilink/bot/get_qrcode_status?qrcode=xxx → returns {status, bot_token, ilink_bot_id, ...}
Flow:
- Call GetLoginQRCode to obtain a QR code URL and opaque qrcode token
- Display qrcode_img_content URL to user (in frontend)
- Long-poll PollQRCodeStatus until user scans and confirms
- On success, receive bot_token + ilink_bot_id + ilink_user_id + baseurl
Index ¶
- Variables
- func BuildCDNDownloadURL(encryptQueryParam string) string
- func NewFactory() im.AdapterFactory
- type Adapter
- func (a *Adapter) DownloadFile(ctx context.Context, msg *im.IncomingMessage) (io.ReadCloser, string, error)
- func (a *Adapter) HandleURLVerification(c *gin.Context) bool
- func (a *Adapter) ParseCallback(c *gin.Context) (*im.IncomingMessage, error)
- func (a *Adapter) Platform() im.Platform
- func (a *Adapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, reply *im.ReplyMessage) error
- func (a *Adapter) SendTyping(ctx context.Context, incoming *im.IncomingMessage) error
- func (a *Adapter) VerifyCallback(c *gin.Context) error
- type LoginResult
- type LongPollClient
- type QRCodeResult
- type QRCodeService
Constants ¶
This section is empty.
Variables ¶
var ErrTokenExpired = fmt.Errorf("wechat bot token expired")
ErrTokenExpired indicates the bot token has expired and a re-login is required.
Functions ¶
func BuildCDNDownloadURL ¶
BuildCDNDownloadURL constructs a CDN download URL from an encrypt_query_param.
func NewFactory ¶
func NewFactory() im.AdapterFactory
NewFactory returns an im.AdapterFactory for WeChat channels (iLink bot). WeChat only supports a long-polling mode, so there is no mode branch.
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter implements im.Adapter for WeChat via iLink Bot API.
func NewAdapter ¶
NewAdapter creates a new WeChat iLink adapter.
func (*Adapter) DownloadFile ¶
func (a *Adapter) DownloadFile(ctx context.Context, msg *im.IncomingMessage) (io.ReadCloser, string, error)
DownloadFile downloads a media file from the iLink CDN. Files are AES-128-ECB encrypted; the key is provided in the message Extra.
func (*Adapter) HandleURLVerification ¶
HandleURLVerification is not applicable for WeChat.
func (*Adapter) ParseCallback ¶
ParseCallback is not supported — messages arrive via long-polling.
func (*Adapter) SendReply ¶
func (a *Adapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, reply *im.ReplyMessage) error
SendReply sends a text reply to the user via iLink /ilink/bot/sendmessage API.
func (*Adapter) SendTyping ¶
SendTyping sends a typing indicator to the user.
type LoginResult ¶
type LoginResult struct {
Status string `json:"status"` // "wait", "scaned", "confirmed", "expired"
BotToken string `json:"bot_token"` // Bearer token for API calls
ILinkBotID string `json:"ilink_bot_id"` // Bot identifier
ILinkUserID string `json:"ilink_user_id"` // User identifier
BaseURL string `json:"baseurl"` // API base URL (may override default)
}
LoginResult holds the credentials returned after successful QR code scan.
type LongPollClient ¶
type LongPollClient struct {
// contains filtered or unexported fields
}
LongPollClient receives messages from WeChat via HTTP long-polling.
func NewLongPollClient ¶
func NewLongPollClient(botToken, ilinkBotID string, handler func(ctx context.Context, msg *im.IncomingMessage) error) *LongPollClient
NewLongPollClient creates a new WeChat long-polling client.
type QRCodeResult ¶
type QRCodeResult struct {
// QRCodeURL is the URL to render as a QR code image (qrcode_img_content).
QRCodeURL string `json:"qrcode_url"`
// QRCode is the opaque token used to poll for scan status.
QRCode string `json:"qrcode"`
}
QRCodeResult holds the result of requesting a login QR code.
type QRCodeService ¶
type QRCodeService struct {
// contains filtered or unexported fields
}
QRCodeService handles WeChat QR code login operations.
func NewQRCodeService ¶
func NewQRCodeService() *QRCodeService
NewQRCodeService creates a new QR code service.
func (*QRCodeService) GetLoginQRCode ¶
func (s *QRCodeService) GetLoginQRCode(ctx context.Context) (*QRCodeResult, error)
GetLoginQRCode requests a new login QR code from iLink API. GET /ilink/bot/get_bot_qrcode?bot_type=3
func (*QRCodeService) PollQRCodeStatus ¶
func (s *QRCodeService) PollQRCodeStatus(ctx context.Context, qrcode string) (*LoginResult, error)
PollQRCodeStatus checks the scan status of a QR code. GET /ilink/bot/get_qrcode_status?qrcode=xxx This is a long-poll endpoint: the server holds the connection until there is a status change or ~35 seconds elapse. Status values: "wait", "scaned", "confirmed", "expired"