wechat

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0, MIT Imports: 17 Imported by: 0

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:

  1. POST /ilink/bot/getupdates with get_updates_buf + base_info
  2. Parse response msgs[] into IncomingMessage
  3. Call msgHandler for each message
  4. Update cursor (get_updates_buf) for next poll
  5. 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:

  1. Call GetLoginQRCode to obtain a QR code URL and opaque qrcode token
  2. Display qrcode_img_content URL to user (in frontend)
  3. Long-poll PollQRCodeStatus until user scans and confirms
  4. On success, receive bot_token + ilink_bot_id + ilink_user_id + baseurl

Index

Constants

This section is empty.

Variables

View Source
var ErrTokenExpired = fmt.Errorf("wechat bot token expired")

ErrTokenExpired indicates the bot token has expired and a re-login is required.

Functions

func BuildCDNDownloadURL

func BuildCDNDownloadURL(encryptQueryParam string) string

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

func NewAdapter(botToken, ilinkBotID string) *Adapter

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

func (a *Adapter) HandleURLVerification(c *gin.Context) bool

HandleURLVerification is not applicable for WeChat.

func (*Adapter) ParseCallback

func (a *Adapter) ParseCallback(c *gin.Context) (*im.IncomingMessage, error)

ParseCallback is not supported — messages arrive via long-polling.

func (*Adapter) Platform

func (a *Adapter) Platform() im.Platform

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

func (a *Adapter) SendTyping(ctx context.Context, incoming *im.IncomingMessage) error

SendTyping sends a typing indicator to the user.

func (*Adapter) VerifyCallback

func (a *Adapter) VerifyCallback(c *gin.Context) error

VerifyCallback is not supported — WeChat iLink uses long-polling, not webhooks.

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.

func (*LongPollClient) Start

func (c *LongPollClient) Start(ctx context.Context) error

Start begins the long-polling loop. It reconnects automatically on transient errors. Returns ErrTokenExpired when the bot token expires (errcode -14).

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"

Jump to

Keyboard shortcuts

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