Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.dev.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"api_key": "your api key",
"auto_pass": true
"auto_pass": true,
"proxy_url": ""
}
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Configuration struct {
ApiKey string `json:"api_key"`
// 自动通过好友
AutoPass bool `json:"auto_pass"`
// 代理地址
ProxyURL string `json:"proxy_url"`
}

var config *Configuration
Expand All @@ -39,12 +41,16 @@ func LoadConfig() *Configuration {
// 如果环境变量有配置,读取环境变量
ApiKey := os.Getenv("ApiKey")
AutoPass := os.Getenv("AutoPass")
HttpProxy := os.Getenv("HTTP_PROXY")
if ApiKey != "" {
config.ApiKey = ApiKey
}
if AutoPass == "true" {
config.AutoPass = true
}
if HttpProxy != "" {
config.ProxyURL = HttpProxy
}
})
return config
}
18 changes: 17 additions & 1 deletion gtp/gtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package gtp
import (
"bytes"
"encoding/json"
"github.com/869413421/wechatbot/config"
"io/ioutil"
"log"
"net/http"
"net/url"

"github.com/869413421/wechatbot/config"
)

const BASEURL = "https://api.openai.com/v1/"
Expand Down Expand Up @@ -65,6 +67,20 @@ func Completions(msg string) (string, error) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}

httpProxy := config.LoadConfig().ProxyURL
if httpProxy != "" {
uri, err := url.Parse(httpProxy)
if err != nil {
return "", err
}
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(uri),
},
}
}

response, err := client.Do(req)
if err != nil {
return "", err
Expand Down