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,
SameSite: http.SameSiteLaxMode,
},
AlApiToken: "",
StartWechatBot: false,
}
}

View File

@ -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 {

View File

@ -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{

View File

@ -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
}),
// 创建控制器

View File

@ -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")
// 执行热登录

View File

@ -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, "")

View File

@ -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, "")

View File

@ -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, "")