wechat

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package wechat provides wechat api integration

Index

Constants

This section is empty.

Variables

View Source
var (
	// 登录相关错误
	ErrNotLoggedIn     = fmt.Errorf("微信未登录")
	ErrSessionExpired  = fmt.Errorf("微信会话已过期")
	ErrLoginFailed     = fmt.Errorf("微信登录失败")
	ErrNeedScanQRCode  = fmt.Errorf("需要扫码登录")
	ErrNeedPushConfirm = fmt.Errorf("需要在手机上确认登录")

	// 消息相关错误
	ErrMessageNotFound   = fmt.Errorf("消息不存在")
	ErrMessageTooLong    = fmt.Errorf("消息过长")
	ErrMessageSendFailed = fmt.Errorf("消息发送失败")
	ErrNoUnreadMessages  = fmt.Errorf("没有未读消息")

	// 联系人相关错误
	ErrContactNotFound  = fmt.Errorf("联系人不存在")
	ErrInvalidContactID = fmt.Errorf("无效的联系人ID")

	// 网络相关错误
	ErrNetworkError      = fmt.Errorf("网络连接失败")
	ErrWechatServerError = fmt.Errorf("微信服务器错误")
	ErrRateLimited       = fmt.Errorf("操作频率过高,请稍后重试")

	// 配置相关错误
	ErrConfigNotFound = fmt.Errorf("配置文件不存在")
	ErrInvalidConfig  = fmt.Errorf("无效的配置")
	ErrStorageError   = fmt.Errorf("存储错误")

	// 权限相关错误
	ErrPermissionDenied    = fmt.Errorf("权限不足")
	ErrOperationNotAllowed = fmt.Errorf("操作不被允许")
)

定义清晰的错误类型

Functions

func ErrorCode

func ErrorCode(err error) string

ErrorCode 获取错误代码(用于AI判断错误类型)

func GetDefaultConfigPath

func GetDefaultConfigPath() string

GetDefaultConfigPath 获取默认配置文件路径

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError 判断错误是否可重试

func SaveConfig

func SaveConfig(config *Config, path string) error

SaveConfig 保存配置到文件

func ShouldReLoginError

func ShouldReLoginError(err error) bool

ShouldReLoginError 判断错误是否需要重新登录

Types

type Bot

type Bot struct {
	// contains filtered or unexported fields
}

Bot 微信机器人封装

func NewBot

func NewBot(config *Config) (*Bot, error)

NewBot 创建新的微信机器人

func (*Bot) Block

func (b *Bot) Block(ctx context.Context) error

Block 阻塞等待消息

func (*Bot) Close

func (b *Bot) Close() error

Close 关闭资源

func (*Bot) GetCurrentUser

func (b *Bot) GetCurrentUser(ctx context.Context) (*openwechat.Self, error)

GetCurrentUser 获取当前用户信息

func (*Bot) GetFriends

func (b *Bot) GetFriends(ctx context.Context, refresh bool) (openwechat.Friends, error)

GetFriends 获取好友列表

func (*Bot) GetGroups

func (b *Bot) GetGroups(ctx context.Context, refresh bool) (openwechat.Groups, error)

GetGroups 获取群组列表

func (*Bot) GetMps

func (b *Bot) GetMps(ctx context.Context, refresh bool) (openwechat.Mps, error)

GetMps 获取公众号列表

func (*Bot) IsLoggedIn

func (b *Bot) IsLoggedIn() bool

IsLoggedIn 检查是否已登录

func (*Bot) Logout

func (b *Bot) Logout(ctx context.Context) error

Logout 退出登录

func (*Bot) SendText

func (b *Bot) SendText(ctx context.Context, to, content string) error

SendText 发送文本消息

func (*Bot) SmartLogin

func (b *Bot) SmartLogin(ctx context.Context) error

SmartLogin 智能登录:尝试最优登录方式

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 微信客户端API

func NewClient

func NewClient(config *Config) (*Client, error)

NewClient 创建新的微信客户端

func NewClientFromConfig

func NewClientFromConfig() (*Client, error)

NewClientFromConfig 从默认配置文件创建客户端

func (*Client) Close

func (c *Client) Close() error

Close 关闭客户端

func (*Client) GetFriends

func (c *Client) GetFriends(ctx context.Context) ([]map[string]string, error)

GetFriends 获取好友列表

func (*Client) GetGroups

func (c *Client) GetGroups(ctx context.Context) ([]map[string]string, error)

GetGroups 获取群组列表

func (*Client) GetMessage

func (c *Client) GetMessage(ctx context.Context, messageID string) (*Message, error)

GetMessage 获取单条消息

func (*Client) GetMessages

func (c *Client) GetMessages(ctx context.Context, unread bool, limit int) ([]Message, error)

GetMessages 获取消息

func (*Client) Login

func (c *Client) Login(ctx context.Context) error

Login 登录微信

func (*Client) Logout

func (c *Client) Logout(ctx context.Context) error

Logout 退出登录

func (*Client) MarkAsRead

func (c *Client) MarkAsRead(ctx context.Context, messageID string) error

MarkAsRead 标记消息为已读

func (*Client) ReplyMessage

func (c *Client) ReplyMessage(ctx context.Context, messageID, content string) error

