gpt唤醒词配置

This commit is contained in:
rainerosion 2023-04-14 16:49:24 +08:00
parent cf4ac6d173
commit 2d43dd8d3b
4 changed files with 26 additions and 9 deletions

View File

@ -2,4 +2,6 @@
"api_key": "", # chatgpt api key "api_key": "", # chatgpt api key
"auto_pass": true, # 是否自动通过好友申请 "auto_pass": true, # 是否自动通过好友申请
"bing_chat_url": "" # New Bing 聊天接口 "bing_chat_url": "" # New Bing 聊天接口
"bing_chat_wake_word": "#bing", # new Bing唤醒词
"gpt_chat_wake_word": "#gpt" # ChatGPT唤醒词
} }

View File

@ -13,9 +13,11 @@ type Configuration struct {
ApiKey string `json:"api_key"` ApiKey string `json:"api_key"`
// 自动通过好友 // 自动通过好友
AutoPass bool `json:"auto_pass"` AutoPass bool `json:"auto_pass"`
// bing 聊天接口 // bing 聊天接口
BingChatUrl string `json:"bing_chat_url"` BingChatUrl string `json:"bing_chat_url"`
// 机器人唤醒词
BingChatWakeWord string `json:"bing_chat_wake_word"`
GptChatWakeWord string `json:"gpt_chat_wake_word"`
} }
var config *Configuration var config *Configuration

View File

@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"github.com/bujnlc8/wechatbot/config"
"log" "log"
"strings" "strings"
@ -16,10 +17,12 @@ type GroupMessageHandler struct {
// handle 处理消息 // handle 处理消息
func (g *GroupMessageHandler) handle(msg *openwechat.Message) error { func (g *GroupMessageHandler) handle(msg *openwechat.Message) error {
bingWakeWord := config.LoadConfig().BingChatWakeWord
gptWakeWord := config.LoadConfig().GptChatWakeWord
if msg.IsText() { if msg.IsText() {
return g.ReplyText(msg) return g.ReplyText(msg)
} else { } else {
if strings.Contains(msg.Content, "@GPTBot") || strings.Contains(msg.Content, "@bing") { if strings.Contains(msg.Content, gptWakeWord) || strings.Contains(msg.Content, bingWakeWord) {
msg.ReplyText("目前我只支持文字哦~") msg.ReplyText("目前我只支持文字哦~")
} }
} }
@ -40,15 +43,18 @@ func (g *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
group := openwechat.Group{User: sender} group := openwechat.Group{User: sender}
log.Printf("Received Group %v Text Msg : %v", group.NickName, msg.Content) log.Printf("Received Group %v Text Msg : %v", group.NickName, msg.Content)
bingWakeWord := config.LoadConfig().BingChatWakeWord
gptWakeWord := config.LoadConfig().GptChatWakeWord
// @GPTBot 或者 @bing的消息才处理 // @GPTBot 或者 @bing的消息才处理
if !(strings.Contains(msg.Content, "@GPTBot") || strings.Contains(msg.Content, "@bing")) { if !(strings.Contains(msg.Content, gptWakeWord) || strings.Contains(msg.Content, bingWakeWord)) {
return nil return nil
} }
requestText := strings.TrimSpace(strings.ReplaceAll(msg.Content, "@GPTBot", "")) requestText := strings.TrimSpace(strings.ReplaceAll(msg.Content, gptWakeWord, ""))
var reply = "" var reply = ""
if strings.Contains(msg.Content, "@bing") { if strings.Contains(msg.Content, bingWakeWord) {
requestText = strings.TrimSpace(strings.ReplaceAll(msg.Content, "@bing", "")) requestText = strings.TrimSpace(strings.ReplaceAll(msg.Content, bingWakeWord, ""))
reply, err = gpt.BingSearch(requestText, group.UserName) reply, err = gpt.BingSearch(requestText, group.UserName)
if reply != "" && strings.HasPrefix(reply, "[") { if reply != "" && strings.HasPrefix(reply, "[") {
reply = "\n" + reply reply = "\n" + reply

View File

@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"github.com/bujnlc8/wechatbot/config"
"log" "log"
"strings" "strings"
@ -43,12 +44,18 @@ func (g *UserMessageHandler) ReplyText(msg *openwechat.Message) error {
requestText := strings.TrimSpace(msg.Content) requestText := strings.TrimSpace(msg.Content)
requestText = strings.Trim(msg.Content, "\n") requestText = strings.Trim(msg.Content, "\n")
bingWakeWord := config.LoadConfig().BingChatWakeWord
gptWakeWord := config.LoadConfig().GptChatWakeWord
var reply = "" var reply = ""
if strings.Contains(msg.Content, "@bing") { if strings.Contains(msg.Content, bingWakeWord) {
requestText = strings.TrimSpace(strings.ReplaceAll(msg.Content, "@bing", "")) requestText = strings.TrimSpace(strings.ReplaceAll(msg.Content, bingWakeWord, ""))
reply, err = gpt.BingSearch(requestText, sender.UserName) reply, err = gpt.BingSearch(requestText, sender.UserName)
} else { } else if strings.Contains(msg.Content, gptWakeWord) {
requestText = strings.TrimSpace(strings.ReplaceAll(msg.Content, gptWakeWord, ""))
reply, err = gpt.Completions(requestText, sender.UserName) reply, err = gpt.Completions(requestText, sender.UserName)
} else {
//可以考虑对接其他机器人恢复信息
} }
if err != nil { if err != nil {
log.Printf("gpt request error: %v \n", err) log.Printf("gpt request error: %v \n", err)