refactor: enhance reply comment functionality with improved error handling and response structure
- Simplified error handling in handleReplyComment to check for both comment_id and user_id simultaneously. - Updated response message to include both Comment ID and User ID upon successful reply. - Modified ReplyCommentArgs struct to make comment_id and user_id optional. - Renamed MCP tool for replying to comments for clarity.
This commit is contained in:
@@ -552,23 +552,13 @@ func (s *AppServer) handleReplyComment(ctx context.Context, args map[string]inte
|
||||
}
|
||||
}
|
||||
|
||||
commentID, ok := args["comment_id"].(string)
|
||||
if !ok || commentID == "" {
|
||||
commentID, _ := args["comment_id"].(string)
|
||||
userID, _ := args["user_id"].(string)
|
||||
if commentID == "" && userID == "" {
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: "回复评论失败: 缺少comment_id参数",
|
||||
}},
|
||||
IsError: true,
|
||||
}
|
||||
}
|
||||
|
||||
userID, ok := args["user_id"].(string)
|
||||
if !ok || userID == "" {
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: "回复评论失败: 缺少user_id参数",
|
||||
Text: "回复评论失败: 缺少comment_id或user_id参数",
|
||||
}},
|
||||
IsError: true,
|
||||
}
|
||||
@@ -600,11 +590,11 @@ func (s *AppServer) handleReplyComment(ctx context.Context, args map[string]inte
|
||||
}
|
||||
|
||||
// 返回成功结果
|
||||
resultText := fmt.Sprintf("回复评论成功 - Feed ID: %s", result.FeedID)
|
||||
responseText := fmt.Sprintf("评论回复成功 - Feed ID: %s, Comment ID: %s, User ID: %s", result.FeedID, result.TargetCommentID, result.TargetUserID)
|
||||
return &MCPToolResult{
|
||||
Content: []MCPContent{{
|
||||
Type: "text",
|
||||
Text: resultText,
|
||||
Text: responseText,
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,8 +54,8 @@ type PostCommentArgs struct {
|
||||
type ReplyCommentArgs struct {
|
||||
FeedID string `json:"feed_id" jsonschema:"小红书笔记ID,从Feed列表获取"`
|
||||
XsecToken string `json:"xsec_token" jsonschema:"访问令牌,从Feed列表的xsecToken字段获取"`
|
||||
CommentID string `json:"comment_id" jsonschema:"评论ID"`
|
||||
UserID string `json:"user_id" jsonschema:"用户ID"`
|
||||
CommentID string `json:"comment_id,omitempty" jsonschema:"目标评论ID,从评论列表获取"`
|
||||
UserID string `json:"user_id,omitempty" jsonschema:"目标评论用户ID,从评论列表获取"`
|
||||
Content string `json:"content" jsonschema:"回复内容"`
|
||||
}
|
||||
|
||||
@@ -216,10 +216,17 @@ func registerTools(server *mcp.Server, appServer *AppServer) {
|
||||
// 工具 9: 回复评论
|
||||
mcp.AddTool(server,
|
||||
&mcp.Tool{
|
||||
Name: "reply_to_comment",
|
||||
Description: "回复小红书笔记的评论",
|
||||
Name: "reply_comment_in_feed",
|
||||
Description: "回复小红书笔记下的指定评论",
|
||||
},
|
||||
func(ctx context.Context, req *mcp.CallToolRequest, args ReplyCommentArgs) (*mcp.CallToolResult, any, error) {
|
||||
if args.CommentID == "" && args.UserID == "" {
|
||||
return &mcp.CallToolResult{
|
||||
IsError: true,
|
||||
Content: []mcp.Content{&mcp.TextContent{Text: "缺少 comment_id 或 user_id"}},
|
||||
}, nil, nil
|
||||
}
|
||||
|
||||
argsMap := map[string]interface{}{
|
||||
"feed_id": args.FeedID,
|
||||
"xsec_token": args.XsecToken,
|
||||
|
||||
14
service.go
14
service.go
@@ -443,8 +443,8 @@ func (s *XiaohongshuService) UnfavoriteFeed(ctx context.Context, feedID, xsecTok
|
||||
return &ActionResult{FeedID: feedID, Success: true, Message: "取消收藏成功或未收藏"}, nil
|
||||
}
|
||||
|
||||
// ReplyCommentToFeed 回复笔记评论
|
||||
func (s *XiaohongshuService) ReplyCommentToFeed(ctx context.Context, feedID, xsecToken, commentID, userID, content string) (*ActionResult, error) {
|
||||
// ReplyCommentToFeed 回复指定评论
|
||||
func (s *XiaohongshuService) ReplyCommentToFeed(ctx context.Context, feedID, xsecToken, commentID, userID, content string) (*ReplyCommentResponse, error) {
|
||||
b := newBrowser()
|
||||
defer b.Close()
|
||||
|
||||
@@ -452,10 +452,18 @@ func (s *XiaohongshuService) ReplyCommentToFeed(ctx context.Context, feedID, xse
|
||||
defer page.Close()
|
||||
|
||||
action := xiaohongshu.NewCommentFeedAction(page)
|
||||
|
||||
if err := action.ReplyToComment(ctx, feedID, xsecToken, commentID, userID, content); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ActionResult{FeedID: feedID, Success: true, Message: "回复评论成功"}, nil
|
||||
|
||||
return &ReplyCommentResponse{
|
||||
FeedID: feedID,
|
||||
TargetCommentID: commentID,
|
||||
TargetUserID: userID,
|
||||
Success: true,
|
||||
Message: "评论回复成功",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newBrowser() *headless_browser.Browser {
|
||||
|
||||
Reference in New Issue
Block a user