ReplyMessage 回复消息

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, to, content string) error

SendMessage 发送消息

func (*Client) Status

func (c *Client) Status(ctx context.Context) (map[string]any, error)

Status 获取状态

type Config

type Config struct {
	// 账号配置
	Account string `json:"account"` // 微信账号标识
	Mode    string `json:"mode"`    // "desktop" 或 "web"

	// 存储配置
	DBPath string `json:"db_path"` // 数据库路径

	// 登录配置
	AutoLogin bool `json:"auto_login"` // 是否自动尝试热登录
	PushLogin bool `json:"push_login"` // 是否优先使用免扫码登录

	// 消息配置
	ReplyDelay   int `json:"reply_delay"`    // 回复延迟(毫秒)
	MaxMsgLength int `json:"max_msg_length"` // 最大消息长度

	// 安全配置
	AllowedFriends []string `json:"allowed_friends"` // 白名单好友
	BlockedUsers   []string `json:"blocked_users"`   // 黑名单用户
}

Config 微信客户端配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig 从文件加载配置

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证配置

type Message

type Message struct {
	ID        string    `json:"id"`
	WxMsgID   string    `json:"wx_msg_id"`
	Direction string    `json:"direction"` // "incoming" 或 "outgoing"
	From      string    `json:"from"`
	To        string    `json:"to"`
	Content   string    `json:"content"`
	Type      string    `json:"type"`   // "text", "image", "voice", etc.
	Status    string    `json:"status"` // "unread", "read", "replied", "archived"
	CreatedAt time.Time `json:"created_at"`
	RepliedAt time.Time `json:"replied_at"`
}

Message 微信消息结构

func ConvertOpenWechatMessage

func ConvertOpenWechatMessage(ctx context.Context, wxMsg *openwechat.Message, direction string) (*Message, error)

ConvertOpenWechatMessage 转换openwechat消息到内部格式

type MessageFormatter

type MessageFormatter struct {
	// contains filtered or unexported fields
}

MessageFormatter 消息格式化器

func NewMessageFormatter

func NewMessageFormatter(format OutputFormat) *MessageFormatter

NewMessageFormatter 创建消息格式化器

func (*MessageFormatter) FormatMessageDetail

func (f *MessageFormatter) FormatMessageDetail(msg *Message) string

FormatMessageDetail 格式化单条消息详情

func (*MessageFormatter) FormatMessages

func (f *MessageFormatter) FormatMessages(messages []Message) string

FormatMessages 格式化消息列表

type MessageManager

type MessageManager struct {
	// contains filtered or unexported fields
}

MessageManager 消息管理器

func NewMessageManager

func NewMessageManager(db *sql.DB, bot *Bot) *MessageManager

NewMessageManager 创建消息管理器

func (*MessageManager) GetMessageByID

func (m *MessageManager) GetMessageByID(ctx context.Context, messageID string) (*Message, error)

GetMessageByID 根据ID获取消息

func (*MessageManager) GetRecentMessages

func (m *MessageManager) GetRecentMessages(ctx context.Context, limit int) ([]Message, error)

GetRecentMessages 获取最近消息

func (*MessageManager) GetUnreadMessages

func (m *MessageManager) GetUnreadMessages(ctx context.Context, limit int) ([]Message, error)

GetUnreadMessages 获取未读消息

func (*MessageManager) MarkAsRead

func (m *MessageManager) MarkAsRead(ctx context.Context, messageID string) error

MarkAsRead 标记消息为已读

func (*MessageManager) MarkAsReplied

func (m *MessageManager) MarkAsReplied(ctx context.Context, messageID, replyContent string) error

MarkAsReplied 标记消息为已回复

func (*MessageManager) SaveMessage

func (m *MessageManager) SaveMessage(ctx context.Context, msg *Message) error

SaveMessage 保存消息到数据库

type OutputFormat

type OutputFormat string

OutputFormat 输出格式类型

const (
	FormatSimple   OutputFormat = "simple"
	FormatTable    OutputFormat = "table"
	FormatMarkdown OutputFormat = "markdown"
	FormatOrg      OutputFormat = "org"
)

type SQLiteHotReloadStorage

type SQLiteHotReloadStorage struct {
	// contains filtered or unexported fields
}

SQLiteHotReloadStorage 实现 openwechat.HotReloadStorage 接口

func NewSQLiteHotReloadStorage

func NewSQLiteHotReloadStorage(dbPath, account string) (*SQLiteHotReloadStorage, error)

NewSQLiteHotReloadStorage 创建SQLite热存储

func (*SQLiteHotReloadStorage) Close

func (s *SQLiteHotReloadStorage) Close() error

Close 实现 io.Closer 接口

func (*SQLiteHotReloadStorage) Read

func (s *SQLiteHotReloadStorage) Read(p []byte) (n int, err error)

Read 实现 io.Reader 接口

func (*SQLiteHotReloadStorage) Write

func (s *SQLiteHotReloadStorage) Write(p []byte) (n int, err error)

Write 实现 io.Writer 接口

Source Files

  • api.go
  • bot.go
  • config.go
  • errors.go
  • formatter.go
  • message.go
  • storage.go
  • wechat.go

Jump to

Keyboard shortcuts

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