diff --git a/api/core/config.go b/api/core/config.go index b2f6bf34..e870cc9a 100644 --- a/api/core/config.go +++ b/api/core/config.go @@ -33,6 +33,8 @@ func NewDefaultConfig() *types.AppConfig { HttpOnly: false, SameSite: http.SameSiteLaxMode, }, + AlApiToken: "", + StartWechatBot: false, } } diff --git a/api/core/types/config.go b/api/core/types/config.go index 3a796108..ada71ca0 100644 --- a/api/core/types/config.go +++ b/api/core/types/config.go @@ -16,8 +16,10 @@ type AppConfig struct { StaticUrl string // 静态资源 URL Redis RedisConfig // redis 连接信息 - AesEncryptKey string - SmsConfig AliYunSmsConfig // 短信发送配置 + AesEncryptKey string + SmsConfig AliYunSmsConfig // 短信发送配置 + AlApiToken string // AL API 服务 token + StartWechatBot bool // 是否启动微信机器人 } type AliYunSmsConfig struct { diff --git a/api/handler/chat_handler.go b/api/handler/chat_handler.go index 7ff98bec..b513bfd1 100644 --- a/api/handler/chat_handler.go +++ b/api/handler/chat_handler.go @@ -251,7 +251,7 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession if strings.Contains(err.Error(), "context canceled") { logger.Info("用户取消了请求:", prompt) } else if err != io.EOF { - logger.Error(err) + logger.Error("信息读取出错:", err) } break } @@ -313,7 +313,7 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession if err != nil { replyChunkMessage(ws, types.WsMessage{ Type: types.WsMiddle, - Content: "调用函数出错", + Content: "调用函数出错:" + err.Error(), }) } else { replyChunkMessage(ws, types.WsMessage{ diff --git a/api/main.go b/api/main.go index 01d2062e..6731923b 100644 --- a/api/main.go +++ b/api/main.go @@ -12,7 +12,6 @@ import ( "chatplus/store" "context" "embed" - "errors" "io" "log" "os" @@ -114,26 +113,14 @@ func main() { }), // 创建函数 - fx.Provide(func() (function.FuncZaoBao, error) { - apiToken := os.Getenv("AL_API_TOKEN") - if apiToken == "" { - return function.FuncZaoBao{}, errors.New("invalid AL api token") - } - return function.NewZaoBao(apiToken), nil + fx.Provide(func(config *types.AppConfig) (function.FuncZaoBao, error) { + return function.NewZaoBao(config.AlApiToken), nil }), - fx.Provide(func() (function.FuncWeiboHot, error) { - apiToken := os.Getenv("AL_API_TOKEN") - if apiToken == "" { - return function.FuncWeiboHot{}, errors.New("invalid AL api token") - } - return function.NewWeiboHot(apiToken), nil + fx.Provide(func(config *types.AppConfig) (function.FuncWeiboHot, error) { + return function.NewWeiboHot(config.AlApiToken), nil }), - fx.Provide(func() (function.FuncHeadlines, error) { - apiToken := os.Getenv("AL_API_TOKEN") - if apiToken == "" { - return function.FuncHeadlines{}, errors.New("invalid AL api token") - } - return function.NewHeadLines(apiToken), nil + fx.Provide(func(config *types.AppConfig) (function.FuncHeadlines, error) { + return function.NewHeadLines(config.AlApiToken), nil }), // 创建控制器 diff --git a/api/modules/wexin/wechat_bot.go b/api/modules/wexin/wechat_bot.go index 6dbc4b9e..8ac5f5ec 100644 --- a/api/modules/wexin/wechat_bot.go +++ b/api/modules/wexin/wechat_bot.go @@ -1,6 +1,7 @@ package wexin import ( + "chatplus/core/types" logger2 "chatplus/logger" "github.com/eatmoreapple/openwechat" "gorm.io/gorm" @@ -10,11 +11,12 @@ import ( var logger = logger2.GetLogger() type WeChatBot struct { - bot *openwechat.Bot - db *gorm.DB + bot *openwechat.Bot + db *gorm.DB + appConfig *types.AppConfig } -func NewWeChatBot(db *gorm.DB) *WeChatBot { +func NewWeChatBot(db *gorm.DB, config *types.AppConfig) *WeChatBot { bot := openwechat.DefaultBot(openwechat.Desktop) // 注册消息处理函数 bot.MessageHandler = func(msg *openwechat.Message) { @@ -23,12 +25,17 @@ func NewWeChatBot(db *gorm.DB) *WeChatBot { // 注册登陆二维码回调 bot.UUIDCallback = QrCodeCallBack return &WeChatBot{ - bot: bot, - db: db, + bot: bot, + db: db, + appConfig: config, } } func (b *WeChatBot) Login() error { + if !b.appConfig.StartWechatBot { + return nil + } + // 创建热存储容器对象 reloadStorage := openwechat.NewJsonFileHotReloadStorage("storage.json") // 执行热登录 diff --git a/api/service/function/tou_tiao.go b/api/service/function/tou_tiao.go index e9e2402c..b7500782 100644 --- a/api/service/function/tou_tiao.go +++ b/api/service/function/tou_tiao.go @@ -2,6 +2,7 @@ package function import ( "chatplus/utils" + "errors" "fmt" "strings" ) @@ -32,6 +33,9 @@ type HeadLineVo struct { } func (f FuncHeadlines) Invoke(...interface{}) (string, error) { + if f.token == "" { + return "", errors.New("无效的 API Token") + } url := fmt.Sprintf("%s?type=toutiao&token=%s", f.apiURL, f.token) bytes, err := utils.HttpGet(url, "") diff --git a/api/service/function/weibo_hot.go b/api/service/function/weibo_hot.go index ef568498..c9e7dc33 100644 --- a/api/service/function/weibo_hot.go +++ b/api/service/function/weibo_hot.go @@ -2,6 +2,7 @@ package function import ( "chatplus/utils" + "errors" "fmt" "strings" ) @@ -28,6 +29,9 @@ type WeiBoVo struct { } func (f FuncWeiboHot) Invoke(...interface{}) (string, error) { + if f.token == "" { + return "", errors.New("无效的 API Token") + } url := fmt.Sprintf("%s?num=10&token=%s", f.apiURL, f.token) bytes, err := utils.HttpGet(url, "") diff --git a/api/service/function/zao_bao.go b/api/service/function/zao_bao.go index 8065b237..a6486c55 100644 --- a/api/service/function/zao_bao.go +++ b/api/service/function/zao_bao.go @@ -2,6 +2,7 @@ package function import ( "chatplus/utils" + "errors" "fmt" "strings" ) @@ -28,6 +29,9 @@ type ZaoBaoVo struct { } func (f FuncZaoBao) Invoke(...interface{}) (string, error) { + if f.token == "" { + return "", errors.New("无效的 API Token") + } url := fmt.Sprintf("%s?format=json&token=%s", f.apiURL, f.token) bytes, err := utils.HttpGet(url, "")