Documentation
¶
Overview ¶
package weixin provides a small Go client for the ilink Weixin QR-login flow used by the OpenClaw Weixin plugin.
Minimal usage:
client := weixin.NewClient(weixin.Options{})
account, err := client.LoginInteractive(ctx, weixin.InteractiveLoginOptions{
Output: os.Stdout,
SaveDir: ".weixin-accounts",
})
if err != nil {
log.Fatal(err)
}
The returned account contains the ilink bot token and account identifiers that can be reused by your own application.
Index ¶
- Constants
- func AESECBPaddedSize(plaintextSize int64) int64
- func AssertSessionActive(accountID string) error
- func BodyFromItemList(items []MessageItem) string
- func BuildCDNDownloadURL(encryptedQueryParam, cdnBaseURL string) string
- func BuildCDNUploadURL(cdnBaseURL, uploadParam, fileKey string) string
- func DecryptAESECB(ciphertext, key []byte) ([]byte, error)
- func DownloadAndDecryptBuffer(ctx context.Context, httpClient *http.Client, ...) ([]byte, error)
- func DownloadPlainCDNBuffer(ctx context.Context, httpClient *http.Client, ...) ([]byte, error)
- func DownloadRemoteMediaToTemp(ctx context.Context, httpClient *http.Client, rawURL, destDir string) (string, error)
- func EncryptAESECB(plaintext, key []byte) ([]byte, error)
- func ExtensionFromContentTypeOrURL(contentType, rawURL string) string
- func ExtensionFromMIME(mimeType string) string
- func GenerateID(prefix string) string
- func GetContextToken(accountID, userID string) string
- func IsMediaItem(item MessageItem) bool
- func IsSessionPaused(accountID string) bool
- func LoadSyncBuffer(filePath string) (string, error)
- func MIMEFromFilename(filename string) string
- func MarkdownToPlainText(text string) string
- func Monitor(ctx context.Context, opts MonitorOptions) error
- func PauseSession(accountID string)
- func PrintQRCode(w io.Writer, content string) error
- func RemainingPause(accountID string) time.Duration
- func ResolveStateDir() string
- func SaveAccount(dir string, account *Account) (string, error)
- func SaveSyncBuffer(filePath, getUpdatesBuf string) error
- func SetContextToken(accountID, userID, token string)
- func SyncBufFilePath(stateDir, accountID string) string
- func TempFileName(prefix, ext string) string
- func UploadBufferToCDN(ctx context.Context, httpClient *http.Client, buf []byte, ...) (string, error)
- type APIClient
- func (c *APIClient) BuildBaseInfo() BaseInfo
- func (c *APIClient) GetConfig(ctx context.Context, ilinkUserID, contextToken string, timeout time.Duration) (*GetConfigResponse, error)
- func (c *APIClient) GetUpdates(ctx context.Context, req GetUpdatesRequest, timeout time.Duration) (*GetUpdatesResponse, error)
- func (c *APIClient) GetUploadURL(ctx context.Context, req GetUploadURLRequest, timeout time.Duration) (*GetUploadURLResponse, error)
- func (c *APIClient) SendMessage(ctx context.Context, req SendMessageRequest, timeout time.Duration) error
- func (c *APIClient) SendTyping(ctx context.Context, req SendTypingRequest, timeout time.Duration) error
- type APIOptions
- type Account
- type BaseInfo
- type CDNMedia
- type CachedConfig
- type Client
- func (c *Client) LoginInteractive(ctx context.Context, opts InteractiveLoginOptions) (*Account, error)
- func (c *Client) StartLogin(ctx context.Context, accountHint string) (*LoginSession, error)
- func (c *Client) WaitLogin(ctx context.Context, session *LoginSession, opts WaitOptions) (*Account, error)
- type ConfigManager
- type Conversation
- func (c *Conversation) SendFile(ctx context.Context, text, fileName string, uploaded UploadedFileInfo) (string, error)
- func (c *Conversation) SendImage(ctx context.Context, text string, uploaded UploadedFileInfo) (string, error)
- func (c *Conversation) SendMediaFile(ctx context.Context, filePath, text string) (string, error)
- func (c *Conversation) SendText(ctx context.Context, text string) (string, error)
- func (c *Conversation) SendVideo(ctx context.Context, text string, uploaded UploadedFileInfo) (string, error)
- type FileItem
- type GetConfigResponse
- type GetUpdatesRequest
- type GetUpdatesResponse
- type GetUploadURLRequest
- type GetUploadURLResponse
- type ImageItem
- type InboundMediaOptions
- type InteractiveLoginOptions
- type LoginSession
- type MessageAPI
- type MessageContext
- type MessageItem
- type MessageSender
- type MonitorOptions
- type Options
- type RefMessage
- type SaveMediaFunc
- type SendMessageRequest
- type SendTypingRequest
- type SendTypingResponse
- type Sender
- type SenderOptions
- type SilkToWAVFunc
- type Target
- type TextItem
- type UploadedFileInfo
- func UploadFileAttachmentToWeixin(ctx context.Context, filePath, toUserID, cdnBaseURL string, apiOpts APIOptions) (*UploadedFileInfo, error)
- func UploadFileToWeixin(ctx context.Context, filePath, toUserID, cdnBaseURL string, apiOpts APIOptions) (*UploadedFileInfo, error)
- func UploadVideoToWeixin(ctx context.Context, filePath, toUserID, cdnBaseURL string, apiOpts APIOptions) (*UploadedFileInfo, error)
- type VideoItem
- type VoiceItem
- type WaitOptions
- type WeixinMessage
Constants ¶
View Source
const ( DefaultBaseURL = "https://ilinkai.weixin.qq.com" DefaultBotType = "3" DefaultQRSessionTTL = 5 * time.Minute DefaultQRLongPollTimeout = 35 * time.Second DefaultLoginTimeout = 8 * time.Minute DefaultPollInterval = time.Second DefaultMaxQRRefresh = 3 )
View Source
const ( UploadMediaTypeImage = 1 UploadMediaTypeVideo = 2 UploadMediaTypeFile = 3 UploadMediaTypeVoice = 4 )
View Source
const ( MessageTypeNone = 0 MessageTypeUser = 1 MessageTypeBot = 2 )
View Source
const ( MessageItemTypeNone = 0 MessageItemTypeText = 1 MessageItemTypeImage = 2 MessageItemTypeVoice = 3 MessageItemTypeFile = 4 MessageItemTypeVideo = 5 )
View Source
const ( MessageStateNew = 0 MessageStateGenerating = 1 MessageStateFinish = 2 )
View Source
const ( TypingStatusTyping = 1 TypingStatusCancel = 2 )
View Source
const (
SessionExpiredErrCode = -14
)
Variables ¶
This section is empty.
Functions ¶
func AESECBPaddedSize ¶
func AssertSessionActive ¶
func BodyFromItemList ¶
func BodyFromItemList(items []MessageItem) string
func BuildCDNDownloadURL ¶
func BuildCDNUploadURL ¶
func DecryptAESECB ¶
func DownloadPlainCDNBuffer ¶
func EncryptAESECB ¶
func ExtensionFromMIME ¶
func GenerateID ¶
func GetContextToken ¶
func IsMediaItem ¶
func IsMediaItem(item MessageItem) bool
func IsSessionPaused ¶
func LoadSyncBuffer ¶
func MIMEFromFilename ¶
func MarkdownToPlainText ¶
func PauseSession ¶
func PauseSession(accountID string)
func RemainingPause ¶
func ResolveStateDir ¶
func ResolveStateDir() string
func SaveSyncBuffer ¶
func SetContextToken ¶
func SetContextToken(accountID, userID, token string)
func SyncBufFilePath ¶
func TempFileName ¶
Types ¶
type APIClient ¶
type APIClient struct {
// contains filtered or unexported fields
}
func NewAPIClient ¶
func NewAPIClient(opts APIOptions) *APIClient
func (*APIClient) BuildBaseInfo ¶
func (*APIClient) GetUpdates ¶
func (c *APIClient) GetUpdates(ctx context.Context, req GetUpdatesRequest, timeout time.Duration) (*GetUpdatesResponse, error)
func (*APIClient) GetUploadURL ¶
func (c *APIClient) GetUploadURL(ctx context.Context, req GetUploadURLRequest, timeout time.Duration) (*GetUploadURLResponse, error)
func (*APIClient) SendMessage ¶
func (*APIClient) SendTyping ¶
type APIOptions ¶
type Account ¶
type Account struct {
AccountID string `json:"account_id"`
BotToken string `json:"bot_token"`
BaseURL string `json:"base_url,omitempty"`
UserID string `json:"user_id,omitempty"`
SavedAt string `json:"saved_at,omitempty"`
}
func ListAccounts ¶
func LoadAccount ¶
type CachedConfig ¶
type CachedConfig struct {
TypingTicket string
}
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) LoginInteractive ¶
func (*Client) StartLogin ¶
func (*Client) WaitLogin ¶
func (c *Client) WaitLogin(ctx context.Context, session *LoginSession, opts WaitOptions) (*Account, error)
type ConfigManager ¶
type ConfigManager struct {
// contains filtered or unexported fields
}
func NewConfigManager ¶
func NewConfigManager(api *APIClient) *ConfigManager
func (*ConfigManager) GetForUser ¶
func (m *ConfigManager) GetForUser(ctx context.Context, userID, contextToken string) (CachedConfig, error)
type Conversation ¶ added in v0.0.2
type Conversation struct {
// contains filtered or unexported fields
}
func (*Conversation) SendFile ¶ added in v0.0.2
func (c *Conversation) SendFile(ctx context.Context, text, fileName string, uploaded UploadedFileInfo) (string, error)
func (*Conversation) SendImage ¶ added in v0.0.2
func (c *Conversation) SendImage(ctx context.Context, text string, uploaded UploadedFileInfo) (string, error)
func (*Conversation) SendMediaFile ¶ added in v0.0.2
func (*Conversation) SendVideo ¶ added in v0.0.2
func (c *Conversation) SendVideo(ctx context.Context, text string, uploaded UploadedFileInfo) (string, error)
type GetConfigResponse ¶
type GetUpdatesRequest ¶
type GetUpdatesResponse ¶
type GetUpdatesResponse struct {
Ret int `json:"ret,omitempty"`
ErrCode int `json:"errcode,omitempty"`
ErrMsg string `json:"errmsg,omitempty"`
Messages []WeixinMessage `json:"msgs,omitempty"`
SyncBuf string `json:"sync_buf,omitempty"`
GetUpdatesBuf string `json:"get_updates_buf,omitempty"`
LongPollingTimeoutMS int `json:"longpolling_timeout_ms,omitempty"`
}
type GetUploadURLRequest ¶
type GetUploadURLRequest struct {
FileKey string `json:"filekey,omitempty"`
MediaType int `json:"media_type,omitempty"`
ToUserID string `json:"to_user_id,omitempty"`
RawSize int64 `json:"rawsize,omitempty"`
RawFileMD5 string `json:"rawfilemd5,omitempty"`
FileSize int64 `json:"filesize,omitempty"`
ThumbRawSize int64 `json:"thumb_rawsize,omitempty"`
ThumbRawFileMD5 string `json:"thumb_rawfilemd5,omitempty"`
ThumbFileSize int64 `json:"thumb_filesize,omitempty"`
NoNeedThumb bool `json:"no_need_thumb,omitempty"`
AESKey string `json:"aeskey,omitempty"`
}
type GetUploadURLResponse ¶
type ImageItem ¶
type ImageItem struct {
Media *CDNMedia `json:"media,omitempty"`
ThumbMedia *CDNMedia `json:"thumb_media,omitempty"`
AESKeyHex string `json:"aeskey,omitempty"`
URL string `json:"url,omitempty"`
MidSize int64 `json:"mid_size,omitempty"`
ThumbSize int64 `json:"thumb_size,omitempty"`
ThumbHeight int64 `json:"thumb_height,omitempty"`
ThumbWidth int64 `json:"thumb_width,omitempty"`
HDSize int64 `json:"hd_size,omitempty"`
}
type InboundMediaOptions ¶
type InboundMediaOptions struct {
DecryptedPicPath string
DecryptedVoicePath string
VoiceMediaType string
DecryptedFilePath string
FileMediaType string
DecryptedVideoPath string
}
func DownloadMediaFromItem ¶
func DownloadMediaFromItem(ctx context.Context, item MessageItem, cdnBaseURL string, httpClient *http.Client, saveMedia SaveMediaFunc, silkToWAV SilkToWAVFunc) (*InboundMediaOptions, error)
type InteractiveLoginOptions ¶
type LoginSession ¶
type MessageAPI ¶ added in v0.0.2
type MessageAPI interface {
SendMessage(ctx context.Context, req SendMessageRequest, timeout time.Duration) error
GetUploadURL(ctx context.Context, req GetUploadURLRequest, timeout time.Duration) (*GetUploadURLResponse, error)
}
type MessageContext ¶
type MessageContext struct {
Body string
From string
To string
AccountID string
OriginatingTo string
MessageSID string
Timestamp int64
Provider string
ChatType string
SessionKey string
ContextToken string
MediaURL string
MediaPath string
MediaType string
CommandBody string
CommandAuthorized bool
}
func WeixinMessageToContext ¶
func WeixinMessageToContext(msg WeixinMessage, accountID string, opts *InboundMediaOptions) MessageContext
type MessageItem ¶
type MessageItem struct {
Type int `json:"type,omitempty"`
CreateTimeMS int64 `json:"create_time_ms,omitempty"`
UpdateTimeMS int64 `json:"update_time_ms,omitempty"`
IsCompleted bool `json:"is_completed,omitempty"`
MsgID string `json:"msg_id,omitempty"`
RefMessage *RefMessage `json:"ref_msg,omitempty"`
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"`
}
type MessageSender ¶ added in v0.0.2
type MessageSender interface {
SendText(ctx context.Context, text string) (string, error)
SendImage(ctx context.Context, text string, uploaded UploadedFileInfo) (string, error)
SendVideo(ctx context.Context, text string, uploaded UploadedFileInfo) (string, error)
SendFile(ctx context.Context, text, fileName string, uploaded UploadedFileInfo) (string, error)
SendMediaFile(ctx context.Context, filePath, text string) (string, error)
}
type MonitorOptions ¶
type RefMessage ¶
type RefMessage struct {
MessageItem *MessageItem `json:"message_item,omitempty"`
Title string `json:"title,omitempty"`
}
type SaveMediaFunc ¶
type SaveMediaFunc func(buffer []byte, contentType, subdir string, maxBytes int64, originalFilename string) (string, error)
func SaveMediaToDir ¶
func SaveMediaToDir(rootDir string) SaveMediaFunc
type SendMessageRequest ¶
type SendMessageRequest struct {
Message *WeixinMessage `json:"msg,omitempty"`
}
type SendTypingRequest ¶
type SendTypingResponse ¶
type Sender ¶ added in v0.0.2
type Sender struct {
// contains filtered or unexported fields
}
func NewSender ¶ added in v0.0.2
func NewSender(opts SenderOptions) *Sender
func (*Sender) Conversation ¶ added in v0.0.2
func (s *Sender) Conversation(target Target) *Conversation
type SenderOptions ¶ added in v0.0.2
type SilkToWAVFunc ¶
type UploadedFileInfo ¶
type UploadedFileInfo struct {
FileKey string
DownloadEncryptedQueryParam string
AESKeyHex string
FileSize int64
FileSizeCiphertext int64
}
func UploadFileAttachmentToWeixin ¶
func UploadFileAttachmentToWeixin(ctx context.Context, filePath, toUserID, cdnBaseURL string, apiOpts APIOptions) (*UploadedFileInfo, error)
func UploadFileToWeixin ¶
func UploadFileToWeixin(ctx context.Context, filePath, toUserID, cdnBaseURL string, apiOpts APIOptions) (*UploadedFileInfo, error)
func UploadVideoToWeixin ¶
func UploadVideoToWeixin(ctx context.Context, filePath, toUserID, cdnBaseURL string, apiOpts APIOptions) (*UploadedFileInfo, error)
type VideoItem ¶
type VideoItem struct {
Media *CDNMedia `json:"media,omitempty"`
VideoSize int64 `json:"video_size,omitempty"`
PlayLength int64 `json:"play_length,omitempty"`
VideoMD5 string `json:"video_md5,omitempty"`
ThumbMedia *CDNMedia `json:"thumb_media,omitempty"`
ThumbSize int64 `json:"thumb_size,omitempty"`
ThumbHeight int64 `json:"thumb_height,omitempty"`
ThumbWidth int64 `json:"thumb_width,omitempty"`
}
type WaitOptions ¶
type WeixinMessage ¶
type WeixinMessage struct {
Seq int64 `json:"seq,omitempty"`
MessageID int64 `json:"message_id,omitempty"`
FromUserID string `json:"from_user_id,omitempty"`
ToUserID string `json:"to_user_id,omitempty"`
ClientID string `json:"client_id,omitempty"`
CreateTimeMS int64 `json:"create_time_ms,omitempty"`
UpdateTimeMS int64 `json:"update_time_ms,omitempty"`
DeleteTimeMS int64 `json:"delete_time_ms,omitempty"`
SessionID string `json:"session_id,omitempty"`
GroupID string `json:"group_id,omitempty"`
MessageType int `json:"message_type,omitempty"`
MessageState int `json:"message_state,omitempty"`
ItemList []MessageItem `json:"item_list,omitempty"`
ContextToken string `json:"context_token,omitempty"`
}
Click to show internal directories.
Click to hide internal directories.