136 lines
4.0 KiB
Go
136 lines
4.0 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
|
|
replier *chatbot.ChatbotReplier
|
|
env string
|
|
DingTalkBotBiz *biz.DingTalkBotBiz
|
|
}
|
|
|
|
func NewDingBotService(config *config.Config, DingTalkBotBiz *biz.DingTalkBotBiz) *DingBotService {
|
|
return &DingBotService{config: config, replier: chatbot.NewChatbotReplier(), env: "public", DingTalkBotBiz: DingTalkBotBiz}
|
|
}
|
|
|
|
func (d *DingBotService) GetServiceCfg(cfg map[string]*config.DingTalkBot) (*config.DingTalkBot, string) {
|
|
return cfg[d.env], d.env
|
|
}
|
|
|
|
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 recognizeErr := d.DingTalkBotBiz.Recognize(ctx, requireData); recognizeErr != nil {
|
|
requireData.Ch <- entitys.Response{
|
|
Type: entitys.ResponseEnd,
|
|
Content: fmt.Sprintf("处理消息时出错: %v", recognizeErr),
|
|
}
|
|
}
|
|
//向下传递
|
|
if err = d.handle.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.handleRes(ctx, data, resp); err != nil {
|
|
return nil, fmt.Errorf("回复失败: %w", err)
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (d *DingBotService) 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 *DingBotService) 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 *DingBotService) 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 *DingBotService) 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 *DingBotService) 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 *DingBotService) 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 *DingBotService) 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))
|
|
}
|