chore: add error log for reading chat response buffer

This commit is contained in:
RockYang 2023-07-24 12:05:38 +08:00
parent dd71fe80a5
commit 5444ed77ad
8 changed files with 38 additions and 28 deletions

View File

@ -33,6 +33,8 @@ func NewDefaultConfig() *types.AppConfig {
HttpOnly: false, HttpOnly: false,
SameSite: http.SameSiteLaxMode, SameSite: http.SameSiteLaxMode,
}, },
AlApiToken: "",
StartWechatBot: false,
} }
} }

View File

@ -18,6 +18,8 @@ type AppConfig struct {
AesEncryptKey string AesEncryptKey string
SmsConfig AliYunSmsConfig // 短信发送配置 SmsConfig AliYunSmsConfig // 短信发送配置
AlApiToken string // AL API 服务 token
StartWechatBot bool // 是否启动微信机器人
} }
type AliYunSmsConfig struct { type AliYunSmsConfig struct {

View File

@ -251,7 +251,7 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession
if strings.Contains(err.Error(), "context canceled") { if strings.Contains(err.Error(), "context canceled") {
logger.Info("用户取消了请求:", prompt) logger.Info("用户取消了请求:", prompt)
} else if err != io.EOF { } else if err != io.EOF {
logger.Error(err) logger.Error("信息读取出错:", err)
} }
break break
} }
@ -313,7 +313,7 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession
if err != nil { if err != nil {
replyChunkMessage(ws, types.WsMessage{ replyChunkMessage(ws, types.WsMessage{
Type: types.WsMiddle, Type: types.WsMiddle,
Content: "调用函数出错", Content: "调用函数出错" + err.Error(),
}) })
} else { } else {
replyChunkMessage(ws, types.WsMessage{ replyChunkMessage(ws, types.WsMessage{

View File

@ -12,7 +12,6 @@ import (
"chatplus/store" "chatplus/store"
"context" "context"
"embed" "embed"
"errors"
"io" "io"
"log" "log"
"os" "os"
@ -114,26 +113,14 @@ func main() {
}), }),
// 创建函数 // 创建函数
fx.Provide(func() (function.FuncZaoBao, error) { fx.Provide(func(config *types.AppConfig) (function.FuncZaoBao, error) {
apiToken := os.Getenv("AL_API_TOKEN") return function.NewZaoBao(config.AlApiToken), nil
if apiToken == "" {
return function.FuncZaoBao{}, errors.New("invalid AL api token")
}
return function.NewZaoBao(apiToken), nil
}), }),
fx.Provide(func() (function.FuncWeiboHot, error) { fx.Provide(func(config *types.AppConfig) (function.FuncWeiboHot, error) {
apiToken := os.Getenv("AL_API_TOKEN") return function.NewWeiboHot(config.AlApiToken), nil
if apiToken == "" {
return function.FuncWeiboHot{}, errors.New("invalid AL api token")
}
return function.NewWeiboHot(apiToken), nil
}), }),
fx.Provide(func() (function.FuncHeadlines, error) { fx.Provide(func(config *types.AppConfig) (function.FuncHeadlines, error) {
apiToken := os.Getenv("AL_API_TOKEN") return function.NewHeadLines(config.AlApiToken), nil
if apiToken == "" {
return function.FuncHeadlines{}, errors.New("invalid AL api token")
}
return function.NewHeadLines(apiToken), nil
}), }),
// 创建控制器 // 创建控制器

View File

@ -1,6 +1,7 @@
package wexin package wexin
import ( import (
"chatplus/core/types"
logger2 "chatplus/logger" logger2 "chatplus/logger"
"github.com/eatmoreapple/openwechat" "github.com/eatmoreapple/openwechat"
"gorm.io/gorm" "gorm.io/gorm"
@ -12,9 +13,10 @@ var logger = logger2.GetLogger()
type WeChatBot struct { type WeChatBot struct {
bot *openwechat.Bot bot *openwechat.Bot
db *gorm.DB 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 := openwechat.DefaultBot(openwechat.Desktop)
// 注册消息处理函数 // 注册消息处理函数
bot.MessageHandler = func(msg *openwechat.Message) { bot.MessageHandler = func(msg *openwechat.Message) {
@ -25,10 +27,15 @@ func NewWeChatBot(db *gorm.DB) *WeChatBot {
return &WeChatBot{ return &WeChatBot{
bot: bot, bot: bot,
db: db, db: db,
appConfig: config,
} }
} }
func (b *WeChatBot) Login() error { func (b *WeChatBot) Login() error {
if !b.appConfig.StartWechatBot {
return nil
}
// 创建热存储容器对象 // 创建热存储容器对象
reloadStorage := openwechat.NewJsonFileHotReloadStorage("storage.json") reloadStorage := openwechat.NewJsonFileHotReloadStorage("storage.json")
// 执行热登录 // 执行热登录

View File

@ -2,6 +2,7 @@ package function
import ( import (
"chatplus/utils" "chatplus/utils"
"errors"
"fmt" "fmt"
"strings" "strings"
) )
@ -32,6 +33,9 @@ type HeadLineVo struct {
} }
func (f FuncHeadlines) Invoke(...interface{}) (string, error) { 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) url := fmt.Sprintf("%s?type=toutiao&token=%s", f.apiURL, f.token)
bytes, err := utils.HttpGet(url, "") bytes, err := utils.HttpGet(url, "")

View File

@ -2,6 +2,7 @@ package function
import ( import (
"chatplus/utils" "chatplus/utils"
"errors"
"fmt" "fmt"
"strings" "strings"
) )
@ -28,6 +29,9 @@ type WeiBoVo struct {
} }
func (f FuncWeiboHot) Invoke(...interface{}) (string, error) { 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) url := fmt.Sprintf("%s?num=10&token=%s", f.apiURL, f.token)
bytes, err := utils.HttpGet(url, "") bytes, err := utils.HttpGet(url, "")

View File

@ -2,6 +2,7 @@ package function
import ( import (
"chatplus/utils" "chatplus/utils"
"errors"
"fmt" "fmt"
"strings" "strings"
) )
@ -28,6 +29,9 @@ type ZaoBaoVo struct {
} }
func (f FuncZaoBao) Invoke(...interface{}) (string, error) { 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) url := fmt.Sprintf("%s?format=json&token=%s", f.apiURL, f.token)
bytes, err := utils.HttpGet(url, "") bytes, err := utils.HttpGet(url, "")