feat: verify and activate support group binding
This commit is contained in:
@@ -187,6 +187,54 @@ func (s *SupportGroupSender) Send(ctx context.Context, notif coverage.SupportNot
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendTest sends a fixed, non-sensitive message used to verify that the
|
||||
// configured robot can reach the candidate support group. It deliberately
|
||||
// does not require an FAQ-table binding.
|
||||
func (s *SupportGroupSender) SendTest(ctx context.Context) error {
|
||||
msgParam, err := json.Marshal(map[string]string{
|
||||
"title": "DobotAI 技术支持群绑定测试",
|
||||
"text": "DobotAI 技术支持群绑定测试成功。",
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New("dingtalk group sender: encode test message failed")
|
||||
}
|
||||
body := map[string]any{
|
||||
"msgKey": "sampleMarkdown",
|
||||
"msgParam": string(msgParam),
|
||||
"openConversationId": s.openConversation,
|
||||
"robotCode": s.clientID,
|
||||
}
|
||||
bodyJSON, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return errors.New("dingtalk group sender: encode test request failed")
|
||||
}
|
||||
if len(bodyJSON) > maxGroupPayloadBytes {
|
||||
return fmt.Errorf("dingtalk group sender: payload exceeds %d bytes", maxGroupPayloadBytes)
|
||||
}
|
||||
token, err := s.accessToken(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
respPayload, err := s.doJSON(ctx, http.MethodPost, s.groupURL, body, token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var response struct {
|
||||
ProcessQueryKey string `json:"processQueryKey"`
|
||||
ErrCode int `json:"errcode"`
|
||||
}
|
||||
if json.Unmarshal(respPayload, &response) != nil {
|
||||
return errors.New("dingtalk group sender: invalid API response")
|
||||
}
|
||||
if response.ErrCode != 0 {
|
||||
return errors.New("dingtalk group sender: API returned business error")
|
||||
}
|
||||
if strings.TrimSpace(response.ProcessQueryKey) == "" {
|
||||
return errors.New("dingtalk group sender: incomplete API response")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// renderMarkdown produces the compact customer-question context shown in the action card.
|
||||
func (s *SupportGroupSender) renderMarkdown(notif coverage.SupportNotification) (title, text string) {
|
||||
title = "知识库未覆盖提醒"
|
||||
|
||||
@@ -539,6 +539,38 @@ func TestSupportGroupSenderNoPreviousQuestions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSupportGroupSenderSendTestDoesNotRequireFAQBinding(t *testing.T) {
|
||||
gs := newGroupTestServer()
|
||||
defer gs.Close()
|
||||
|
||||
sender, err := newSupportGroupSenderForTest(
|
||||
"appKey", "appSecret", "cid-abc",
|
||||
gs.server.Client(), gs.URL()+"/token", gs.URL()+"/groupMessages/send",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = sender.SendTest(context.Background()); err != nil {
|
||||
t.Fatalf("SendTest: %v", err)
|
||||
}
|
||||
if gs.sendCalls != 1 {
|
||||
t.Fatalf("sendCalls=%d want=1", gs.sendCalls)
|
||||
}
|
||||
if gs.lastSendBody["openConversationId"] != "cid-abc" || gs.lastSendBody["robotCode"] != "appKey" {
|
||||
t.Fatalf("body=%v", gs.lastSendBody)
|
||||
}
|
||||
if gs.lastSendBody["msgKey"] != "sampleMarkdown" {
|
||||
t.Fatalf("msgKey=%v want=sampleMarkdown", gs.lastSendBody["msgKey"])
|
||||
}
|
||||
var params map[string]string
|
||||
if err = json.Unmarshal([]byte(gs.lastSendBody["msgParam"].(string)), ¶ms); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if params["title"] != "DobotAI 技术支持群绑定测试" || !strings.Contains(params["text"], "测试成功") {
|
||||
t.Fatalf("params=%v", params)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSupportGroupSenderRejectsMalformedOrIncompleteSuccessResponse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -29,6 +29,7 @@ type ControllerRepository interface {
|
||||
IsAdministrator(context.Context, string, string) (bool, error)
|
||||
SetPreference(context.Context, string, string, string, string) error
|
||||
SaveBinding(context.Context, ResourceBinding) error
|
||||
TestAndActivateSupportGroup(context.Context, ResourceBinding) error
|
||||
DetectBootstrapCandidate(context.Context, BootstrapCandidate) error
|
||||
GetSettingSession(context.Context, string, string) (SettingSession, bool, error)
|
||||
SaveSettingSession(context.Context, SettingSession) error
|
||||
@@ -123,9 +124,17 @@ func (c *Controller) Handle(ctx context.Context, in SettingInput) (string, error
|
||||
if in.ConversationType != "group" || strings.TrimSpace(in.ConversationID) == "" {
|
||||
return "请在目标技术支持群中@机器人发送 /setting support。", nil
|
||||
}
|
||||
err = c.repo.SaveBinding(ctx, ResourceBinding{Kind: "support_group", TenantKey: in.TenantKey, ConversationID: in.ConversationID, ConversationTitle: in.ConversationTitle, CreatedByStaffID: in.StaffID})
|
||||
binding := ResourceBinding{Kind: "support_group", TenantKey: in.TenantKey, ConversationID: in.ConversationID, ConversationTitle: in.ConversationTitle, CreatedByStaffID: in.StaffID}
|
||||
if len(args) == 2 && strings.EqualFold(args[1], "test") {
|
||||
err = c.repo.TestAndActivateSupportGroup(ctx, binding)
|
||||
if err == nil {
|
||||
return "✅ 测试成功,当前群已激活为技术支持群。", nil
|
||||
}
|
||||
break
|
||||
}
|
||||
err = c.repo.SaveBinding(ctx, binding)
|
||||
if err == nil {
|
||||
return "✅ 当前群已保存为技术支持群(待后台验证)。", nil
|
||||
return "✅ 当前群已保存为技术支持群(待验证)。确认发送测试消息后,请在本群@机器人发送 /setting support test。", nil
|
||||
}
|
||||
case "question", "faq":
|
||||
if len(args) != 2 {
|
||||
|
||||
@@ -29,6 +29,12 @@ func (r *memoryRepo) SaveBinding(_ context.Context, b ResourceBinding) error {
|
||||
r.bindings[b.Kind] = b
|
||||
return nil
|
||||
}
|
||||
func (r *memoryRepo) TestAndActivateSupportGroup(_ context.Context, b ResourceBinding) error {
|
||||
b.Activate = true
|
||||
b.ValidationCode = "support_test_ok"
|
||||
r.bindings[b.Kind] = b
|
||||
return nil
|
||||
}
|
||||
func (r *memoryRepo) DetectBootstrapCandidate(_ context.Context, c BootstrapCandidate) error {
|
||||
r.candidate = c
|
||||
return nil
|
||||
@@ -72,12 +78,33 @@ func TestControllerBindsAdminSupportGroup(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
r.admins["t/a"] = true
|
||||
c := NewController(r)
|
||||
_, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting support", ConversationType: "group", ConversationID: "g", ConversationTitle: "技术支持"})
|
||||
out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting support", ConversationType: "group", ConversationID: "g", ConversationTitle: "技术支持"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.bindings["support_group"].ConversationID != "g" {
|
||||
t.Fatalf("binding=%+v", r.bindings)
|
||||
binding := r.bindings["support_group"]
|
||||
if binding.ConversationID != "g" || binding.Activate {
|
||||
t.Fatalf("binding=%+v", binding)
|
||||
}
|
||||
if !strings.Contains(out, "support test") {
|
||||
t.Fatalf("reply=%q must explain explicit test command", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerExplicitlyTestsAndActivatesSupportGroup(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
r.admins["t/a"] = true
|
||||
c := NewController(r)
|
||||
out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting support test", ConversationType: "group", ConversationID: "g", ConversationTitle: "技术支持"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
binding := r.bindings["support_group"]
|
||||
if binding.ConversationID != "g" || !binding.Activate {
|
||||
t.Fatalf("binding=%+v", binding)
|
||||
}
|
||||
if !strings.Contains(out, "测试成功") || !strings.Contains(out, "已激活") {
|
||||
t.Fatalf("reply=%q", out)
|
||||
}
|
||||
}
|
||||
func TestControllerHelpIsHandledLocally(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user