wecom

package module
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 20 Imported by: 0

README

WeCom SDK

本项目基于github.com/esap/wechat精简,专用于企业微信收发文本消息。

快速开始

package main

import (
	"net/http"

	"github.com/zs5460/wecom" // 微信SDK包
)

func main() {
	
	cfg := &wecom.WxConfig{
		Token:          "yourToken",
		AppId:          "yourAppID",
		AgentId:        "yourAgentId",
		Secret:         "yourSecret",
		EncodingAESKey: "yourEncodingAesKey",
	}

	app := wecom.New(cfg)

    // 主动推送消息
	app.SendText("@all", "Hello,World!")
	
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// 回复消息
		ctx := app.VerifyURL(w, r)
		ctx.NewText("消息已收到:" + ctx.Msg.Content + ",处理中...").Reply()
	})
	
	http.ListenAndServe(":8080", nil)
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// 调试模式,开启后会打印调试信息
	Debug bool = false
)

Functions

func SetTimeOut

func SetTimeOut(d time.Duration)

SetTimeOut 设置全局请求超时

Types

type Server

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

Server 微信服务容器

func New

func New(wc *WxConfig) *Server

New 新建微信服务容器

func (*Server) AddMsg

func (s *Server) AddMsg(v any)

AddMsg 添加队列消息

func (*Server) NewText

func (s *Server) NewText(to string, msg ...string) Text

NewText Text 文本消息

func (*Server) SendMsg

func (s *Server) SendMsg(v any) *WxErr

SendMsg 发送消息

func (*Server) SendText

func (s *Server) SendText(to, msg string) *WxErr

SendText 发送文本消息,过长时按2000长度自动拆分

func (*Server) VerifyURL

func (s *Server) VerifyURL(w http.ResponseWriter, r *http.Request) (ctx *context)

VerifyURL 验证URL,验证成功则返回标准请求载体(Msg已解密)

type Text

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

Text 文本消息

type WxConfig

type WxConfig struct {
	AppId          string
	Token          string
	Secret         string
	EncodingAESKey string
	AgentId        int
	AppName        string
	DataFormat     string // 数据格式:JSON、XML
}

WxConfig 配置,用于New()

type WxErr

type WxErr struct {
	ErrCode int
	ErrMsg  string
}

WxErr 通用错误

func (*WxErr) Error

func (w *WxErr) Error() error

Error 返回错误信息,如果没有错误则返回 nil

type WxMsg

type WxMsg struct {
	XMLName      xml.Name `xml:"xml"`
	ToUserName   string
	FromUserName string
	CreateTime   int64
	MsgId        int64
	MsgType      string
	Content      string // text
	AgentID      int    // corp
}

WxMsg 混合用户消息,业务判断的主体

type WxMsgEnc

type WxMsgEnc struct {
	XMLName    xml.Name `xml:"xml"`
	ToUserName string
	AgentID    int
	Encrypt    string
	AgentType  string
}

WxMsgEnc 加密的用户消息

Jump to

Keyboard shortcuts

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