feat: add DingTalk Stream text ingress
This commit is contained in:
@@ -8,5 +8,12 @@ DOBOTAI_AGENT_BINDINGS={"dingtalk:*":"replace-with-agent-id"}
|
||||
WEKNORA_BASE_URL=http://127.0.0.1:8080
|
||||
WEKNORA_API_KEY=replace-me
|
||||
|
||||
# DingTalk Stream mode (official SDK). Leave all three empty to disable the
|
||||
# DingTalk channel; set all three to enable it. Never commit real values.
|
||||
DINGTALK_CLIENT_ID=
|
||||
DINGTALK_CLIENT_SECRET=
|
||||
# Tenant key this DingTalk ingress serves (matches tenant_key used by QA).
|
||||
DOBOTAI_TENANT_KEY=
|
||||
|
||||
# Independent DobotAI PostgreSQL; never point this at WeKnora's database
|
||||
DATABASE_URL=postgres://dobotai:replace-me@127.0.0.1:5432/dobotai?sslmode=disable
|
||||
|
||||
@@ -15,6 +15,16 @@ Implemented:
|
||||
- external-message idempotency;
|
||||
- independent PostgreSQL session/turn storage;
|
||||
- authenticated internal `POST /v1/qa` ingress endpoint.
|
||||
- DingTalk Stream text ingress (official SDK) wired into `cmd/dobotai`.
|
||||
|
||||
## DingTalk channel (Stream, text only)
|
||||
|
||||
- Uses the official DingTalk Stream SDK (`dingtalk-stream-sdk-go`); no public webhook endpoint is exposed.
|
||||
- Text messages only for now. Images, OpenAPI fallback, and card replies are not implemented yet.
|
||||
- Group chats are handled only when the bot is @-mentioned; other group messages are ignored.
|
||||
- Sessions: direct chats use one long-lived session per user; group chats share one session per group (default `shared` policy).
|
||||
- Replies are sent via the callback's `sessionWebhook`, which is fail-closed: only `https` URLs on `*.dingtalk.com` (port 443, no redirects) are accepted.
|
||||
- Required env vars to enable (all three or none): `DINGTALK_CLIENT_ID`, `DINGTALK_CLIENT_SECRET`, `DOBOTAI_TENANT_KEY`. Partial configuration is a fatal startup error.
|
||||
|
||||
## WeKnora baseline
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/agentbinding"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/channels/dingtalk"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/httpapi"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/qa"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/session"
|
||||
@@ -52,6 +53,39 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
dingCfg := dingtalk.StreamConfig{
|
||||
ClientID: os.Getenv("DINGTALK_CLIENT_ID"),
|
||||
ClientSecret: os.Getenv("DINGTALK_CLIENT_SECRET"),
|
||||
TenantKey: os.Getenv("DOBOTAI_TENANT_KEY"),
|
||||
}
|
||||
if err := dingCfg.Validate(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
var streamClient *dingtalk.StreamClient
|
||||
if dingCfg.Enabled() {
|
||||
sender, err := dingtalk.NewWebhookSender(nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
handler, err := dingtalk.NewHandler(
|
||||
dingtalk.TenantConfig{TenantKey: dingCfg.TenantKey, Channel: "dingtalk"},
|
||||
qaService,
|
||||
sender,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
streamClient, err = dingtalk.NewStreamClient(dingCfg, handler)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := streamClient.Start(ctx); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer streamClient.Close()
|
||||
log.Printf("DingTalk Stream ingress started for tenant %s", dingCfg.TenantKey)
|
||||
}
|
||||
address := strings.TrimSpace(os.Getenv("DOBOTAI_LISTEN_ADDR"))
|
||||
if address == "" {
|
||||
address = "127.0.0.1:8090"
|
||||
|
||||
@@ -56,6 +56,7 @@ No WeKnora source patch is allowed as a workaround.
|
||||
- `internal/qa`: business orchestration and terminal-state ownership.
|
||||
- `internal/session`: external-to-WeKnora session policy.
|
||||
- `internal/store/postgres`: DobotAI business persistence only.
|
||||
- `internal/channels/dingtalk`: DingTalk Stream ingress (official SDK), text-only robot callbacks, fail-closed session webhook replies (`https`, `*.dingtalk.com`, no redirects). Wired in `cmd/dobotai`; enabled only when `DINGTALK_CLIENT_ID`, `DINGTALK_CLIENT_SECRET`, and `DOBOTAI_TENANT_KEY` are all set. Group messages require an @-mention; direct chats map to one long-lived session per user, groups to one shared session per group. Images, OpenAPI fallback, and card replies are not implemented yet.
|
||||
- future `channels/*`: platform callbacks and outbound replies.
|
||||
- future `content-sync/*`: DingTalk KB, NAS, and CMS synchronization.
|
||||
- model gateway: token accounting, routing, quotas, circuit breaking, and provider observability.
|
||||
|
||||
@@ -2,9 +2,14 @@ module gitea.zheromiku.top/Weknora/dobotai-platform
|
||||
|
||||
go 1.25.0
|
||||
|
||||
require github.com/jackc/pgx/v5 v5.9.2
|
||||
require (
|
||||
github.com/jackc/pgx/v5 v5.9.2
|
||||
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
@@ -7,13 +13,20 @@ github.com/jackc/pgx/v5 v5.9.2 h1:3ZhOzMWnR4yJ+RW1XImIPsD1aNSz4T4fyP7zlQb56hw=
|
||||
github.com/jackc/pgx/v5 v5.9.2/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1 h1:Lb/Uzkiw2Ugt2Xf03J5wmv81PdkYOiWbI8CNBi1boC8=
|
||||
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1/go.mod h1:ln3IqPYYocZbYvl9TAOrG/cxGR9xcn4pnZRLdCTEGEU=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
package dingtalk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
)
|
||||
|
||||
const (
|
||||
// msgTypeText is the DingTalk robot callback message type for text.
|
||||
msgTypeText = "text"
|
||||
// conversationTypeDirect is a 1:1 (direct) conversation callback.
|
||||
conversationTypeDirect = "1"
|
||||
// conversationTypeGroup is a group conversation callback.
|
||||
conversationTypeGroup = "2"
|
||||
|
||||
// replyTitle is the markdown title used for session webhook replies.
|
||||
replyTitle = "DobotAI"
|
||||
|
||||
// unsupportedMessageReply is sent for non-text messages.
|
||||
unsupportedMessageReply = "当前版本暂仅支持文字消息。"
|
||||
// qaErrorReply is sent when the QA backend fails. It must never leak
|
||||
// internal error details to end users.
|
||||
qaErrorReply = "抱歉,当前暂时无法完成回答,请稍后重试。"
|
||||
)
|
||||
|
||||
// TenantConfig identifies the tenant + channel this handler serves.
|
||||
type TenantConfig struct {
|
||||
TenantKey string
|
||||
Channel string
|
||||
}
|
||||
|
||||
// BotCallback is the decoded DingTalk robot callback payload for the
|
||||
// fields the text ingress path needs.
|
||||
type BotCallback struct {
|
||||
ConversationType string
|
||||
ConversationID string
|
||||
Msgtype string
|
||||
MsgID string
|
||||
SenderNick string
|
||||
SenderStaffID string
|
||||
SenderID string
|
||||
IsInAtList bool
|
||||
SessionWebhook string
|
||||
Text string
|
||||
}
|
||||
|
||||
// QAAsker answers questions (implemented by the QA service).
|
||||
type QAAsker interface {
|
||||
Ask(ctx context.Context, req core.QARequest) (core.QAResult, error)
|
||||
}
|
||||
|
||||
// MarkdownSender sends a markdown reply to a session webhook
|
||||
// (implemented by WebhookSender).
|
||||
type MarkdownSender interface {
|
||||
SendMarkdown(ctx context.Context, webhookURL, title, text string) error
|
||||
}
|
||||
|
||||
// Handler processes DingTalk robot callbacks synchronously.
|
||||
type Handler struct {
|
||||
cfg TenantConfig
|
||||
qa QAAsker
|
||||
reply MarkdownSender
|
||||
}
|
||||
|
||||
// NewHandler validates its dependencies and returns a Handler.
|
||||
func NewHandler(cfg TenantConfig, qa QAAsker, reply MarkdownSender) (*Handler, error) {
|
||||
if strings.TrimSpace(cfg.TenantKey) == "" {
|
||||
return nil, errors.New("dingtalk handler: tenant key is required")
|
||||
}
|
||||
if strings.TrimSpace(cfg.Channel) == "" {
|
||||
return nil, errors.New("dingtalk handler: channel is required")
|
||||
}
|
||||
if qa == nil {
|
||||
return nil, errors.New("dingtalk handler: QA asker is required")
|
||||
}
|
||||
if reply == nil {
|
||||
return nil, errors.New("dingtalk handler: markdown sender is required")
|
||||
}
|
||||
return &Handler{cfg: cfg, qa: qa, reply: reply}, nil
|
||||
}
|
||||
|
||||
// Handle processes a single bot callback synchronously and returns nil
|
||||
// once any required reply has been sent (or the message was silently
|
||||
// ignored). No goroutines are spawned.
|
||||
func (h *Handler) Handle(ctx context.Context, cb BotCallback) error {
|
||||
// Group messages that do not @ the bot are silently ignored.
|
||||
if cb.ConversationType == conversationTypeGroup && !cb.IsInAtList {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Non-text messages never reach QA; reply with the unsupported notice.
|
||||
if cb.Msgtype != msgTypeText {
|
||||
if strings.TrimSpace(cb.SessionWebhook) == "" {
|
||||
return errors.New("dingtalk handler: sessionWebhook is required to reply")
|
||||
}
|
||||
return h.reply.SendMarkdown(ctx, cb.SessionWebhook, replyTitle, unsupportedMessageReply)
|
||||
}
|
||||
|
||||
query := strings.TrimSpace(cb.Text)
|
||||
if query == "" {
|
||||
return errors.New("dingtalk handler: text message content is empty")
|
||||
}
|
||||
if cb.MsgID == "" {
|
||||
return errors.New("dingtalk handler: msgId is required")
|
||||
}
|
||||
|
||||
externalUserID := cb.SenderStaffID
|
||||
if externalUserID == "" {
|
||||
externalUserID = cb.SenderID
|
||||
}
|
||||
if externalUserID == "" {
|
||||
return errors.New("dingtalk handler: sender staffId/senderId is required")
|
||||
}
|
||||
|
||||
// The session webhook is the only reply channel; fail fast before
|
||||
// calling QA so a missing webhook never wastes a model call.
|
||||
if strings.TrimSpace(cb.SessionWebhook) == "" {
|
||||
return errors.New("dingtalk handler: sessionWebhook is required to reply")
|
||||
}
|
||||
|
||||
req := core.QARequest{
|
||||
TenantKey: h.cfg.TenantKey,
|
||||
Channel: h.cfg.Channel,
|
||||
ExternalUserID: externalUserID,
|
||||
ExternalMessageID: cb.MsgID,
|
||||
SenderName: cb.SenderNick,
|
||||
Query: query,
|
||||
}
|
||||
switch cb.ConversationType {
|
||||
case conversationTypeGroup:
|
||||
if cb.ConversationID == "" {
|
||||
return errors.New("dingtalk handler: conversationId is required for group messages")
|
||||
}
|
||||
req.ConversationType = core.ConversationGroup
|
||||
req.ExternalChatID = cb.ConversationID
|
||||
default:
|
||||
req.ConversationType = core.ConversationDirect
|
||||
}
|
||||
|
||||
result, err := h.qa.Ask(ctx, req)
|
||||
if err != nil {
|
||||
// Never leak internal error details to the user. If the friendly
|
||||
// reply cannot be delivered, surface that failure instead.
|
||||
if replyErr := h.sendReply(ctx, cb.SessionWebhook, qaErrorReply); replyErr != nil {
|
||||
return replyErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return h.sendReply(ctx, cb.SessionWebhook, result.Answer)
|
||||
}
|
||||
|
||||
func (h *Handler) sendReply(ctx context.Context, webhookURL, text string) error {
|
||||
if strings.TrimSpace(webhookURL) == "" {
|
||||
return errors.New("dingtalk handler: sessionWebhook is required to reply")
|
||||
}
|
||||
if err := h.reply.SendMarkdown(ctx, webhookURL, replyTitle, text); err != nil {
|
||||
return fmt.Errorf("dingtalk handler: send reply: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
package dingtalk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
)
|
||||
|
||||
type fakeQA struct {
|
||||
called bool
|
||||
lastReq core.QARequest
|
||||
result core.QAResult
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeQA) Ask(_ context.Context, req core.QARequest) (core.QAResult, error) {
|
||||
f.called = true
|
||||
f.lastReq = req
|
||||
return f.result, f.err
|
||||
}
|
||||
|
||||
type fakeReply struct {
|
||||
called bool
|
||||
lastURL string
|
||||
lastTitle string
|
||||
lastText string
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeReply) SendMarkdown(_ context.Context, url, title, text string) error {
|
||||
f.called = true
|
||||
f.lastURL = url
|
||||
f.lastTitle = title
|
||||
f.lastText = text
|
||||
return f.err
|
||||
}
|
||||
|
||||
func newTestHandler(t *testing.T, qa *fakeQA, reply *fakeReply) *Handler {
|
||||
t.Helper()
|
||||
cfg := TenantConfig{
|
||||
TenantKey: "tenant-a",
|
||||
Channel: "dingtalk",
|
||||
}
|
||||
h, err := NewHandler(cfg, qa, reply)
|
||||
if err != nil {
|
||||
t.Fatalf("NewHandler: %v", err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func directTextCallback() BotCallback {
|
||||
return BotCallback{
|
||||
ConversationType: "1",
|
||||
Msgtype: "text",
|
||||
MsgID: "msg-1",
|
||||
SenderNick: "Alice",
|
||||
SenderStaffID: "staff-123",
|
||||
SenderID: "user-abc",
|
||||
SessionWebhook: "https://oapi.dingtalk.com/robot/send?access_token=x",
|
||||
Text: " hello world ",
|
||||
}
|
||||
}
|
||||
|
||||
func groupTextCallback() BotCallback {
|
||||
return BotCallback{
|
||||
ConversationType: "2",
|
||||
ConversationID: "cid-group-1",
|
||||
Msgtype: "text",
|
||||
MsgID: "msg-2",
|
||||
SenderNick: "Bob",
|
||||
SenderStaffID: "staff-456",
|
||||
SenderID: "user-def",
|
||||
IsInAtList: true,
|
||||
SessionWebhook: "https://oapi.dingtalk.com/robot/send?access_token=x",
|
||||
Text: " group question ",
|
||||
}
|
||||
}
|
||||
|
||||
// --- A: direct text ---
|
||||
|
||||
func TestHandleDirectTextCallsQAAndRepliesAnswer(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "**answer**"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
err := h.Handle(context.Background(), directTextCallback())
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if !qa.called {
|
||||
t.Fatal("QA.Ask was not called")
|
||||
}
|
||||
req := qa.lastReq
|
||||
if req.TenantKey != "tenant-a" || req.Channel != "dingtalk" {
|
||||
t.Fatalf("unexpected TenantKey/Channel: %+v", req)
|
||||
}
|
||||
if req.ConversationType != core.ConversationDirect {
|
||||
t.Fatalf("ConversationType = %q, want direct", req.ConversationType)
|
||||
}
|
||||
if req.ExternalUserID != "staff-123" {
|
||||
t.Fatalf("ExternalUserID = %q, want staff-123", req.ExternalUserID)
|
||||
}
|
||||
if req.ExternalMessageID != "msg-1" {
|
||||
t.Fatalf("ExternalMessageID = %q, want msg-1", req.ExternalMessageID)
|
||||
}
|
||||
if req.SenderName != "Alice" {
|
||||
t.Fatalf("SenderName = %q, want Alice", req.SenderName)
|
||||
}
|
||||
if req.Query != "hello world" {
|
||||
t.Fatalf("Query = %q, want trimmed 'hello world'", req.Query)
|
||||
}
|
||||
if req.ExternalChatID != "" {
|
||||
t.Fatalf("ExternalChatID should be empty for direct, got %q", req.ExternalChatID)
|
||||
}
|
||||
|
||||
if !reply.called {
|
||||
t.Fatal("Reply.SendMarkdown was not called")
|
||||
}
|
||||
if reply.lastText != "**answer**" {
|
||||
t.Fatalf("reply text = %q, want answer", reply.lastText)
|
||||
}
|
||||
if reply.lastURL == "" {
|
||||
t.Fatal("reply URL should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleDirectTextFallsBackToSenderID(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.SenderStaffID = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if qa.lastReq.ExternalUserID != "user-abc" {
|
||||
t.Fatalf("ExternalUserID = %q, want fallback to senderId user-abc", qa.lastReq.ExternalUserID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleDirectTextTrimsQuery(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Text = " padded "
|
||||
_ = h.Handle(context.Background(), data)
|
||||
if qa.lastReq.Query != "padded" {
|
||||
t.Fatalf("Query = %q, want trimmed 'padded'", qa.lastReq.Query)
|
||||
}
|
||||
}
|
||||
|
||||
// --- B: group text, @bot ---
|
||||
|
||||
func TestHandleGroupTextAtBotCallsQAAndReplies(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "group answer"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
err := h.Handle(context.Background(), groupTextCallback())
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if !qa.called {
|
||||
t.Fatal("QA.Ask was not called")
|
||||
}
|
||||
req := qa.lastReq
|
||||
if req.ConversationType != core.ConversationGroup {
|
||||
t.Fatalf("ConversationType = %q, want group", req.ConversationType)
|
||||
}
|
||||
if req.ExternalChatID != "cid-group-1" {
|
||||
t.Fatalf("ExternalChatID = %q, want cid-group-1", req.ExternalChatID)
|
||||
}
|
||||
if req.Query != "group question" {
|
||||
t.Fatalf("Query = %q, want trimmed 'group question'", req.Query)
|
||||
}
|
||||
if !reply.called {
|
||||
t.Fatal("Reply.SendMarkdown was not called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGroupTextNotAtBotSilentlyIgnored(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := groupTextCallback()
|
||||
data.IsInAtList = false
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: expected nil error for non-@bot group message, got %v", err)
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for non-@bot group message")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for non-@bot group message")
|
||||
}
|
||||
}
|
||||
|
||||
// --- C: non-text message ---
|
||||
|
||||
func TestHandleNonTextRepliesUnsupported(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Msgtype = "picture"
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for non-text message")
|
||||
}
|
||||
if !reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should be called for non-text message")
|
||||
}
|
||||
if !strings.Contains(reply.lastText, "暂仅支持文字消息") {
|
||||
t.Fatalf("reply text = %q, want unsupported-message notice", reply.lastText)
|
||||
}
|
||||
}
|
||||
|
||||
// --- D: QA error ---
|
||||
|
||||
func TestHandleQAErrorRepliesFriendlyMessage(t *testing.T) {
|
||||
qa := &fakeQA{err: errors.New("weknora connection timeout: internal stack trace")}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
err := h.Handle(context.Background(), directTextCallback())
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if !reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should be called on QA error")
|
||||
}
|
||||
if !strings.Contains(reply.lastText, "暂时无法完成回答") {
|
||||
t.Fatalf("reply text = %q, want friendly error message", reply.lastText)
|
||||
}
|
||||
if strings.Contains(reply.lastText, "weknora") || strings.Contains(reply.lastText, "timeout") || strings.Contains(reply.lastText, "stack") {
|
||||
t.Fatalf("reply text leaks internal error: %q", reply.lastText)
|
||||
}
|
||||
}
|
||||
|
||||
// --- E: invalid input ---
|
||||
|
||||
func TestHandleEmptyTextReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Text = " "
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty text")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for empty text")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for empty text")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleMissingMsgIDReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.MsgID = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing MsgId")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for missing MsgId")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for missing MsgId")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleMissingUserReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.SenderStaffID = ""
|
||||
data.SenderID = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing user")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for missing user")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for missing user")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGroupMissingConversationIDReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := groupTextCallback()
|
||||
data.ConversationID = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for group missing ConversationId")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for group missing ConversationId")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for group missing ConversationId")
|
||||
}
|
||||
}
|
||||
|
||||
// --- F: missing sessionWebhook ---
|
||||
|
||||
func TestHandleMissingSessionWebhookReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.SessionWebhook = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing sessionWebhook")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called when sessionWebhook is missing (model call would be wasted)")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called when sessionWebhook is missing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleNonTextMissingSessionWebhookReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Msgtype = "picture"
|
||||
data.SessionWebhook = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-text with missing sessionWebhook")
|
||||
}
|
||||
}
|
||||
|
||||
// --- reply send failure ---
|
||||
|
||||
func TestHandleReplySendFailureReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{err: errors.New("webhook timeout")}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
err := h.Handle(context.Background(), directTextCallback())
|
||||
if err == nil {
|
||||
t.Fatal("expected error when reply send fails")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package dingtalk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"
|
||||
dtsdk "github.com/open-dingtalk/dingtalk-stream-sdk-go/client"
|
||||
)
|
||||
|
||||
// StreamConfig holds the DingTalk Stream mode credentials plus the tenant
|
||||
// this ingress serves. An entirely empty config means the DingTalk channel
|
||||
// is disabled; a partially filled config is a fatal misconfiguration.
|
||||
type StreamConfig struct {
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
TenantKey string
|
||||
}
|
||||
|
||||
// Enabled reports whether all three values are present. Callers must run
|
||||
// Validate first: Enabled on a partially filled config returns false.
|
||||
func (c StreamConfig) Enabled() bool {
|
||||
return strings.TrimSpace(c.ClientID) != "" &&
|
||||
strings.TrimSpace(c.ClientSecret) != "" &&
|
||||
strings.TrimSpace(c.TenantKey) != ""
|
||||
}
|
||||
|
||||
// Validate enforces the all-or-nothing rule: either every field is blank
|
||||
// (channel disabled, no error) or every field is set (channel enabled).
|
||||
// Any mix is an error so misconfigured deployments fail fast at startup.
|
||||
func (c StreamConfig) Validate() error {
|
||||
id := strings.TrimSpace(c.ClientID) != ""
|
||||
secret := strings.TrimSpace(c.ClientSecret) != ""
|
||||
tenant := strings.TrimSpace(c.TenantKey) != ""
|
||||
if !id && !secret && !tenant {
|
||||
return nil
|
||||
}
|
||||
if id && secret && tenant {
|
||||
return nil
|
||||
}
|
||||
missing := make([]string, 0, 3)
|
||||
if !id {
|
||||
missing = append(missing, "client id")
|
||||
}
|
||||
if !secret {
|
||||
missing = append(missing, "client secret")
|
||||
}
|
||||
if !tenant {
|
||||
missing = append(missing, "tenant key")
|
||||
}
|
||||
return fmt.Errorf("dingtalk stream config is partial: missing %s", strings.Join(missing, ", "))
|
||||
}
|
||||
|
||||
// BotCallbackFromSDK converts the official SDK callback model into the
|
||||
// channel-internal BotCallback. It is a pure one-to-one field mapping; no
|
||||
// trimming, validation or business logic happens here (Handler owns that).
|
||||
func BotCallbackFromSDK(data *chatbot.BotCallbackDataModel) (BotCallback, error) {
|
||||
if data == nil {
|
||||
return BotCallback{}, errors.New("dingtalk stream: callback data is nil")
|
||||
}
|
||||
return BotCallback{
|
||||
ConversationType: data.ConversationType,
|
||||
ConversationID: data.ConversationId,
|
||||
Msgtype: data.Msgtype,
|
||||
MsgID: data.MsgId,
|
||||
SenderNick: data.SenderNick,
|
||||
SenderStaffID: data.SenderStaffId,
|
||||
SenderID: data.SenderId,
|
||||
IsInAtList: data.IsInAtList,
|
||||
SessionWebhook: data.SessionWebhook,
|
||||
Text: data.Text.Content,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// StreamClient wraps the official DingTalk Stream SDK client. It wires
|
||||
// robot callbacks into the existing Handler synchronously (no goroutines,
|
||||
// no queue). The SDK default auto-reconnect stays enabled while running;
|
||||
// Close disables it so the connection stays closed.
|
||||
type StreamClient struct {
|
||||
inner *dtsdk.StreamClient
|
||||
}
|
||||
|
||||
// NewStreamClient validates the config, builds the SDK client with the app
|
||||
// credential and registers the chatbot callback router. The handler is
|
||||
// invoked synchronously from the SDK callback; its return value only
|
||||
// acknowledges the frame, replies always go through the session webhook.
|
||||
func NewStreamClient(config StreamConfig, handler *Handler) (*StreamClient, error) {
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !config.Enabled() {
|
||||
return nil, errors.New("dingtalk stream: config is empty, client not created")
|
||||
}
|
||||
if handler == nil {
|
||||
return nil, errors.New("dingtalk stream: handler is required")
|
||||
}
|
||||
cred := dtsdk.NewAppCredentialConfig(strings.TrimSpace(config.ClientID), strings.TrimSpace(config.ClientSecret))
|
||||
// Keep the SDK default AutoReconnect=true so runtime disconnects
|
||||
// reconnect automatically; Close flips it off before closing.
|
||||
inner := dtsdk.NewStreamClient(
|
||||
dtsdk.WithAppCredential(cred),
|
||||
)
|
||||
inner.RegisterChatBotCallbackRouter(func(ctx context.Context, data *chatbot.BotCallbackDataModel) ([]byte, error) {
|
||||
cb, err := BotCallbackFromSDK(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := handler.Handle(ctx, cb); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
return &StreamClient{inner: inner}, nil
|
||||
}
|
||||
|
||||
// Start connects to DingTalk Stream and spawns the SDK read loop. Any
|
||||
// connection or credential error is propagated to the caller.
|
||||
func (c *StreamClient) Start(ctx context.Context) error {
|
||||
if c == nil || c.inner == nil {
|
||||
return errors.New("dingtalk stream: client is nil")
|
||||
}
|
||||
if err := c.inner.Start(ctx); err != nil {
|
||||
return fmt.Errorf("dingtalk stream start: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close tears down the websocket. Auto-reconnect is disabled before the
|
||||
// underlying Close so the SDK processLoop cannot reconnect afterwards.
|
||||
func (c *StreamClient) Close() {
|
||||
if c == nil || c.inner == nil {
|
||||
return
|
||||
}
|
||||
c.inner.AutoReconnect = false
|
||||
c.inner.Close()
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package dingtalk
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"
|
||||
)
|
||||
|
||||
func TestStreamConfigValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg StreamConfig
|
||||
wantErr bool
|
||||
wantOn bool
|
||||
errSubstr string
|
||||
}{
|
||||
{
|
||||
name: "all empty is disabled without error",
|
||||
cfg: StreamConfig{},
|
||||
wantErr: false,
|
||||
wantOn: false,
|
||||
},
|
||||
{
|
||||
name: "whitespace only is disabled without error",
|
||||
cfg: StreamConfig{ClientID: " ", ClientSecret: "\t", TenantKey: " "},
|
||||
wantErr: false,
|
||||
wantOn: false,
|
||||
},
|
||||
{
|
||||
name: "client id only is an error",
|
||||
cfg: StreamConfig{ClientID: "id"},
|
||||
wantErr: true,
|
||||
errSubstr: "client secret",
|
||||
},
|
||||
{
|
||||
name: "client secret only is an error",
|
||||
cfg: StreamConfig{ClientSecret: "secret"},
|
||||
wantErr: true,
|
||||
errSubstr: "client id",
|
||||
},
|
||||
{
|
||||
name: "credentials without tenant key is an error",
|
||||
cfg: StreamConfig{ClientID: "id", ClientSecret: "secret"},
|
||||
wantErr: true,
|
||||
errSubstr: "tenant key",
|
||||
},
|
||||
{
|
||||
name: "tenant key without credentials is an error",
|
||||
cfg: StreamConfig{TenantKey: "acme"},
|
||||
wantErr: true,
|
||||
errSubstr: "client id",
|
||||
},
|
||||
{
|
||||
name: "all three present is enabled",
|
||||
cfg: StreamConfig{ClientID: "id", ClientSecret: "secret", TenantKey: "acme"},
|
||||
wantErr: false,
|
||||
wantOn: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := tt.cfg.Validate()
|
||||
if tt.wantErr && err == nil {
|
||||
t.Fatalf("Validate() = nil, want error containing %q", tt.errSubstr)
|
||||
}
|
||||
if !tt.wantErr && err != nil {
|
||||
t.Fatalf("Validate() = %v, want nil", err)
|
||||
}
|
||||
if tt.wantErr && tt.errSubstr != "" && !strings.Contains(err.Error(), tt.errSubstr) {
|
||||
t.Fatalf("Validate() error = %q, want substring %q", err.Error(), tt.errSubstr)
|
||||
}
|
||||
if got := tt.cfg.Enabled(); got != tt.wantOn {
|
||||
t.Fatalf("Enabled() = %v, want %v", got, tt.wantOn)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBotCallbackFromSDK(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Run("nil data returns error", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if _, err := BotCallbackFromSDK(nil); err == nil {
|
||||
t.Fatal("BotCallbackFromSDK(nil) = nil error, want error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("maps every field one to one", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
data := &chatbot.BotCallbackDataModel{
|
||||
ConversationType: "2",
|
||||
ConversationId: "cid123",
|
||||
Msgtype: "text",
|
||||
MsgId: "msg-9",
|
||||
SenderNick: "Alice",
|
||||
SenderStaffId: "staff-1",
|
||||
SenderId: "sender-1",
|
||||
IsInAtList: true,
|
||||
SessionWebhook: "https://oapi.dingtalk.com/robot/sendBySession?session=abc",
|
||||
Text: chatbot.BotCallbackDataTextModel{Content: " 你好 "},
|
||||
}
|
||||
cb, err := BotCallbackFromSDK(data)
|
||||
if err != nil {
|
||||
t.Fatalf("BotCallbackFromSDK() error = %v, want nil", err)
|
||||
}
|
||||
want := BotCallback{
|
||||
ConversationType: "2",
|
||||
ConversationID: "cid123",
|
||||
Msgtype: "text",
|
||||
MsgID: "msg-9",
|
||||
SenderNick: "Alice",
|
||||
SenderStaffID: "staff-1",
|
||||
SenderID: "sender-1",
|
||||
IsInAtList: true,
|
||||
SessionWebhook: "https://oapi.dingtalk.com/robot/sendBySession?session=abc",
|
||||
Text: " 你好 ",
|
||||
}
|
||||
if cb != want {
|
||||
t.Fatalf("BotCallbackFromSDK() = %+v, want %+v", cb, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("text content is not trimmed by conversion", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
data := &chatbot.BotCallbackDataModel{
|
||||
ConversationType: "1",
|
||||
Msgtype: "text",
|
||||
MsgId: "m",
|
||||
Text: chatbot.BotCallbackDataTextModel{Content: ""},
|
||||
}
|
||||
cb, err := BotCallbackFromSDK(data)
|
||||
if err != nil {
|
||||
t.Fatalf("BotCallbackFromSDK() error = %v", err)
|
||||
}
|
||||
if cb.Text != "" {
|
||||
t.Fatalf("Text = %q, want empty", cb.Text)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func mustTestHandler(t *testing.T) *Handler {
|
||||
t.Helper()
|
||||
return newTestHandler(t, &fakeQA{}, &fakeReply{})
|
||||
}
|
||||
|
||||
func TestNewStreamClientValidation(t *testing.T) {
|
||||
t.Parallel()
|
||||
h := mustTestHandler(t)
|
||||
|
||||
t.Run("nil handler is an error", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := StreamConfig{ClientID: "id", ClientSecret: "secret", TenantKey: "acme"}
|
||||
if _, err := NewStreamClient(cfg, nil); err == nil {
|
||||
t.Fatal("NewStreamClient with nil handler = nil error, want error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid config is an error", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if _, err := NewStreamClient(StreamConfig{}, h); err == nil {
|
||||
t.Fatal("NewStreamClient with empty config = nil error, want error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("valid config keeps SDK auto reconnect on until Close disables it", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := StreamConfig{ClientID: "id", ClientSecret: "secret", TenantKey: "acme"}
|
||||
sc, err := NewStreamClient(cfg, h)
|
||||
if err != nil {
|
||||
t.Fatalf("NewStreamClient() error = %v, want nil", err)
|
||||
}
|
||||
if sc == nil || sc.inner == nil {
|
||||
t.Fatal("NewStreamClient() returned nil client")
|
||||
}
|
||||
// The SDK default is AutoReconnect=true; keep it so runtime
|
||||
// disconnects reconnect automatically.
|
||||
if !sc.inner.AutoReconnect {
|
||||
t.Fatal("AutoReconnect = false after NewStreamClient, want true (SDK default must be kept)")
|
||||
}
|
||||
// Close must flip AutoReconnect off first so the SDK processLoop
|
||||
// does not reconnect a closed connection.
|
||||
sc.Close()
|
||||
if sc.inner.AutoReconnect {
|
||||
t.Fatal("AutoReconnect = true after Close, want false (Close must stay closed)")
|
||||
}
|
||||
// Closing again must remain a safe no-op.
|
||||
sc.Close()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package dingtalk
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// maxWebhookBodyBytes caps both the outgoing request body and the
|
||||
// response body we are willing to read back from the session webhook.
|
||||
maxWebhookBodyBytes = 1 << 20 // 1 MiB
|
||||
defaultHTTPTimeout = 15 * time.Second
|
||||
)
|
||||
|
||||
// WebhookSender posts replies to a DingTalk session webhook. It fails
|
||||
// closed: only https URLs on port 443 without userinfo whose hostname is
|
||||
// dingtalk.com or a subdomain are accepted, redirects are refused, and any
|
||||
// non-2xx response is an error. This prevents answers from leaking to
|
||||
// arbitrary public hosts (SSRF).
|
||||
type WebhookSender struct {
|
||||
client *http.Client
|
||||
extraHosts map[string]bool // test-only additional allowed hostnames
|
||||
validateURL func(string) error
|
||||
}
|
||||
|
||||
// NewWebhookSender builds a sender. A nil client installs a hardened default
|
||||
// (no redirects, sane timeout).
|
||||
func NewWebhookSender(client *http.Client) (*WebhookSender, error) {
|
||||
return newWebhookSender(client, nil)
|
||||
}
|
||||
|
||||
func newWebhookSender(client *http.Client, extraHosts map[string]bool) (*WebhookSender, error) {
|
||||
if client == nil {
|
||||
client = &http.Client{Timeout: defaultHTTPTimeout}
|
||||
}
|
||||
wrapped := *client
|
||||
wrapped.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
||||
return errors.New("dingtalk webhook redirects are not allowed")
|
||||
}
|
||||
sender := &WebhookSender{client: &wrapped, extraHosts: extraHosts}
|
||||
sender.validateURL = func(raw string) error {
|
||||
return sender.validate(raw)
|
||||
}
|
||||
return sender, nil
|
||||
}
|
||||
|
||||
// newWebhookSenderForTest is used by tests to allow loopback TLS servers
|
||||
// (any port) while exercising the exact same send/validation code path.
|
||||
func newWebhookSenderForTest(client *http.Client, hosts ...string) (*WebhookSender, error) {
|
||||
extra := make(map[string]bool, len(hosts))
|
||||
for _, h := range hosts {
|
||||
extra[strings.ToLower(h)] = true
|
||||
}
|
||||
sender, err := newWebhookSender(client, extra)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base := sender.validateURL
|
||||
sender.validateURL = func(raw string) error {
|
||||
if err := base(raw); err == nil {
|
||||
return nil
|
||||
} else if !strings.Contains(err.Error(), "default port 443") {
|
||||
return err
|
||||
}
|
||||
// Allow non-443 ports only for explicitly allow-listed test hosts.
|
||||
u, parseErr := url.Parse(raw)
|
||||
if parseErr != nil {
|
||||
return parseErr
|
||||
}
|
||||
if u.Scheme != "https" || u.User != nil || !extra[strings.ToLower(u.Hostname())] {
|
||||
return fmt.Errorf("dingtalk webhook URL must use default port 443, got %q", u.Port())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return sender, nil
|
||||
}
|
||||
|
||||
// ValidateWebhookURL enforces the production webhook URL policy.
|
||||
func ValidateWebhookURL(raw string) error {
|
||||
return (&WebhookSender{}).validate(raw)
|
||||
}
|
||||
|
||||
func (s *WebhookSender) validate(raw string) error {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return errors.New("dingtalk webhook URL is empty")
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dingtalk webhook URL parse: %w", err)
|
||||
}
|
||||
if u.Scheme != "https" {
|
||||
return fmt.Errorf("dingtalk webhook URL must be https, got %q", u.Scheme)
|
||||
}
|
||||
if u.User != nil {
|
||||
return errors.New("dingtalk webhook URL must not contain userinfo")
|
||||
}
|
||||
host := u.Hostname()
|
||||
if host == "" {
|
||||
return errors.New("dingtalk webhook URL must include a host")
|
||||
}
|
||||
port := u.Port()
|
||||
if port != "" && port != "443" {
|
||||
return fmt.Errorf("dingtalk webhook URL must use default port 443, got %q", port)
|
||||
}
|
||||
if !s.hostAllowed(host) {
|
||||
return fmt.Errorf("dingtalk webhook host %q is not allowed", host)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *WebhookSender) hostAllowed(host string) bool {
|
||||
host = strings.ToLower(host)
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
return false
|
||||
}
|
||||
if host == "dingtalk.com" || strings.HasSuffix(host, ".dingtalk.com") {
|
||||
return true
|
||||
}
|
||||
return s.extraHosts[host]
|
||||
}
|
||||
|
||||
// SendMarkdown posts a markdown message to the session webhook.
|
||||
func (s *WebhookSender) SendMarkdown(ctx context.Context, webhookURL, title, text string) error {
|
||||
if err := s.validateURL(webhookURL); err != nil {
|
||||
return err
|
||||
}
|
||||
if title == "" {
|
||||
title = "DobotAI"
|
||||
}
|
||||
body, err := json.Marshal(map[string]any{
|
||||
"msgtype": "markdown",
|
||||
"markdown": map[string]string{
|
||||
"title": title,
|
||||
"text": text,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal dingtalk webhook payload: %w", err)
|
||||
}
|
||||
if len(body) > maxWebhookBodyBytes {
|
||||
return errors.New("dingtalk webhook payload exceeds 1MiB")
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, webhookURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return fmt.Errorf("build dingtalk webhook request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("send dingtalk webhook: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, maxWebhookBodyBytes))
|
||||
return fmt.Errorf("dingtalk webhook returned status %d", resp.StatusCode)
|
||||
}
|
||||
n, err := io.Copy(io.Discard, io.LimitReader(resp.Body, maxWebhookBodyBytes+1))
|
||||
if err != nil {
|
||||
return fmt.Errorf("read dingtalk webhook response: %w", err)
|
||||
}
|
||||
if n > maxWebhookBodyBytes {
|
||||
return fmt.Errorf("dingtalk webhook response exceeds %d bytes", maxWebhookBodyBytes)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package dingtalk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newTestSender(t *testing.T, client *http.Client) *WebhookSender {
|
||||
t.Helper()
|
||||
sender, err := NewWebhookSender(client)
|
||||
if err != nil {
|
||||
t.Fatalf("NewWebhookSender: %v", err)
|
||||
}
|
||||
return sender
|
||||
}
|
||||
|
||||
func TestWebhookSenderRejectsInvalidURLs(t *testing.T) {
|
||||
sender := newTestSender(t, http.DefaultClient)
|
||||
cases := map[string]string{
|
||||
"http not https": "http://oapi.dingtalk.com/robot/send?access_token=x",
|
||||
"userinfo": "https://user:pass@oapi.dingtalk.com/robot/send?access_token=x",
|
||||
"arbitrary public host": "https://evil.example.com/robot/send",
|
||||
"non-default port": "https://oapi.dingtalk.com:8443/robot/send",
|
||||
"empty url": "",
|
||||
"no host": "https:///robot/send",
|
||||
}
|
||||
for name, raw := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if err := sender.SendMarkdown(context.Background(), raw, "t", "hello"); err == nil {
|
||||
t.Fatalf("expected rejection for %q", raw)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhookSenderRejectsRedirect(t *testing.T) {
|
||||
var finalURL string
|
||||
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
finalURL = r.URL.String()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
parsed, err := url.Parse(server.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
redirector := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, parsed.String(), http.StatusFound)
|
||||
}))
|
||||
defer redirector.Close()
|
||||
|
||||
client := redirector.Client()
|
||||
// TLS test servers run on 127.0.0.1, so allow the loopback host in this test only.
|
||||
sender, err := newWebhookSenderForTest(client, "127.0.0.1")
|
||||
if err != nil {
|
||||
t.Fatalf("newWebhookSenderForTest: %v", err)
|
||||
}
|
||||
err = sender.SendMarkdown(context.Background(), redirector.URL+"/robot/send", "t", "hello")
|
||||
if err == nil {
|
||||
t.Fatalf("expected redirect to be rejected, final request reached %q", finalURL)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "redirect") {
|
||||
t.Fatalf("expected redirect error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhookSenderRejectsNon2xx(t *testing.T) {
|
||||
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
defer server.Close()
|
||||
sender, err := newWebhookSenderForTest(server.Client(), "127.0.0.1")
|
||||
if err != nil {
|
||||
t.Fatalf("newWebhookSenderForTest: %v", err)
|
||||
}
|
||||
if err := sender.SendMarkdown(context.Background(), server.URL, "t", "hello"); err == nil {
|
||||
t.Fatal("expected non-2xx error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhookSenderAcceptsHTTPSDingTalkAndSendsMarkdown(t *testing.T) {
|
||||
var body []byte
|
||||
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.TLS == nil {
|
||||
t.Error("request must use TLS")
|
||||
}
|
||||
var readErr error
|
||||
body, readErr = io.ReadAll(r.Body)
|
||||
if readErr != nil {
|
||||
t.Errorf("read body: %v", readErr)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"errcode":0}`))
|
||||
}))
|
||||
server.TLS = &tls.Config{}
|
||||
server.StartTLS()
|
||||
defer server.Close()
|
||||
|
||||
// Test-only variant that accepts the loopback host but keeps the same
|
||||
// markdown payload code path as the production oapi.dingtalk.com sender.
|
||||
sender, err := newWebhookSenderForTest(server.Client(), "127.0.0.1")
|
||||
if err != nil {
|
||||
t.Fatalf("newWebhookSenderForTest: %v", err)
|
||||
}
|
||||
if err := sender.SendMarkdown(context.Background(), server.URL+"/robot/send?access_token=x", "回答", "**你好**"); err != nil {
|
||||
t.Fatalf("SendMarkdown: %v", err)
|
||||
}
|
||||
|
||||
var payload struct {
|
||||
Msgtype string `json:"msgtype"`
|
||||
Markdown struct {
|
||||
Title string `json:"title"`
|
||||
Text string `json:"text"`
|
||||
} `json:"markdown"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
t.Fatalf("payload is not JSON: %v", err)
|
||||
}
|
||||
if payload.Msgtype != "markdown" {
|
||||
t.Fatalf("msgtype = %q, want markdown", payload.Msgtype)
|
||||
}
|
||||
if payload.Markdown.Text != "**你好**" || payload.Markdown.Title == "" {
|
||||
t.Fatalf("unexpected markdown payload: %+v", payload.Markdown)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhookSenderRejectsOversizedResponse(t *testing.T) {
|
||||
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
// Stream more than 1MiB so the sender must fail rather than
|
||||
// silently truncate.
|
||||
chunk := make([]byte, 64*1024)
|
||||
for i := 0; i < (maxWebhookBodyBytes/len(chunk))+1; i++ {
|
||||
_, _ = w.Write(chunk)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
sender, err := newWebhookSenderForTest(server.Client(), "127.0.0.1")
|
||||
if err != nil {
|
||||
t.Fatalf("newWebhookSenderForTest: %v", err)
|
||||
}
|
||||
err = sender.SendMarkdown(context.Background(), server.URL, "t", "hello")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for response body larger than 1MiB, got silent truncation")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "exceeds") {
|
||||
t.Fatalf("expected size-limit error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebhookSenderValidateWebhookURLAcceptsDingTalkHosts asserts the pure
|
||||
// URL policy accepts oapi.dingtalk.com and *.dingtalk.com over https/443.
|
||||
func TestWebhookSenderValidateWebhookURLAcceptsDingTalkHosts(t *testing.T) {
|
||||
for _, raw := range []string{
|
||||
"https://oapi.dingtalk.com/robot/send?access_token=x",
|
||||
"https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend",
|
||||
} {
|
||||
if err := ValidateWebhookURL(raw); err != nil {
|
||||
t.Fatalf("ValidateWebhookURL(%q): %v", raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user