Skip to content

Commit

Permalink
feat: 增加删除chatgpt插件apikey
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Feb 10, 2023
1 parent 442af33 commit 709b68e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion plugins/chatgpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* 用法:发送`开始会话`即可进行连续会话,发送`结束会话`即可结束会话,或5分钟没新的提问自动结束
* 说明:需要获取apiKey,请到 [https://beta.openai.com](https://beta.openai.com) 获取
* 其他命令:
* `set chatgpt apiKey __(多个key用;符号隔开)` 设置ChatGPT的apiKey
* `set chatgpt apiKey __(多个key用;符号隔开)` 设置ChatGPT的apiKey`apiKey`可小写`apikey`
* `get chatgpt info` 获取ChatGPT的信息
* `set chatgpt apiKey __(多个key用;符号隔开)` 删除ChatGPT的apiKey,`apiKey`可小写`apikey`

## 预览:

Expand Down
19 changes: 17 additions & 2 deletions plugins/chatgpt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,12 @@ func init() {
})

// 设置openai api key
engine.OnRegex("set chatgpt apiKey (.*)", robot.OnlyPrivate, robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) {
engine.OnRegex("set chatgpt api[K|k]ey (.*)", robot.OnlyPrivate, robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) {
keys := strings.Split(ctx.State["regex_matched"].([]string)[1], ";")
failedKeys := make([]string, 0)
for i := range keys {
data := ApiKey{Key: keys[i]}
if err := db.Orm.Table("apikey").Where(&data).FirstOrCreate(&data).Error; err != nil {
ctx.ReplyTextAndAt("设置apiKey失败")
failedKeys = append(failedKeys, keys[i])
continue
}
Expand Down Expand Up @@ -154,6 +153,22 @@ func init() {
}
ctx.ReplyTextAndAt(fmt.Sprintf("插件 - ChatGPT\n%s", apiKeyMsg))
})

engine.OnRegex("del chatgpt api[K|k]ey (.*)", robot.OnlyPrivate, robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) {
keys := strings.Split(ctx.State["regex_matched"].([]string)[1], ";")
failedKeys := make([]string, 0)
for i := range keys {
if err := db.Orm.Table("apikey").Where("key = ?", keys[i]).Delete(&ApiKey{}).Error; err != nil {
failedKeys = append(failedKeys, keys[i])
continue
}
}
if len(failedKeys) > 0 {
ctx.ReplyText(fmt.Sprintf("以下apiKey删除失败: %v", failedKeys))
return
}
ctx.ReplyText("apiKey删除成功")
})
}

func askChatGPT(question string) (string, error) {
Expand Down

0 comments on commit 709b68e

Please sign in to comment.