mirror of
https://github.com/tuoeg/chatgpt-wechatwork
synced 2025-12-24 11:10:55 +08:00
feat:add yaml config
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
"server/pkg/route"
|
||||
|
||||
"server/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -23,7 +25,7 @@ func startServer() {
|
||||
gin.SetMode(gin.Mode())
|
||||
r := gin.Default()
|
||||
route.InstallRoutes(r)
|
||||
serverBindAddr := fmt.Sprintf("%s:%d", "0.0.0.0", 8081)
|
||||
serverBindAddr := fmt.Sprintf("%s:%d", config.NewConfig().Server.Host, config.NewConfig().Server.Post)
|
||||
r.Run(serverBindAddr) // listen and serve
|
||||
}
|
||||
|
||||
|
||||
11
config.yaml
Normal file
11
config.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
host: "0.0.0.0"
|
||||
port: 8081
|
||||
wxConfig:
|
||||
token: "p0HrmcDC1goL"
|
||||
encoding_aeskey: "yMA8ePfLF5ChUOSDL0ZynZzA99uOoxJxLb5fhwxffiS"
|
||||
corp_id: "ww7f756c0495ad8cc4"
|
||||
corp_secret: "YFY0gcQVQQOHSKex7PIPpR58fvtjiXjrCYTlEyKgHw8"
|
||||
openAIConfig:
|
||||
model: "gpt-3.5-turbo"
|
||||
token: "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"
|
||||
49
config/config.go
Normal file
49
config/config.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var c Config
|
||||
|
||||
type Config struct {
|
||||
Server Server `yaml:"server"`
|
||||
WxConfig WxConfig `yaml:"wxConfig"`
|
||||
OpenAIConfig OpenAIConfig `yaml:"openAIConfig"`
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Host string `yaml:"host"`
|
||||
Post int `yaml:"post"`
|
||||
}
|
||||
|
||||
type WxConfig struct {
|
||||
Token string `yaml:"token"`
|
||||
EncodingAeskey string `yaml:"encoding_aeskey"`
|
||||
CorpId string `yaml:"corp_id"`
|
||||
CorpSecret string `yaml:"corp_secret"`
|
||||
}
|
||||
|
||||
type OpenAIConfig struct {
|
||||
Model string `yaml:"model"`
|
||||
Token string `yaml:"token"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yml")
|
||||
viper.AddConfigPath("/etc/appname/")
|
||||
viper.AddConfigPath("$HOME/.appname")
|
||||
viper.AddConfigPath("./")
|
||||
err := viper.ReadInConfig()
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Fatal error config file: %w \n", err))
|
||||
}
|
||||
viper.Unmarshal(&c)
|
||||
}
|
||||
|
||||
func NewConfig() Config {
|
||||
return c
|
||||
}
|
||||
15
go.mod
15
go.mod
@@ -1,12 +1,24 @@
|
||||
module server
|
||||
|
||||
go 1.17
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.9.0
|
||||
github.com/spf13/cobra v1.6.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/spf13/afero v1.9.3 // indirect
|
||||
github.com/spf13/cast v1.5.0 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/subosito/gotenv v1.4.2 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.8.0 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||
@@ -25,6 +37,7 @@ require (
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/spf13/viper v1.15.0
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.9 // indirect
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
|
||||
|
||||
@@ -2,16 +2,16 @@ package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"server/config"
|
||||
"server/pkg/wxbizmsgcrypt"
|
||||
)
|
||||
|
||||
func (s *service) Auth(msgSignature, timestamp, nonce, echostr string) (string, error) {
|
||||
wxcpt := wxbizmsgcrypt.NewWXBizMsgCrypt("p0HrmcDC1goL", "yMA8ePfLF5ChUOSDL0ZynZzA99uOoxJxLb5fhwxffiS", "ww7f756c0495ad8cc4", wxbizmsgcrypt.XmlType)
|
||||
wxcpt := wxbizmsgcrypt.NewWXBizMsgCrypt(config.NewConfig().WxConfig.Token, config.NewConfig().WxConfig.EncodingAeskey, config.NewConfig().WxConfig.CorpId, wxbizmsgcrypt.XmlType)
|
||||
res, cryptErr := wxcpt.VerifyURL(msgSignature, timestamp, nonce, echostr)
|
||||
if cryptErr != nil {
|
||||
fmt.Println(cryptErr.ErrMsg)
|
||||
return "", fmt.Errorf("Error:%s", cryptErr.ErrMsg)
|
||||
}
|
||||
fmt.Println(string(res))
|
||||
return string(res), nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"log"
|
||||
"server/config"
|
||||
"server/pkg/model"
|
||||
"server/pkg/util"
|
||||
"server/pkg/wxbizmsgcrypt"
|
||||
@@ -13,8 +14,8 @@ import (
|
||||
|
||||
func (s *service) GetToken() (string, error) {
|
||||
var tokenResponse model.TokenResponse
|
||||
corpid := "ww7f756c0495ad8cc4"
|
||||
corpsecret := "YFY0gcQVQQOHSKex7PIPpR58fvtjiXjrCYTlEyKgHw8"
|
||||
corpid := config.NewConfig().WxConfig.CorpId
|
||||
corpsecret := config.NewConfig().WxConfig.CorpSecret
|
||||
url := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", corpid, corpsecret)
|
||||
req, _, err := util.SendHTTPRequest(s.httpClient, url, "GET", nil, nil)
|
||||
if err != nil {
|
||||
@@ -28,12 +29,11 @@ func (s *service) GetToken() (string, error) {
|
||||
|
||||
func (s *service) SendMsg(msgSignature, timestamp, nonce string, msg []byte) (string, error) {
|
||||
accessToken, err := s.GetToken()
|
||||
fmt.Println(accessToken)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 校验
|
||||
wxcpt := wxbizmsgcrypt.NewWXBizMsgCrypt("p0HrmcDC1goL", "yMA8ePfLF5ChUOSDL0ZynZzA99uOoxJxLb5fhwxffiS", "ww7f756c0495ad8cc4", wxbizmsgcrypt.XmlType)
|
||||
wxcpt := wxbizmsgcrypt.NewWXBizMsgCrypt(config.NewConfig().WxConfig.Token, config.NewConfig().WxConfig.EncodingAeskey, config.NewConfig().WxConfig.CorpId, wxbizmsgcrypt.XmlType)
|
||||
msg, cryptErr := wxcpt.DecryptMsg(msgSignature, timestamp, nonce, msg)
|
||||
if cryptErr != nil {
|
||||
return "", fmt.Errorf("Decrypt msg error:%s", cryptErr.ErrMsg)
|
||||
@@ -44,7 +44,6 @@ func (s *service) SendMsg(msgSignature, timestamp, nonce string, msg []byte) (st
|
||||
if nil != err {
|
||||
return "", err
|
||||
}
|
||||
// openai解析消息
|
||||
|
||||
// 异步发送消息到企业微信
|
||||
go s.sendMsgToWx(msgContent.FromUsername, msgContent.Content, accessToken)
|
||||
@@ -148,10 +147,10 @@ func (s *service) sendMsgToWx(user, msg, token string) {
|
||||
func (s *service) sendMsgToOpenAI(msg, user string) (string, error) {
|
||||
var openAIResponse model.OpenAIResponse
|
||||
openAIRequest := model.OpenAIRequest{
|
||||
Model: "gpt-3.5-turbo",
|
||||
Model: config.NewConfig().OpenAIConfig.Model,
|
||||
Messages: []model.Message{{Role: "user", Content: msg}},
|
||||
}
|
||||
req, _, err := util.SendHTTPRequest(s.httpClient, "https://api.openai.com/v1/chat/completions", "POST", openAIRequest, &util.HTTPOptions{Token: "Bearer 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", ContentType: "application/json;charset=utf-8"})
|
||||
req, _, err := util.SendHTTPRequest(s.httpClient, "https://api.openai.com/v1/chat/completions", "POST", openAIRequest, &util.HTTPOptions{Token: fmt.Sprintf("Bearer %s", config.NewConfig().OpenAIConfig.Token), ContentType: "application/json;charset=utf-8"})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user