Documentation
¶
Overview ¶
Package wechat provides wechat api integration
Index ¶
- Variables
- func ErrorCode(err error) string
- func GetDefaultConfigPath() string
- func IsRetryableError(err error) bool
- func SaveConfig(config *Config, path string) error
- func ShouldReLoginError(err error) bool
- type Bot
- func (b *Bot) Block(ctx context.Context) error
- func (b *Bot) Close() error
- func (b *Bot) GetCurrentUser(ctx context.Context) (*openwechat.Self, error)
- func (b *Bot) GetFriends(ctx context.Context, refresh bool) (openwechat.Friends, error)
- func (b *Bot) GetGroups(ctx context.Context, refresh bool) (openwechat.Groups, error)
- func (b *Bot) GetMps(ctx context.Context, refresh bool) (openwechat.Mps, error)
- func (b *Bot) IsLoggedIn() bool
- func (b *Bot) Logout(ctx context.Context) error
- func (b *Bot) SendText(ctx context.Context, to, content string) error
- func (b *Bot) SmartLogin(ctx context.Context) error
- type Client
- func (c *Client) Close() error
- func (c *Client) GetFriends(ctx context.Context) ([]map[string]string, error)
- func (c *Client) GetGroups(ctx context.Context) ([]map[string]string, error)
- func (c *Client) GetMessage(ctx context.Context, messageID string) (*Message, error)
- func (c *Client) GetMessages(ctx context.Context, unread bool, limit int) ([]Message, error)
- func (c *Client) Login(ctx context.Context) error
- func (c *Client) Logout(ctx context.Context) error
- func (c *Client) MarkAsRead(ctx context.Context, messageID string) error
- func (c *Client) ReplyMessage(ctx context.Context, messageID, content string) error
- func (c *Client) SendMessage(ctx context.Context, to, content string) error
- func (c *Client) Status(ctx context.Context) (map[string]any, error)
- type Config
- type Message
- type MessageFormatter
- type MessageManager
- func (m *MessageManager) GetMessageByID(ctx context.Context, messageID string) (*Message, error)
- func (m *MessageManager) GetRecentMessages(ctx context.Context, limit int) ([]Message, error)
- func (m *MessageManager) GetUnreadMessages(ctx context.Context, limit int) ([]Message, error)
- func (m *MessageManager) MarkAsRead(ctx context.Context, messageID string) error
- func (m *MessageManager) MarkAsReplied(ctx context.Context, messageID, replyContent string) error
- func (m *MessageManager) SaveMessage(ctx context.Context, msg *Message) error
- type OutputFormat
- type SQLiteHotReloadStorage
Constants ¶
This section is empty.
Variables ¶
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 ¶
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
Bot 微信机器人封装
func (*Bot) GetCurrentUser ¶
GetCurrentUser 获取当前用户信息
func (*Bot) GetFriends ¶
GetFriends 获取好友列表
func (*Bot) GetGroups ¶
GetGroups 获取群组列表
func (*Bot) GetMps ¶
GetMps 获取公众号列表
func (*Bot) SendText ¶
SendText 发送文本消息
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 微信客户端API
func NewClient ¶
NewClient 创建新的微信客户端 NewClient 创建新的微信客户端
func NewClientFromConfig ¶
NewClientFromConfig 从默认配置文件创建客户端
func (*Client) GetFriends ¶
GetFriends 获取好友列表
func (*Client) GetGroups ¶
GetGroups 获取群组列表
func (*Client) GetMessage ¶
GetMessage 获取单条消息
func (*Client) GetMessages ¶
GetMessages 获取消息
func (*Client) MarkAsRead ¶
MarkAsRead 标记消息为已读
func (*Client) ReplyMessage ¶
ReplyMessage 回复消息
func (*Client) SendMessage ¶
SendMessage 发送消息
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 微信客户端配置
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 微信消息结构
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 ¶
GetMessageByID 根据ID获取消息
func (*MessageManager) GetRecentMessages ¶
GetRecentMessages 获取最近消息
func (*MessageManager) GetUnreadMessages ¶
GetUnreadMessages 获取未读消息
func (*MessageManager) MarkAsRead ¶
func (m *MessageManager) MarkAsRead(ctx context.Context, messageID string) error
MarkAsRead 标记消息为已读
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