Skip to content

Commit

Permalink
aireply supports ChatGPT
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Dec 7, 2022
1 parent d76a65e commit 8399e49
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.20221203040820-0dda47e61cd8
github.com/FloatTech/AnimeAPI v1.6.1-0.20221207112300-402869bb24ce
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
Expand Down Expand Up @@ -50,7 +50,7 @@ require (
github.com/gabriel-vasile/mimetype v1.0.4 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down Expand Up @@ -78,7 +78,7 @@ require (
golang.org/x/exp/shiny v0.0.0-20221126150942-6ab00d035af9 // indirect
golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/tools v0.1.12 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.20221203040820-0dda47e61cd8 h1:HlJDqBlEuS1337zBhjjyutf+GESQJ6wMIk9zBoOVgBI=
github.com/FloatTech/AnimeAPI v1.6.1-0.20221203040820-0dda47e61cd8/go.mod h1:9d56SRgNBXZrs2mnndEMBuctdFHhxoTwGBvOK3jIVi0=
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/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=
Expand Down Expand Up @@ -88,8 +88,9 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZ
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
Expand Down Expand Up @@ -235,8 +236,9 @@ golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b h1:tvrvnPFcdzp294diPnrdZZZ8XUt2Tyj7svb7X52iDuU=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
19 changes: 14 additions & 5 deletions plugin/ai_reply/ai_tts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

zero "github.com/wdvxdr1123/ZeroBot"

"github.com/FloatTech/AnimeAPI/aireply"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
)
Expand Down Expand Up @@ -113,19 +114,27 @@ func setReplyMode(ctx *zero.Ctx, name string) error {
return m.SetData(gid, index)
}

func getReplyMode(ctx *zero.Ctx) (name string) {
var chats *aireply.ChatGPT

func getReplyMode(ctx *zero.Ctx) aireply.AIReply {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
m, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if ok {
index := m.GetData(gid)
if int(index) < len(replyModes) {
return replyModes[index]
switch m.GetData(gid) {
case 0:
return aireply.NewQYK(aireply.QYKURL, aireply.QYKBotName)
case 1:
return aireply.NewXiaoAi(aireply.XiaoAiURL, aireply.XiaoAiBotName)
case 2:
if chats != nil {
return chats
}
}
}
return "青云客"
return aireply.NewQYK(aireply.QYKURL, aireply.QYKBotName)
}

/*************************************************************
Expand Down
40 changes: 32 additions & 8 deletions plugin/ai_reply/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package aireply
import (
"fmt"
"net/url"
"os"
"strconv"
"time"

"github.com/FloatTech/AnimeAPI/aireply"
"github.com/FloatTech/AnimeAPI/chatgpt"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
Expand All @@ -17,7 +19,7 @@ import (
"github.com/wdvxdr1123/ZeroBot/message"
)

var replyModes = [...]string{"青云客", "小爱"}
var replyModes = [...]string{"青云客", "小爱", "ChatGPT"}

func init() { // 插件主体
ent := control.Register("tts", &ctrl.Options[*zero.Ctx]{
Expand All @@ -32,17 +34,18 @@ func init() { // 插件主体
})
tts := newttsmode()
enr := control.Register("aireply", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "人工智能回复",
Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱]",
DisableOnDefault: false,
Brief: "人工智能回复",
Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱|ChatGPT]\n- 设置 ChatGPT api token xxx",
PrivateDataFolder: "aireply",
})
/*************************************************************
*******************************AIreply************************
*************************************************************/
enr.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
aireply := aireply.NewAIReply(getReplyMode(ctx))
reply := message.ParseMessageFromString(aireply.Talk(ctx.ExtractPlainText(), zero.BotConfig.NickName[0]))
aireply := getReplyMode(ctx)
reply := message.ParseMessageFromString(aireply.Talk(ctx.Event.UserID, ctx.ExtractPlainText(), zero.BotConfig.NickName[0]))
// 回复
time.Sleep(time.Second * 1)
if zero.OnlyPublic(ctx) {
Expand All @@ -68,9 +71,9 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) {
msg := ctx.ExtractPlainText()
// 获取回复模式
r := aireply.NewAIReply(getReplyMode(ctx))
r := getReplyMode(ctx)
// 获取回复的文本
reply := r.TalkPlain(msg, zero.BotConfig.NickName[0])
reply := r.TalkPlain(ctx.Event.UserID, msg, zero.BotConfig.NickName[0])
// 获取语音
index := tts.getSoundMode(ctx)
record := message.Record(fmt.Sprintf(cnapi, index, url.QueryEscape(
Expand Down Expand Up @@ -152,4 +155,25 @@ 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) {
token := ctx.State["regex_matched"].([]string)[1]
f, err := os.Create(ent.DataFolder() + "chatgpt.txt")
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
defer f.Close()
_, err = f.WriteString(token)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
chats = aireply.NewChatGPT(&chatgpt.Config{
UA: chatgpt.UA,
SessionToken: token,
RefreshInterval: time.Hour,
Timeout: time.Minute,
})
ctx.SendChain(message.Text("设置成功"))
})
}

0 comments on commit 8399e49

Please sign in to comment.