68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package services
|
|
|
|
import (
|
|
"ai_scheduler/internal/biz"
|
|
"ai_scheduler/internal/config"
|
|
|
|
"ai_scheduler/internal/entitys"
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"
|
|
)
|
|
|
|
type DingBotService struct {
|
|
config *config.Config
|
|
|
|
dingTalkBotBiz *biz.DingTalkBotBiz
|
|
}
|
|
|
|
func NewDingBotService(config *config.Config, DingTalkBotBiz *biz.DingTalkBotBiz) *DingBotService {
|
|
return &DingBotService{config: config, dingTalkBotBiz: DingTalkBotBiz}
|
|
}
|
|
|
|
func (d *DingBotService) GetServiceCfg() ([]entitys.DingTalkBot, error) {
|
|
return d.dingTalkBotBiz.GetDingTalkBotCfgList()
|
|
}
|
|
|
|
func (d *DingBotService) OnChatBotMessageReceived(ctx context.Context, data *chatbot.BotCallbackDataModel) (content []byte, err error) {
|
|
requireData, err := d.dingTalkBotBiz.InitRequire(ctx, data)
|
|
if err != nil {
|
|
return
|
|
}
|
|
go func() {
|
|
//defer close(requireData.Ch)
|
|
//if match, _err := d.dingTalkBotBiz.Recognize(ctx, data); _err != nil {
|
|
// requireData.Ch <- entitys.Response{
|
|
// Type: entitys.ResponseEnd,
|
|
// Content: fmt.Sprintf("处理消息时出错: %s", _err.Error()),
|
|
// }
|
|
//}
|
|
////向下传递
|
|
//if err = d.dingTalkBotBiz.HandleMatch(ctx, nil, requireData); err != nil {
|
|
// requireData.Ch <- entitys.Response{
|
|
// Type: entitys.ResponseEnd,
|
|
// Content: fmt.Sprintf("匹配失败: %v", err),
|
|
// }
|
|
//}
|
|
}()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
case resp, ok := <-requireData.Ch:
|
|
if !ok {
|
|
return []byte("success"), nil // 通道关闭,处理完成
|
|
}
|
|
|
|
if resp.Type == entitys.ResponseLog {
|
|
return
|
|
}
|
|
if err := d.dingTalkBotBiz.HandleRes(ctx, data, resp); err != nil {
|
|
return nil, fmt.Errorf("回复失败: %w", err)
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|