Documentation
¶
Overview ¶
Package wecom provides cryptographic utilities for WeChat Work (WeCom) message encryption and decryption. It implements the WXBizMsgCrypt algorithm for secure message handling with WeChat Work APIs.
Index ¶
- Constants
- Variables
- func GetHelpMessage() string
- type AIBotClient
- type ConversationState
- type JsonParse
- type MessageHandler
- type PKCS7Encoder
- type Prpcrypt
- type SHA1
- type Stream
- type UserReq
- type UserResp
- type WXBizJsonMsgCrypt
- func (w *WXBizJsonMsgCrypt) DecryptMsg(sPostData, sMsgSignature, sTimeStamp, sNonce string) (int, string)
- func (w *WXBizJsonMsgCrypt) EncryptMsg(sReplyMsg, sNonce string, timestamp ...string) (int, string)
- func (w *WXBizJsonMsgCrypt) VerifyURL(sMsgSignature, sTimeStamp, sNonce, sEchoStr string) (int, string)
Constants ¶
const ( WXBizMsgCrypt_OK = 0 WXBizMsgCrypt_ValidateSignature_Error = 40001 WXBizMsgCrypt_ParseJson_Error = 40002 WXBizMsgCrypt_ComputeSignature_Error = 40003 WXBizMsgCrypt_IllegalAesKey = 40004 WXBizMsgCrypt_EncryptAES_Error = 40005 WXBizMsgCrypt_DecryptAES_Error = 40006 WXBizMsgCrypt_IllegalBuffer = 40007 WXBizMsgCrypt_ValidateCorpid_Error = 40008 )
Variables ¶
var ErrFormat = errors.New("format error")
Functions ¶
Types ¶
type AIBotClient ¶
type AIBotClient struct {
Token string
EncodingAESKey string
// contains filtered or unexported fields
}
AIBotClient 企业微信智能机器人客户端 API文档: https://developer.work.weixin.qq.com/document/path/100719
func NewAIBotClient ¶
func NewAIBotClient(ctx context.Context, token, encodingAESKey string) (*AIBotClient, error)
NewAIBotClient 创建企业微信AI机器人客户端
func (*AIBotClient) DecryptUserReq ¶
func (c *AIBotClient) DecryptUserReq(signature, timestamp, nonce, msg string) (*UserReq, error)
DecryptUserReq 解密用户请求消息
func (*AIBotClient) MakeStreamResp ¶
func (c *AIBotClient) MakeStreamResp(nonce, id, content string, isFinish bool) (string, error)
MakeStreamResp 生成流式响应消息
type ConversationState ¶
type ConversationState struct {
Question string
Buffer strings.Builder
NotificationChan chan string
IsVisited bool
IsDone bool
Mutex sync.Mutex
}
ConversationState 对话状态
type JsonParse ¶
type JsonParse struct{}
JsonParse 提取/生成 json 消息
type MessageHandler ¶
type MessageHandler struct {
Client *AIBotClient // 导出以便外部访问
// contains filtered or unexported fields
}
MessageHandler 企业微信消息处理器
func NewMessageHandler ¶
NewMessageHandler 创建消息处理器
func (*MessageHandler) HandleStreamRequest ¶
HandleStreamRequest 处理流式轮询请求
func (*MessageHandler) HandleTextMessage ¶
HandleTextMessage 处理文本消息
type PKCS7Encoder ¶
type PKCS7Encoder struct {
BlockSize int // 使用 32 与 Python 示例一致
}
PKCS7Encoder 提供基于 PKCS7 的填充/去填充
func NewPKCS7Encoder ¶
func NewPKCS7Encoder() *PKCS7Encoder
func (*PKCS7Encoder) Encode ¶
func (p *PKCS7Encoder) Encode(src []byte) []byte
type Prpcrypt ¶
Prpcrypt 提供 AES 加解密功能
func NewPrpcrypt ¶
type Stream ¶
type Stream struct {
Id string `json:"id"`
Finish bool `json:"finish"`
Content string `json:"content"`
MsgItem []struct {
Msgtype string `json:"msgtype"`
Image struct {
Base64 string `json:"base64"`
Md5 string `json:"md5"`
} `json:"image"`
} `json:"msg_item"`
}
Stream 流式消息
type UserReq ¶
type UserReq struct {
Msgid string `json:"msgid"`
Aibotid string `json:"aibotid"`
Chattype string `json:"chattype"`
From struct {
Userid string `json:"userid"`
} `json:"from"`
Msgtype string `json:"msgtype"`
Text struct {
Content string `json:"content"`
} `json:"text"`
Stream struct {
Id string `json:"id"`
} `json:"stream"`
}
UserReq 用户请求消息
type WXBizJsonMsgCrypt ¶
type WXBizJsonMsgCrypt struct {
Token string
EncodingKey []byte
ReceiveID string
// contains filtered or unexported fields
}
WXBizJsonMsgCrypt 将整个流程封装:初始化时传入 token, encodingAESKey, receiveID
func NewWXBizJsonMsgCrypt ¶
func NewWXBizJsonMsgCrypt(sToken, sEncodingAESKey, sReceiveID string) (*WXBizJsonMsgCrypt, int, error)
NewWXBizJsonMsgCrypt 构造:sToken, sEncodingAESKey, sReceiveID
func (*WXBizJsonMsgCrypt) DecryptMsg ¶
func (w *WXBizJsonMsgCrypt) DecryptMsg(sPostData, sMsgSignature, sTimeStamp, sNonce string) (int, string)
DecryptMsg 验证签名并解密 POST 的 json 数据包 sPostData: POST 的 json 数据字符串(包含 encrypt 字段) sMsgSignature: URL param msg_signature sTimeStamp: timestamp sNonce: nonce 返回 (code, jsonContent)
func (*WXBizJsonMsgCrypt) EncryptMsg ¶
func (w *WXBizJsonMsgCrypt) EncryptMsg(sReplyMsg, sNonce string, timestamp ...string) (int, string)
EncryptMsg 对要回复的消息 sReplyMsg(json 字符串)进行加密并生成外层 JSON 包装 返回 (code, generatedJson)