forked from askfname/gpt4freets_wechat_robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
173 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"api_key": "your api key", | ||
"auto_pass": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"os" | ||
"sync" | ||
) | ||
|
||
// Configuration 项目配置 | ||
type Configuration struct { | ||
// gtp apikey | ||
ApiKey string `json:"api_key"` | ||
// 自动通过好友 | ||
AutoPass bool `json:"auto_pass"` | ||
} | ||
|
||
var config *Configuration | ||
var once sync.Once | ||
|
||
// LoadConfig 加载配置 | ||
func LoadConfig() *Configuration { | ||
once.Do(func() { | ||
// 从文件中读取 | ||
config = &Configuration{} | ||
f, err := os.Open("config.json") | ||
if err != nil { | ||
log.Fatalf("open config err: %v", err) | ||
return | ||
} | ||
defer f.Close() | ||
encoder := json.NewDecoder(f) | ||
err = encoder.Decode(config) | ||
if err != nil { | ||
log.Fatalf("decode config err: %v", err) | ||
return | ||
} | ||
|
||
// 如果环境变量有配置,读取环境变量 | ||
ApiKey := os.Getenv("ApiKey") | ||
AutoPass := os.Getenv("AutoPass") | ||
if ApiKey != "" { | ||
config.ApiKey = ApiKey | ||
} | ||
if AutoPass == "true" { | ||
config.AutoPass = true | ||
} | ||
}) | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package handlers | ||
|
||
import ( | ||
"github.com/869413421/wechatbot/gtp" | ||
"github.com/eatmoreapple/openwechat" | ||
"log" | ||
"strings" | ||
) | ||
|
||
var _ MessageHandlerInterface = (*UserMessageHandler)(nil) | ||
|
||
// UserMessageHandler 私聊消息处理 | ||
type UserMessageHandler struct { | ||
} | ||
|
||
// handle 处理消息 | ||
func (g *UserMessageHandler) handle(msg *openwechat.Message) error { | ||
if msg.IsText() { | ||
return g.ReplyText(msg) | ||
} | ||
return nil | ||
} | ||
|
||
// NewUserMessageHandler 创建私聊处理器 | ||
func NewUserMessageHandler() MessageHandlerInterface { | ||
return &UserMessageHandler{} | ||
} | ||
|
||
// ReplyText 发送文本消息到群 | ||
func (g *UserMessageHandler) ReplyText(msg *openwechat.Message) error { | ||
// 接收私聊消息 | ||
sender, err := msg.Sender() | ||
log.Printf("Received User %v Text Msg : %v", sender.NickName, msg.Content) | ||
|
||
// 向GPT发起请求 | ||
requestText := strings.TrimSpace(msg.Content) | ||
requestText = strings.Trim(msg.Content, "\n") | ||
reply, err := gtp.Completions(requestText) | ||
if err != nil { | ||
log.Printf("gtp request error: %v \n", err) | ||
msg.ReplyText("机器人神了,我一会发现了就去修。") | ||
return err | ||
} | ||
if reply == "" { | ||
return nil | ||
} | ||
|
||
// 回复用户 | ||
reply = strings.TrimSpace(reply) | ||
reply = strings.Trim(reply, "\n") | ||
_, err = msg.ReplyText(reply) | ||
if err != nil { | ||
log.Printf("response user error: %v \n", err) | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters