wecom

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

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

View Source
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

View Source
var ErrFormat = errors.New("format error")

Functions

func GetHelpMessage

func GetHelpMessage() string

GetHelpMessage 获取帮助信息

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 生成流式响应消息

func (*AIBotClient) VerifyURL

func (c *AIBotClient) VerifyURL(signature, timestamp, nonce, echoStr string) (string, error)

VerifyURL 验证URL(用于配置回调地址)

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 消息

func (*JsonParse) Extract

func (jp *JsonParse) Extract(jsonText string) (int, string)

Extract 从 json 字符串中提取 encrypt 字段 返回 (code, encrypt)

func (*JsonParse) Generate

func (jp *JsonParse) Generate(encrypt, signature, timestamp, nonce string) string

Generate 根据参数生成 json 字符串

type MessageHandler

type MessageHandler struct {
	Client *AIBotClient // 导出以便外部访问
	// contains filtered or unexported fields
}

MessageHandler 企业微信消息处理器

func NewMessageHandler

func NewMessageHandler(cfg *config.Config, mcpServer *imcp.MCPServer) (*MessageHandler, error)

NewMessageHandler 创建消息处理器

func (*MessageHandler) HandleStreamRequest

func (h *MessageHandler) HandleStreamRequest(ctx context.Context, req *UserReq) (string, error)

HandleStreamRequest 处理流式轮询请求

func (*MessageHandler) HandleTextMessage

func (h *MessageHandler) HandleTextMessage(ctx context.Context, req *UserReq) (string, error)

HandleTextMessage 处理文本消息

type PKCS7Encoder

type PKCS7Encoder struct {
	BlockSize int // 使用 32 与 Python 示例一致
}

PKCS7Encoder 提供基于 PKCS7 的填充/去填充

func NewPKCS7Encoder

func NewPKCS7Encoder() *PKCS7Encoder

func (*PKCS7Encoder) Decode

func (p *PKCS7Encoder) Decode(decrypted []byte) ([]byte, error)

func (*PKCS7Encoder) Encode

func (p *PKCS7Encoder) Encode(src []byte) []byte

type Prpcrypt

type Prpcrypt struct {
	Key  []byte
	Mode string
}

Prpcrypt 提供 AES 加解密功能

func NewPrpcrypt

func NewPrpcrypt(key []byte) *Prpcrypt

func (*Prpcrypt) Decrypt

func (pc *Prpcrypt) Decrypt(base64Cipher string, receiveID string) (int, string)

Decrypt 解密 base64 文本,返回 (code, jsonContent)

func (*Prpcrypt) Encrypt

func (pc *Prpcrypt) Encrypt(plainText string, receiveID string) (int, string)

Encrypt 对明文加密,返回 (code, base64Ciphertext)

type SHA1

type SHA1 struct{}

SHA1 负责生成安全签名(sha1)

func (*SHA1) GetSHA1

func (s *SHA1) GetSHA1(token, timestamp, nonce string, encrypt interface{}) (int, string)

GetSHA1 : 对 token, timestamp, nonce, encrypt 排序后 sha1 返回 (code, signature)

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 UserResp

type UserResp struct {
	Msgtype string `json:"msgtype"`
	Stream  Stream `json:"stream"`
}

UserResp 回复消息

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)

func (*WXBizJsonMsgCrypt) VerifyURL

func (w *WXBizJsonMsgCrypt) VerifyURL(sMsgSignature, sTimeStamp, sNonce, sEchoStr string) (int, string)

VerifyURL 校验并解密 sEchoStr(用于首次验证 URL) 返回 (code, sReplyEchoStr)

Jump to

Keyboard shortcuts

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