Documentation
¶
Overview ¶
Package wechatbot provides a WeChat iLink Bot SDK for Go.
It handles QR login, long-poll message receiving, text/media sending, typing indicators, context_token management, and AES-128-ECB CDN crypto.
Index ¶
- type BaseInfo
- type Bot
- func (b *Bot) Download(ctx context.Context, msg *IncomingMessage) (*DownloadedMedia, error)
- func (b *Bot) DownloadRaw(ctx context.Context, media *CDNMedia, aeskeyOverride string) ([]byte, error)
- func (b *Bot) Login(ctx context.Context, force bool) (*Credentials, error)
- func (b *Bot) OnMessage(handler MessageHandler)
- func (b *Bot) Reply(ctx context.Context, msg *IncomingMessage, text string) error
- func (b *Bot) ReplyContent(ctx context.Context, msg *IncomingMessage, content SendContent) error
- func (b *Bot) Run(ctx context.Context) error
- func (b *Bot) Send(ctx context.Context, userID, text string) error
- func (b *Bot) SendMedia(ctx context.Context, userID string, content SendContent) error
- func (b *Bot) SendTyping(ctx context.Context, userID string) error
- func (b *Bot) Stop()
- func (b *Bot) StopTyping(ctx context.Context, userID string) error
- func (b *Bot) Upload(ctx context.Context, data []byte, userID string, mediaType int) (*UploadResult, error)
- type CDNMedia
- type ContentType
- type Credentials
- type DownloadedMedia
- type FileContent
- type FileItem
- type ImageContent
- type ImageItem
- type IncomingMessage
- type MediaType
- type MessageHandler
- type MessageItem
- type MessageItemType
- type MessageState
- type MessageType
- type Options
- type QuotedMessage
- type RefMessage
- type SendContent
- type TextItem
- type UploadResult
- type VideoContent
- type VideoItem
- type VoiceContent
- type VoiceItem
- type WireMessage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseInfo ¶
type BaseInfo struct {
ChannelVersion string `json:"channel_version"`
}
BaseInfo is included in every POST request body.
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
Bot is the main WeChat bot client.
func (*Bot) Download ¶
func (b *Bot) Download(ctx context.Context, msg *IncomingMessage) (*DownloadedMedia, error)
Download downloads media from an incoming message. Returns nil if the message has no media. Priority: image > file > video > voice.
func (*Bot) DownloadRaw ¶
func (b *Bot) DownloadRaw(ctx context.Context, media *CDNMedia, aeskeyOverride string) ([]byte, error)
DownloadRaw downloads and decrypts a raw CDN media reference.
func (*Bot) OnMessage ¶
func (b *Bot) OnMessage(handler MessageHandler)
OnMessage registers a message handler.
func (*Bot) ReplyContent ¶
func (b *Bot) ReplyContent(ctx context.Context, msg *IncomingMessage, content SendContent) error
ReplyContent replies with any content type.
func (*Bot) Run ¶
Run starts the long-poll loop. Blocks until Stop() is called or context is cancelled.
func (*Bot) SendTyping ¶
SendTyping shows the "typing..." indicator.
func (*Bot) StopTyping ¶
StopTyping cancels the "typing..." indicator.
type CDNMedia ¶
type CDNMedia struct {
EncryptQueryParam string `json:"encrypt_query_param"`
AESKey string `json:"aes_key"`
EncryptType int `json:"encrypt_type,omitempty"`
// FullURL is the complete download URL returned by server; when set, use directly.
FullURL string `json:"full_url,omitempty"`
}
CDNMedia references an encrypted file on the WeChat CDN.
type ContentType ¶
type ContentType string
ContentType is the primary type of an incoming message.
const ( ContentText ContentType = "text" ContentImage ContentType = "image" ContentVoice ContentType = "voice" ContentFile ContentType = "file" ContentVideo ContentType = "video" )
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.
type DownloadedMedia ¶
type DownloadedMedia struct {
Data []byte
Type string // "image", "file", "video", "voice"
FileName string
Format string // "silk" for voice
}
DownloadedMedia is the result of downloading media from a message.
type FileContent ¶
FileContent holds parsed file data.
type FileItem ¶
type FileItem struct {
Media *CDNMedia `json:"media,omitempty"`
FileName string `json:"file_name,omitempty"`
MD5 string `json:"md5,omitempty"`
Len string `json:"len,omitempty"`
}
FileItem holds file content.
type ImageContent ¶
type ImageContent struct {
Media *CDNMedia
ThumbMedia *CDNMedia
AESKey string
URL string
Width int
Height int
}
ImageContent holds parsed image data from a message.
type ImageItem ¶
type ImageItem struct {
Media *CDNMedia `json:"media,omitempty"`
ThumbMedia *CDNMedia `json:"thumb_media,omitempty"`
AESKey string `json:"aeskey,omitempty"`
URL string `json:"url,omitempty"`
MidSize int64 `json:"mid_size,omitempty"`
ThumbSize int64 `json:"thumb_size,omitempty"`
ThumbWidth int `json:"thumb_width,omitempty"`
ThumbHeight int `json:"thumb_height,omitempty"`
HDSize int64 `json:"hd_size,omitempty"`
}
ImageItem holds image content and CDN references.
type IncomingMessage ¶
type IncomingMessage struct {
UserID string
Text string
Type ContentType
Timestamp time.Time
Images []ImageContent
Voices []VoiceContent
Files []FileContent
Videos []VideoContent
QuotedMessage *QuotedMessage
Raw *WireMessage
ContextToken string // internal, managed by SDK
}
IncomingMessage is a parsed, user-friendly representation.
type MessageHandler ¶
type MessageHandler func(msg *IncomingMessage)
MessageHandler is called for each incoming user message.
type MessageItem ¶
type MessageItem struct {
Type MessageItemType `json:"type"`
TextItem *TextItem `json:"text_item,omitempty"`
ImageItem *ImageItem `json:"image_item,omitempty"`
VoiceItem *VoiceItem `json:"voice_item,omitempty"`
FileItem *FileItem `json:"file_item,omitempty"`
VideoItem *VideoItem `json:"video_item,omitempty"`
RefMsg *RefMessage `json:"ref_msg,omitempty"`
}
MessageItem is a single content item within a message.
type MessageItemType ¶
type MessageItemType int
MessageItemType indicates the content type of a message item.
const ( ItemText MessageItemType = 1 ItemImage MessageItemType = 2 ItemVoice MessageItemType = 3 ItemFile MessageItemType = 4 ItemVideo MessageItemType = 5 )
type MessageState ¶
type MessageState int
MessageState indicates the message delivery state.
const ( MessageStateNew MessageState = 0 MessageStateGenerating MessageState = 1 MessageStateFinish MessageState = 2 )
type MessageType ¶
type MessageType int
MessageType indicates who sent the message.
const ( MessageTypeUser MessageType = 1 MessageTypeBot MessageType = 2 )
type Options ¶
type Options struct {
BaseURL string
CredPath string
LogLevel string // "debug", "info", "warn", "error", "silent"
OnQRURL func(url string)
OnScanned func()
OnExpired func()
OnError func(err error)
}
Options configures a Bot instance.
type QuotedMessage ¶
type QuotedMessage struct {
Title string
Text string
Type ContentType
}
QuotedMessage represents a referenced message.
type RefMessage ¶
type RefMessage struct {
Title string `json:"title,omitempty"`
MessageItem *MessageItem `json:"message_item,omitempty"`
}
RefMessage represents a quoted/referenced message.
type SendContent ¶
type SendContent struct {
Text string
Image []byte
Video []byte
File []byte
FileName string
Caption string
}
SendContent describes what to send. Use one of:
- SendText("Hello!")
- SendImage(data)
- SendVideo(data)
- SendFile(data, "report.pdf")
func SendFile ¶
func SendFile(data []byte, fileName string) SendContent
SendFile creates a file SendContent.
type UploadResult ¶
UploadResult is the result of uploading media to CDN.
type VideoContent ¶
type VideoContent struct {
Media *CDNMedia
ThumbMedia *CDNMedia
DurationMs int
Width int
Height int
}
VideoContent holds parsed video data.
type VideoItem ¶
type VideoItem struct {
Media *CDNMedia `json:"media,omitempty"`
VideoSize int64 `json:"video_size,omitempty"`
PlayLength int `json:"play_length,omitempty"`
ThumbMedia *CDNMedia `json:"thumb_media,omitempty"`
}
VideoItem holds video content.
type VoiceContent ¶
VoiceContent holds parsed voice data.
type VoiceItem ¶
type VoiceItem struct {
Media *CDNMedia `json:"media,omitempty"`
EncodeType int `json:"encode_type,omitempty"`
Text string `json:"text,omitempty"`
Playtime int `json:"playtime,omitempty"`
}
VoiceItem holds voice 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"`
MessageState MessageState `json:"message_state"`
ContextToken string `json:"context_token"`
ItemList []MessageItem `json:"item_list"`
}
WireMessage is the raw message from the iLink API.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
echo-bot
command
Echo bot example — receives messages and replies with "Echo: <text>".
|
Echo bot example — receives messages and replies with "Echo: <text>". |
|
internal
|
|
|
auth
Package auth handles QR code login and credential persistence.
|
Package auth handles QR code login and credential persistence. |
|
crypto
Package crypto provides AES-128-ECB encryption/decryption for WeChat CDN media.
|
Package crypto provides AES-128-ECB encryption/decryption for WeChat CDN media. |
|
protocol
Package protocol implements the raw iLink Bot API HTTP calls.
|
Package protocol implements the raw iLink Bot API HTTP calls. |