ai_scheduler/internal/tools_bot/dtalk_bot.go

99 lines
2.9 KiB
Go

package tools_bot
import (
"ai_scheduler/internal/config"
"ai_scheduler/internal/data/constants"
errors "ai_scheduler/internal/data/error"
"ai_scheduler/internal/data/impl"
"ai_scheduler/internal/entitys"
"ai_scheduler/internal/pkg"
"ai_scheduler/internal/pkg/l_request"
"ai_scheduler/internal/pkg/utils_ollama"
"context"
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2/log"
"xorm.io/builder"
)
type BotTool struct {
config *config.Config
llm *utils_ollama.Client
sessionImpl *impl.SessionImpl
}
// NewBotTool 创建直连天下订单详情工具
func NewBotTool(config *config.Config, llm *utils_ollama.Client, sessionImpl *impl.SessionImpl) *BotTool {
return &BotTool{config: config, llm: llm, sessionImpl: sessionImpl}
}
// BugOptimizationSubmitForm 工单提交表单参数
type BugOptimizationSubmitForm struct {
Mark string `json:"mark"` // 工单标识
Text string `json:"text"` // 工单描述
Img string `json:"img"` // 工单截图
Creator string `json:"creator"` // 工单创建人
Session string `json:"session"` // 会话ID
}
// Execute 执行直连天下订单详情查询
func (w *BotTool) Execute(ctx context.Context, toolName string, requireData *entitys.RequireData) (err error) {
switch toolName {
case constants.BotToolsBugOptimizationSubmit:
err = w.BugOptimizationSubmit(ctx, requireData)
default:
log.Errorf("未知的工具类型:%s", toolName)
err = errors.ParamErr("未知的工具类型:%s", toolName)
}
return
}
func (w *BotTool) BugOptimizationSubmit(ctx context.Context, requireData *entitys.RequireData) (err error) {
// 获取用户信息
cond := builder.NewCond()
cond = cond.And(builder.Eq{"session_id": requireData.Session})
sessionInfo, err := w.sessionImpl.GetOneBySearch(&cond)
if err != nil {
err = errors.SysErr("获取历史记录失败:%v", err.Error())
return
}
userName := sessionInfo["user_name"].(string)
// 构建工单表单参数
body := BugOptimizationSubmitForm{
Mark: requireData.Match.Index,
Text: requireData.Req.Text,
Img: requireData.Req.Img,
Creator: userName,
Session: requireData.Session,
}
request := l_request.Request{
Url: "https://connector.dingtalk.com/webhook/flow/10352c521dd02104cee9000c",
Method: "POST",
Headers: map[string]string{
"Content-Type": "application/json",
},
JsonByte: pkg.JsonByteIgonErr(body),
}
res, err := request.Send()
if err != nil {
log.Errorf("发送请求失败: %s", err.Error())
return
}
data := make(map[string]bool)
if err = json.Unmarshal(res.Content, &data); err != nil {
return fmt.Errorf("解析商品数据失败:%w", err)
}
if data["success"] {
entitys.ResLoading(requireData.Ch, requireData.Match.Index, "问题信息记录中...")
return
}
entitys.ResJson(requireData.Ch, requireData.Match.Index, "bug问题请咨询 @温子新 ,优化建议请咨询 @贺泽琨 。")
return
}