89 lines
2.2 KiB
Go
89 lines
2.2 KiB
Go
package biz
|
|
|
|
import (
|
|
"ai_scheduler/internal/biz/handle/qywx"
|
|
"ai_scheduler/internal/data/constants"
|
|
"ai_scheduler/internal/data/impl"
|
|
"ai_scheduler/internal/data/model"
|
|
"ai_scheduler/internal/pkg"
|
|
"ai_scheduler/internal/tools/bbxt"
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"ai_scheduler/internal/config"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
// AiRouterBiz 智能路由服务
|
|
type QywxAppBiz struct {
|
|
conf *config.Config
|
|
botGroupQywxImpl *impl.BotGroupQywxImpl
|
|
qywxGroupHandle *qywx.Group
|
|
}
|
|
|
|
// NewDingTalkBotBiz
|
|
func NewQywxAppBiz(
|
|
conf *config.Config,
|
|
botGroupQywxImpl *impl.BotGroupQywxImpl,
|
|
qywxGroupHandle *qywx.Group,
|
|
) *QywxAppBiz {
|
|
return &QywxAppBiz{
|
|
conf: conf,
|
|
botGroupQywxImpl: botGroupQywxImpl,
|
|
qywxGroupHandle: qywxGroupHandle,
|
|
}
|
|
}
|
|
|
|
func (q *QywxAppBiz) InitGroup(ctx context.Context) (string, error) {
|
|
chatId := pkg.RandomString(q.conf.Qywx.ChatIdLen)
|
|
GroupInfo := &model.AiBotGroupQywx{
|
|
Title: "bot_group_" + time.Now().Format(time.DateOnly),
|
|
ChatID: chatId,
|
|
ConfigID: q.conf.Qywx.DefaultConfigId,
|
|
AppSecret: q.conf.Qywx.AppSecret,
|
|
}
|
|
_, err := q.botGroupQywxImpl.Add(GroupInfo)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
resp, err := q.qywxGroupHandle.Create(
|
|
ctx,
|
|
qywx.GroupCreateReq{
|
|
Name: GroupInfo.Title,
|
|
Chatid: GroupInfo.ChatID,
|
|
Userlist: []string{
|
|
q.conf.Qywx.InitAccount,
|
|
},
|
|
},
|
|
q.conf.Qywx.CorpId,
|
|
GroupInfo.AppSecret,
|
|
)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return resp.Chatid, nil
|
|
}
|
|
|
|
func (q *QywxAppBiz) GetGroupInfo(ctx context.Context, groupId int) (group model.AiBotGroupQywx, err error) {
|
|
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"group_id": groupId})
|
|
cond = cond.And(builder.Eq{"status": constants.Enable})
|
|
err = q.botGroupQywxImpl.GetOneBySearchToStrut(&cond, &group)
|
|
|
|
return
|
|
}
|
|
|
|
func (q *QywxAppBiz) SendReport(ctx context.Context, groupInfo *model.AiBotGroupQywx, report *bbxt.ReportRes) (err error) {
|
|
confitent := fmt.Sprintf("%s\n%s", report.Title, fmt.Sprintf("", report.Url))
|
|
err = q.qywxGroupHandle.SendMarkDown(ctx, qywx.GroupSendMarkDownReq{
|
|
Chatid: groupInfo.ChatID,
|
|
Markdown: qywx.MarkDown{
|
|
Content: confitent,
|
|
},
|
|
}, q.conf.Qywx.CorpId, groupInfo.AppSecret)
|
|
return
|
|
}
|