go wecom
v0.10.1
Wechat Work/Wecom/企业微信 Golang SDK
package wecom_test
import (
"fmt"
"os"
"github.com/wenerme/go-req"
"github.com/wenerme/go-wecom/wecom"
)
func ExampleNewClient() {
// token store - 默认内存 Map - 可以使用数据库实现
store := &wecom.SyncMapStore{}
// 加载缓存 - 复用之前的 Token
if bytes, err := os.ReadFile("wecom-cache.json"); err == nil {
_ = store.Restore(bytes)
}
// 当 Token 变化时生成缓存文件
store.OnChange = func(s *wecom.SyncMapStore) {
_ = os.WriteFile("wecom-cache.json", s.Dump(), 0o600)
}
client := wecom.NewClient(wecom.Conf{
CorpID: "",
AgentID: 0,
CorpSecret: "",
// 不配置默认使用 内存缓存
TokenProvider: &wecom.TokenCache{
Store: store,
},
})
// 访问接口会自动获取或使用当前缓存
token, err := client.AccessToken()
if err != nil {
panic(err)
}
fmt.Println("Token", token)
ticket, err := client.JsAPITicket()
if err != nil {
panic(err)
}
fmt.Println("Ticket", ticket)
// 访问没有实现的接口
dto := wecom.IPListResponse{}
err = client.Request.With(req.Request{
URL: "/cgi-bin/get_api_domain_ip",
Options: []interface{}{
// 如果不需要 access_token
// wecom.WithoutAccessToken,
},
}).Fetch(&dto)
if err != nil {
panic(err)
}
fmt.Println("response", dto)
}
wecom.WebhookSend(&wecom.WebhookSendRequest{
Key: "KEY",
Content: wecom.SendTextContent{Content: "Hello"},
// debug for test only
Request: req.Request{
Options: []interface{}{req.DebugHook(&req.DebugOptions{
Body: true,
})},
},
})
client := wecom.NewClient(wecom.Conf{
CorpID: "",
ProviderSecret: "",
AuthCorpID: "",
AuthCorpPermanentCode: "",
SuiteID: "",
SuiteSecret: "",
SuiteTicket: "",
})
Note
- 会话存档保存 5 天
- 从 Sequence+1 拉取,不包含 Sequence
- limit 最大 1000
- 图片 jpg, 音频 amr, 视频 mp4
- MediaData 的 MD5 可能匹配不上,可以一直重试
- MediaData 单次最多返回 512K
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem
命令行工具
make bin
# 配置
cp .env.example .env
# extract lib
./bin/wwfinance-libs
LD_LIBRARY_PATH=/tmp/wwf/libs ./bin/wwfinance-poller
# Docker 运行
docker run --rm -it -v $PWD/.env:/app/.env -v $PWD/data:/app/data wener/go-wecom
# 开发测试
LD_LIBRARY_PATH=$PWD/WeWorkFinanceSDK/libs go run ./cmd/wwfinance-poller/main.go
代码调用
package main
import (
"fmt"
dotenv "github.com/joho/godotenv"
"github.com/wenerme/go-wecom/WeWorkFinanceSDK"
)
func main() {
_ = dotenv.Load()
client, err := WeWorkFinanceSDK.NewClientFromEnv()
if err != nil {
panic(err)
}
data, err := client.GetChatData(WeWorkFinanceSDK.GetChatDataOptions{
Limit: 10,
Timeout: 5,
})
if err != nil {
panic(err)
}
for _, v := range data {
fmt.Println(v.Message)
}
}