Skip to content

Commit a05d7d3

Browse files
committed
Definite Version with complete Config
1 parent b44e5d8 commit a05d7d3

File tree

3 files changed

+84
-52
lines changed

3 files changed

+84
-52
lines changed

connect/connect.go

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@ package connect
22

33
import (
44
"fmt"
5-
zero "github.com/wdvxdr1123/ZeroBot"
6-
"github.com/wdvxdr1123/ZeroBot/driver"
7-
"github.com/wdvxdr1123/ZeroBot/message"
85
"gpt-bot/chat"
96
"gpt-bot/client"
10-
"gpt-bot/json"
7+
"gpt-bot/jsonconfig"
118
"gpt-bot/plugins/weather"
129
"strings"
1310
"time"
11+
12+
zero "github.com/wdvxdr1123/ZeroBot"
13+
"github.com/wdvxdr1123/ZeroBot/driver"
14+
"github.com/wdvxdr1123/ZeroBot/message"
1415
)
1516

17+
var superUsers []int64
18+
1619
// Connect 连接到服务器
1720
func Connect() {
21+
1822
zero.RunAndBlock(&zero.Config{
1923
NickName: []string{"gpt-bot"},
2024
CommandPrefix: "#",
21-
SuperUsers: []int64{},
25+
SuperUsers: superUsers,
2226
Driver: []zero.Driver{
2327
driver.NewWebSocketClient("ws://127.0.0.1:8080/", ""),
2428
},
@@ -28,11 +32,12 @@ func Connect() {
2832
func init() {
2933
engine := zero.New()
3034
// 读取配置文件
31-
config, err := json.LoadConfig("config.json")
35+
config, err := jsonconfig.LoadConfig("config.json")
3236
if err != nil {
3337
fmt.Println("Error loading config.json:", err)
3438
return
3539
}
40+
superUsers = config.SuperUsers
3641
// 创建客户端
3742
aClient := client.CreateClient(config.Apikey, config.Proxy)
3843
chat.SystemContent = chat.Dog
@@ -44,11 +49,10 @@ func init() {
4449
#help 获取帮助信息
4550
#time 获取当前时间
4651
#weather 获取当前天气,命令后跟着城市名称,例如:#weather北京
47-
#role 修改人工智能角色,目前支持的角色有:狗狗,猫娘,原版,默认为狗狗
48-
gpt-bot将最晚在4月1日停止工作,且行且珍惜,感谢大家的使用!`))
52+
#role 修改人工智能角色,目前支持的角色有:狗狗,猫娘,原版,默认为狗狗,注意,仅允许超级用户修改人工智能角色`))
4953
})
5054
// 修改角色
51-
engine.OnCommand("role").SetBlock(true).Handle(func(ctx *zero.Ctx) {
55+
engine.OnCommand("role", zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
5256
msg := ctx.MessageString()
5357
if strings.TrimSpace(msg) == "#role" {
5458
ctx.Send(message.Text("请输入角色名称,例如:#role猫娘"))
@@ -76,22 +80,28 @@ gpt-bot将最晚在4月1日停止工作,且行且珍惜,感谢大家的使
7680
ctx.Send(message.Text("当前时间:" + currentTime.Format("2006年01月02日 15:04:05 ") + getChineseWeekday(currentTime.Weekday())))
7781
})
7882
// 天气
79-
engine.OnCommand("weather").SetBlock(true).Handle(func(ctx *zero.Ctx) {
80-
msg := ctx.MessageString()
81-
if strings.TrimSpace(msg) == "#weather" {
82-
ctx.Send(message.Text("请输入城市名称,例如:#weather北京"))
83-
return
84-
}
85-
location := strings.TrimSpace(strings.Replace(msg, "#weather", "", 1))
86-
ctx.Send(message.Text("正在获取天气信息,请稍等..."))
87-
aWeather, err := weather.GetWeather(location)
88-
if err != nil {
89-
//fmt.Println("Error getting weather:", err)
90-
ctx.Send(err)
91-
return
92-
}
93-
ctx.Send(message.Text(aWeather))
94-
})
83+
if config.WeatherApikey != "" {
84+
engine.OnCommand("weather").SetBlock(true).Handle(func(ctx *zero.Ctx) {
85+
msg := ctx.MessageString()
86+
if strings.TrimSpace(msg) == "#weather" {
87+
ctx.Send(message.Text("请输入城市名称,例如:#weather北京"))
88+
return
89+
}
90+
location := strings.TrimSpace(strings.Replace(msg, "#weather", "", 1))
91+
ctx.Send(message.Text("正在获取天气信息,请稍等..."))
92+
aWeather, err := weather.GetWeather(location, config.WeatherApikey)
93+
if err != nil {
94+
//fmt.Println("Error getting weather:", err)
95+
ctx.Send(message.Text(err))
96+
return
97+
}
98+
ctx.Send(message.Text(aWeather))
99+
})
100+
} else {
101+
engine.OnCommand("weather").SetBlock(true).Handle(func(ctx *zero.Ctx) {
102+
ctx.Send(message.Text("未配置天气API,无法获取天气信息"))
103+
})
104+
}
95105
// 私发消息
96106
engine.OnMessage(zero.OnlyToMe).Handle(func(ctx *zero.Ctx) {
97107
qq := ctx.Event.UserID

json/json.go renamed to jsonconfig/jsonconfig.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package json
1+
package jsonconfig
22

33
import (
44
"encoding/json"
@@ -7,27 +7,31 @@ import (
77
)
88

99
type Config struct {
10-
Apikey string `json:"apikey"`
11-
Model string `json:"model"`
12-
Proxy string `json:"proxy"`
13-
MaxTokens int `json:"max_tokens"`
10+
Apikey string `json:"apikey"`
11+
Model string `json:"model"`
12+
Proxy string `json:"proxy"`
13+
MaxTokens int `json:"max_tokens"`
14+
SuperUsers []int64 `json:"super_users"`
15+
WeatherApikey string `json:"weather_apikey"`
1416
}
1517

1618
// LoadConfig 从文件中读取配置,如果文件不存在则返回默认配置
1719
func LoadConfig(filename string) (*Config, error) {
1820
config := &Config{
19-
Apikey: "your api key",
20-
Model: "gpt-3.5-turbo",
21-
Proxy: "http://localhost:7890",
22-
MaxTokens: 800,
21+
Apikey: "your api key",
22+
Model: "gpt-3.5-turbo",
23+
Proxy: "http://localhost:7890",
24+
MaxTokens: 800,
25+
SuperUsers: []int64{},
26+
WeatherApikey: "",
2327
}
2428

2529
// 如果文件不存在,则创建文件并写入默认配置
2630
if _, err := os.Stat(filename); os.IsNotExist(err) {
2731
if err := SaveConfig(filename, config); err != nil {
2832
return nil, err
2933
}
30-
panic("Please edit the config.json file and restart the program.")
34+
return nil, err
3135
}
3236

3337
// 读取文件并解析为配置项

plugins/weather/weather.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,58 @@ package weather
22

33
import (
44
"compress/gzip"
5-
libjson "encoding/json"
5+
"encoding/json"
66
"fmt"
7+
"io"
78
"io/ioutil"
89
"net/http"
910
"net/url"
1011
"strings"
1112
)
1213

13-
func GetWeather(location string) (string, error) {
14-
// 构造API请求URL
15-
apiKey := ""
16-
locationID, locationName, err := getGeo(location)
14+
func GetWeather(location, apiKey string) (string, error) {
15+
locationID, locationName, err := getGeo(location, apiKey)
1716
if err != nil {
1817
//fmt.Println("获取地理位置信息失败:", err)
1918
return "", err
2019
}
21-
url := fmt.Sprintf("https://devapi.qweather.com/v7/weather/3d?location=%s&key=%s", locationID, apiKey)
20+
Url := fmt.Sprintf("https://devapi.qweather.com/v7/weather/3d?location=%s&key=%s", locationID, apiKey)
2221
// 发送HTTP请求
23-
resp, err := http.Get(url)
22+
resp, err := http.Get(Url)
2423
if err != nil {
2524
//fmt.Println("发送HTTP请求失败:", err)
2625
return "", err
2726
}
28-
defer resp.Body.Close()
27+
defer func(Body io.ReadCloser) {
28+
err := Body.Close()
29+
if err != nil {
30+
fmt.Println("关闭HTTP响应Body失败:", err)
31+
}
32+
}(resp.Body)
2933
var data map[string]interface{}
3034
if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
3135
// 如果是gzip压缩,则使用gzip.Reader解压缩响应数据
3236
gzipReader, err := gzip.NewReader(resp.Body)
3337
if err != nil {
3438
return "", fmt.Errorf("解压缩失败:%v", err)
3539
}
36-
defer gzipReader.Close()
40+
defer func(gzipReader *gzip.Reader) {
41+
err := gzipReader.Close()
42+
if err != nil {
43+
fmt.Println("关闭gzip.Reader失败:", err)
44+
}
45+
}(gzipReader)
3746
jsonData, err := ioutil.ReadAll(gzipReader)
3847
if err != nil {
3948
return "", fmt.Errorf("读取API响应失败:%v", err)
4049
}
41-
err = libjson.Unmarshal(jsonData, &data)
50+
err = json.Unmarshal(jsonData, &data)
4251
if err != nil {
4352
return "", fmt.Errorf("解析JSON失败:%v", err)
4453
}
4554
} else {
4655
// 如果响应未经过gzip压缩,则直接读取并解析JSON
47-
err = libjson.NewDecoder(resp.Body).Decode(&data)
56+
err = json.NewDecoder(resp.Body).Decode(&data)
4857
if err != nil {
4958
return "", fmt.Errorf("解析JSON失败:%v", err)
5059
}
@@ -71,16 +80,20 @@ func GetWeather(location string) (string, error) {
7180
}
7281
}
7382

74-
func getGeo(location string) (string, string, error) {
75-
apiKey := ""
83+
func getGeo(location, apiKey string) (string, string, error) {
7684
// 构建请求URL
7785
Url := fmt.Sprintf("https://geoapi.qweather.com/v2/city/lookup?location=%s&key=%s", url.QueryEscape(location), apiKey)
7886
// 发送HTTP GET请求
7987
resp, err := http.Get(Url)
8088
if err != nil {
8189
return "", "", fmt.Errorf("发送HTTP请求失败:%v", err)
8290
}
83-
defer resp.Body.Close()
91+
defer func(Body io.ReadCloser) {
92+
err := Body.Close()
93+
if err != nil {
94+
fmt.Println("关闭HTTP响应Body失败:", err)
95+
}
96+
}(resp.Body)
8497
// 读取响应数据并解析JSON
8598
var result map[string]interface{}
8699
// 检查响应是否为gzip压缩
@@ -90,18 +103,23 @@ func getGeo(location string) (string, string, error) {
90103
if err != nil {
91104
return "", "", fmt.Errorf("解压缩失败:%v", err)
92105
}
93-
defer gzipReader.Close()
106+
defer func(gzipReader *gzip.Reader) {
107+
err := gzipReader.Close()
108+
if err != nil {
109+
fmt.Println("关闭gzip.Reader失败:", err)
110+
}
111+
}(gzipReader)
94112
jsonData, err := ioutil.ReadAll(gzipReader)
95113
if err != nil {
96114
return "", "", fmt.Errorf("读取API响应失败:%v", err)
97115
}
98-
err = libjson.Unmarshal(jsonData, &result)
116+
err = json.Unmarshal(jsonData, &result)
99117
if err != nil {
100118
return "", "", fmt.Errorf("解析JSON失败:%v", err)
101119
}
102120
} else {
103121
// 如果响应未经过gzip压缩,则直接读取并解析JSON
104-
err = libjson.NewDecoder(resp.Body).Decode(&result)
122+
err = json.NewDecoder(resp.Body).Decode(&result)
105123
if err != nil {
106124
return "", "", fmt.Errorf("解析JSON失败:%v", err)
107125
}

0 commit comments

Comments
 (0)