149 lines
4.5 KiB
Go
149 lines
4.5 KiB
Go
package biz
|
|
|
|
import (
|
|
"ai_scheduler/internal/biz/do"
|
|
"ai_scheduler/internal/biz/handle/dingtalk"
|
|
"ai_scheduler/internal/data/constants"
|
|
"ai_scheduler/internal/data/impl"
|
|
"ai_scheduler/internal/data/model"
|
|
"ai_scheduler/internal/entitys"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/gofiber/fiber/v2/log"
|
|
"github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
// AiRouterBiz 智能路由服务
|
|
type DingTalkBotBiz struct {
|
|
do *do.Do
|
|
handle *do.Handle
|
|
botConfigImpl *impl.BotConfigImpl
|
|
replier *chatbot.ChatbotReplier
|
|
log log.Logger
|
|
dingTalkUser *dingtalk.User
|
|
}
|
|
|
|
// NewDingTalkBotBiz
|
|
func NewDingTalkBotBiz(
|
|
do *do.Do,
|
|
handle *do.Handle,
|
|
botConfigImpl *impl.BotConfigImpl,
|
|
dingTalkUser *dingtalk.User,
|
|
|
|
) *DingTalkBotBiz {
|
|
return &DingTalkBotBiz{
|
|
do: do,
|
|
handle: handle,
|
|
botConfigImpl: botConfigImpl,
|
|
replier: chatbot.NewChatbotReplier(),
|
|
dingTalkUser: dingTalkUser,
|
|
}
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) GetDingTalkBotCfgList() (dingBotList []entitys.DingTalkBot, err error) {
|
|
botConfig := make([]model.AiBotConfig, 0)
|
|
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"status": constants.Enable})
|
|
cond = cond.And(builder.Eq{"bot_type": constants.BotTypeDingTalk})
|
|
err = d.botConfigImpl.GetRangeToMapStruct(&cond, &botConfig)
|
|
for _, v := range botConfig {
|
|
var config entitys.DingTalkBot
|
|
err = json.Unmarshal([]byte(v.BotConfig), &config)
|
|
if err != nil {
|
|
d.log.Info("初始化“%s”失败:%s", v.BotName, err.Error())
|
|
}
|
|
config.BotIndex = v.BotIndex
|
|
dingBotList = append(dingBotList, config)
|
|
}
|
|
return
|
|
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) InitRequire(ctx context.Context, data *chatbot.BotCallbackDataModel) (requireData *entitys.RequireDataDingTalkBot, err error) {
|
|
requireData = &entitys.RequireDataDingTalkBot{
|
|
Req: data,
|
|
Ch: make(chan entitys.Response, 2),
|
|
}
|
|
entitys.ResLog(requireData.Ch, "recognize_start", "收到消息,正在处理中,请稍等")
|
|
|
|
requireData.UserInfo, err = d.dingTalkUser.GetUserInfoFromBot(ctx, data.SenderStaffId, dingtalk.WithId(1))
|
|
|
|
return
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) Recognize(ctx context.Context, bot *chatbot.BotCallbackDataModel) (match entitys.Match, err error) {
|
|
|
|
return d.handle.Recognize(ctx, nil, &do.WithDingTalkBot{})
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) HandleRes(ctx context.Context, data *chatbot.BotCallbackDataModel, resp entitys.Response) error {
|
|
switch resp.Type {
|
|
case entitys.ResponseText:
|
|
return d.replyText(ctx, data.SessionWebhook, resp.Content)
|
|
case entitys.ResponseStream:
|
|
return d.replySteam(ctx, data.SessionWebhook, resp.Content)
|
|
case entitys.ResponseImg:
|
|
return d.replyImg(ctx, data.SessionWebhook, resp.Content)
|
|
case entitys.ResponseFile:
|
|
return d.replyFile(ctx, data.SessionWebhook, resp.Content)
|
|
case entitys.ResponseMarkdown:
|
|
return d.replyMarkdown(ctx, data.SessionWebhook, resp.Content)
|
|
case entitys.ResponseActionCard:
|
|
return d.replyActionCard(ctx, data.SessionWebhook, resp.Content)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) replySteam(ctx context.Context, SessionWebhook string, content string, arg ...string) error {
|
|
msg := content
|
|
if len(arg) > 0 {
|
|
msg = fmt.Sprintf(content, arg)
|
|
}
|
|
return d.replier.SimpleReplyText(ctx, SessionWebhook, []byte(msg))
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) replyText(ctx context.Context, SessionWebhook string, content string, arg ...string) error {
|
|
msg := content
|
|
if len(arg) > 0 {
|
|
msg = fmt.Sprintf(content, arg)
|
|
}
|
|
return d.replier.SimpleReplyText(ctx, SessionWebhook, []byte(msg))
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) replyImg(ctx context.Context, SessionWebhook string, content string, arg ...string) error {
|
|
msg := content
|
|
if len(arg) > 0 {
|
|
msg = fmt.Sprintf(content, arg)
|
|
}
|
|
return d.replier.SimpleReplyText(ctx, SessionWebhook, []byte(msg))
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) replyFile(ctx context.Context, SessionWebhook string, content string, arg ...string) error {
|
|
msg := content
|
|
if len(arg) > 0 {
|
|
msg = fmt.Sprintf(content, arg)
|
|
}
|
|
return d.replier.SimpleReplyText(ctx, SessionWebhook, []byte(msg))
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) replyMarkdown(ctx context.Context, SessionWebhook string, content string, arg ...string) error {
|
|
msg := content
|
|
if len(arg) > 0 {
|
|
msg = fmt.Sprintf(content, arg)
|
|
}
|
|
return d.replier.SimpleReplyText(ctx, SessionWebhook, []byte(msg))
|
|
}
|
|
|
|
func (d *DingTalkBotBiz) replyActionCard(ctx context.Context, SessionWebhook string, content string, arg ...string) error {
|
|
msg := content
|
|
if len(arg) > 0 {
|
|
msg = fmt.Sprintf(content, arg)
|
|
}
|
|
return d.replier.SimpleReplyText(ctx, SessionWebhook, []byte(msg))
|
|
}
|