mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 05:30:07 +08:00
✨ add ChatGPT
This commit is contained in:
parent
8399e49072
commit
e49b568d6d
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.19
|
||||
require (
|
||||
github.com/Baidu-AIP/golang-sdk v1.1.1
|
||||
github.com/Coloured-glaze/gg v1.3.4
|
||||
github.com/FloatTech/AnimeAPI v1.6.1-0.20221207112300-402869bb24ce
|
||||
github.com/FloatTech/AnimeAPI v1.6.1-0.20221207123128-6d7d00d380db
|
||||
github.com/FloatTech/floatbox v0.0.0-20221203040505-d7352e564906
|
||||
github.com/FloatTech/sqlite v1.5.7
|
||||
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b
|
||||
|
||||
4
go.sum
4
go.sum
@ -4,8 +4,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
|
||||
github.com/Coloured-glaze/gg v1.3.4 h1:l31zIF/HaVwkzjrj+A56RGQoSKyKuR1IWtIrqXGFStI=
|
||||
github.com/Coloured-glaze/gg v1.3.4/go.mod h1:Ih5NLNNDHOy3RJbB0EPqGTreIzq/H02TGThIagh8HJg=
|
||||
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
|
||||
github.com/FloatTech/AnimeAPI v1.6.1-0.20221207112300-402869bb24ce h1:jGfPAVKjptk9zLvXEAwG4xRqHkhRVKWzFW6ZvTdIKoA=
|
||||
github.com/FloatTech/AnimeAPI v1.6.1-0.20221207112300-402869bb24ce/go.mod h1:JL6hGTEAYFKSGq+k4Zz7MEF+UH2lUKci2X6pvb/mNsI=
|
||||
github.com/FloatTech/AnimeAPI v1.6.1-0.20221207123128-6d7d00d380db h1:B4ChWN3j+qxF+vNuogDDMe7Vw/DqAaoCNV4sFHkyvw0=
|
||||
github.com/FloatTech/AnimeAPI v1.6.1-0.20221207123128-6d7d00d380db/go.mod h1:JL6hGTEAYFKSGq+k4Zz7MEF+UH2lUKci2X6pvb/mNsI=
|
||||
github.com/FloatTech/floatbox v0.0.0-20221203040505-d7352e564906 h1:Rxc/7zh/dR96prmq/mOjmtam261CSRwZPFXi/+oU7d8=
|
||||
github.com/FloatTech/floatbox v0.0.0-20221203040505-d7352e564906/go.mod h1:i8k21EWZVoSz7/0PB2reDQXnGR6eEL9VytEev2XuqLc=
|
||||
github.com/FloatTech/rendercard v0.0.2-0.20221128165614-a41216d2422e h1:7bF01RHsYS99Zp+OWfob1W/Cymho6fcggoRSpiuiYB8=
|
||||
|
||||
@ -10,11 +10,12 @@ import (
|
||||
|
||||
"github.com/FloatTech/AnimeAPI/aireply"
|
||||
"github.com/FloatTech/AnimeAPI/chatgpt"
|
||||
"github.com/FloatTech/floatbox/binary"
|
||||
ctrl "github.com/FloatTech/zbpctrl"
|
||||
"github.com/FloatTech/zbputils/control"
|
||||
"github.com/FloatTech/zbputils/ctxext"
|
||||
"github.com/pkumza/numcn"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/sirupsen/logrus"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
)
|
||||
@ -36,7 +37,7 @@ func init() { // 插件主体
|
||||
enr := control.Register("aireply", &ctrl.Options[*zero.Ctx]{
|
||||
DisableOnDefault: false,
|
||||
Brief: "人工智能回复",
|
||||
Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱|ChatGPT]\n- 设置 ChatGPT api token xxx",
|
||||
Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱|ChatGPT]\n- 设置 ChatGPT SessionToken xxx",
|
||||
PrivateDataFolder: "aireply",
|
||||
})
|
||||
/*************************************************************
|
||||
@ -81,7 +82,7 @@ func init() { // 插件主体
|
||||
re.ReplaceAllStringFunc(reply, func(s string) string {
|
||||
f, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil {
|
||||
log.Errorln("[tts]:", err)
|
||||
logrus.Errorln("[tts]", err)
|
||||
return s
|
||||
}
|
||||
return numcn.EncodeFromFloat64(f)
|
||||
@ -155,9 +156,31 @@ func init() { // 插件主体
|
||||
}
|
||||
ctx.SendChain(message.Text("设置成功"))
|
||||
})
|
||||
ent.OnRegex(`^设置\s*ChatGPT\s*api\s*token\s*(.+)$`, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||
chatgptfile := ent.DataFolder() + "chatgpt.txt"
|
||||
cfg := &chatgpt.Config{
|
||||
UA: chatgpt.UA,
|
||||
RefreshInterval: time.Hour,
|
||||
Timeout: time.Minute,
|
||||
}
|
||||
data, err := os.ReadFile(chatgptfile)
|
||||
if err == nil {
|
||||
cfg.SessionToken = binary.BytesToString(data)
|
||||
chats = aireply.NewChatGPT(cfg)
|
||||
}
|
||||
go func() {
|
||||
for range time.NewTicker(time.Hour).C {
|
||||
if chats == nil {
|
||||
continue
|
||||
}
|
||||
err := os.WriteFile(chatgptfile, binary.StringToBytes(cfg.SessionToken), 0644)
|
||||
if err != nil {
|
||||
logrus.Warnln("[aireply] 保存 chatgpt session token 到", chatgptfile, "失败:", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
ent.OnRegex(`^设置\s*ChatGPT\s*SessionToken\s*(.*)$`, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||
token := ctx.State["regex_matched"].([]string)[1]
|
||||
f, err := os.Create(ent.DataFolder() + "chatgpt.txt")
|
||||
f, err := os.Create(chatgptfile)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
|
||||
Loading…
Reference in New Issue
Block a user