VSCode调试环境支持

This commit is contained in:
YongXin 2023-02-22 14:57:23 +08:00
parent eafbe2c459
commit 143427d04c
9 changed files with 73 additions and 16 deletions

18
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}",
"cwd": "${workspaceRoot}",
"env": {},
"args": []
}
]
}

View File

@ -2,13 +2,14 @@ package bootstrap
import ( import (
"fmt" "fmt"
"strings"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/config" "github.com/wechatgpt/wechatbot/config"
"github.com/wechatgpt/wechatbot/handler/telegram" "github.com/wechatgpt/wechatbot/handler/telegram"
"github.com/wechatgpt/wechatbot/utils" "github.com/wechatgpt/wechatbot/utils"
"strings"
"time"
) )
func StartTelegramBot() { func StartTelegramBot() {
@ -17,6 +18,7 @@ func StartTelegramBot() {
log.Info("未找到tg token,不启动tg tot") log.Info("未找到tg token,不启动tg tot")
return return
} }
bot, err := tgbotapi.NewBotAPI(*telegramKey) bot, err := tgbotapi.NewBotAPI(*telegramKey)
if err != nil { if err != nil {
log.Error("tg bot 启动失败:", err.Error()) log.Error("tg bot 启动失败:", err.Error())
@ -37,6 +39,7 @@ func StartTelegramBot() {
if update.Message == nil { if update.Message == nil {
continue continue
} }
text := update.Message.Text text := update.Message.Text
chatID := update.Message.Chat.ID chatID := update.Message.Chat.ID
chatUserName := update.Message.Chat.UserName chatUserName := update.Message.Chat.UserName
@ -68,26 +71,32 @@ func StartTelegramBot() {
if len(key) == 0 { if len(key) == 0 {
continue continue
} }
splitItems := strings.Split(content, key) splitItems := strings.Split(content, key)
if len(splitItems) < 2 { if len(splitItems) < 2 {
continue continue
} }
requestText := strings.TrimSpace(splitItems[1]) requestText := strings.TrimSpace(splitItems[1])
log.Println("问题:", requestText) log.Println("问题:", requestText)
reply = telegram.Handle(requestText) reply = telegram.Handle(requestText)
} else { } else {
reply = telegram.Handle(text) reply = telegram.Handle(text)
} }
if reply == nil { if reply == nil {
continue continue
} }
msg := tgbotapi.NewMessage(chatID, *reply) msg := tgbotapi.NewMessage(chatID, *reply)
send, err := bot.Send(msg) send, err := bot.Send(msg)
if err != nil { if err != nil {
log.Errorf("发送消息出错:%s", err.Error()) log.Errorf("发送消息出错:%s", err.Error())
continue continue
} }
fmt.Println(send.Text) fmt.Println(send.Text)
} }
select {} select {}
} }

View File

@ -1,10 +1,11 @@
package bootstrap package bootstrap
import ( import (
"os"
"github.com/eatmoreapple/openwechat" "github.com/eatmoreapple/openwechat"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/handler/wechat" "github.com/wechatgpt/wechatbot/handler/wechat"
"os"
) )
func StartWebChat() { func StartWebChat() {
@ -19,6 +20,7 @@ func StartWebChat() {
if err != nil { if err != nil {
return return
} }
reloadStorage = openwechat.NewJsonFileHotReloadStorage("token.json") reloadStorage = openwechat.NewJsonFileHotReloadStorage("token.json")
err = bot.HotLogin(reloadStorage) err = bot.HotLogin(reloadStorage)
if err != nil { if err != nil {
@ -34,14 +36,15 @@ func StartWebChat() {
} }
friends, err := self.Friends() friends, err := self.Friends()
for i, friend := range friends { for i, friend := range friends {
log.Println(i, friend) log.Println(i, friend)
} }
groups, err := self.Groups() groups, err := self.Groups()
for i, group := range groups { for i, group := range groups {
log.Println(i, group) log.Println(i, group)
} }
err = bot.Block() err = bot.Block()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -31,21 +31,24 @@ func LoadConfig() error {
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
return err return err
} }
if err := viper.Unmarshal(&config); err != nil { if err := viper.Unmarshal(&config); err != nil {
return err return err
} }
return nil return nil
} }
func GetWechat() *string { func GetWechat() *string {
wechat := getEnv("wechat") wechat := getEnv("wechat")
if wechat != nil { if wechat != nil {
return wechat return wechat
} }
if config == nil { if config == nil {
return nil return nil
} }
if wechat == nil { if wechat == nil {
wechat = config.ChatGpt.Wechat wechat = config.ChatGpt.Wechat
} }
@ -58,9 +61,11 @@ func GetWechatKeyword() *string {
if keyword != nil { if keyword != nil {
return keyword return keyword
} }
if config == nil { if config == nil {
return nil return nil
} }
if keyword == nil { if keyword == nil {
keyword = config.ChatGpt.WechatKeyword keyword = config.ChatGpt.WechatKeyword
} }
@ -72,9 +77,11 @@ func GetTelegram() *string {
if tg != nil { if tg != nil {
return tg return tg
} }
if config == nil { if config == nil {
return nil return nil
} }
if tg == nil { if tg == nil {
tg = config.ChatGpt.Telegram tg = config.ChatGpt.Telegram
} }
@ -87,9 +94,11 @@ func GetTelegramKeyword() *string {
if tgKeyword != nil { if tgKeyword != nil {
return tgKeyword return tgKeyword
} }
if config == nil { if config == nil {
return nil return nil
} }
if tgKeyword == nil { if tgKeyword == nil {
tgKeyword = config.ChatGpt.TgKeyword tgKeyword = config.ChatGpt.TgKeyword
} }
@ -102,9 +111,11 @@ func GetTelegramWhitelist() *string {
if tgWhitelist != nil { if tgWhitelist != nil {
return tgWhitelist return tgWhitelist
} }
if config == nil { if config == nil {
return nil return nil
} }
if tgWhitelist == nil { if tgWhitelist == nil {
tgWhitelist = config.ChatGpt.TgWhitelist tgWhitelist = config.ChatGpt.TgWhitelist
} }
@ -113,7 +124,6 @@ func GetTelegramWhitelist() *string {
func GetOpenAiApiKey() *string { func GetOpenAiApiKey() *string {
apiKey := getEnv("api_key") apiKey := getEnv("api_key")
if apiKey != nil { if apiKey != nil {
return apiKey return apiKey
} }
@ -121,6 +131,7 @@ func GetOpenAiApiKey() *string {
if config == nil { if config == nil {
return nil return nil
} }
if apiKey == nil { if apiKey == nil {
apiKey = &config.ChatGpt.Token apiKey = &config.ChatGpt.Token
} }
@ -143,7 +154,9 @@ func getEnv(key string) *string {
if len(value) > 0 { if len(value) > 0 {
return &value return &value
} else if config.ChatGpt.WechatKeyword != nil { }
if config.ChatGpt.WechatKeyword != nil {
value = *config.ChatGpt.WechatKeyword value = *config.ChatGpt.WechatKeyword
} }
return nil return nil

View File

@ -1,9 +1,10 @@
package telegram package telegram
import ( import (
"strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/openai" "github.com/wechatgpt/wechatbot/openai"
"strings"
) )
func Handle(msg string) *string { func Handle(msg string) *string {

View File

@ -2,12 +2,13 @@ package wechat
import ( import (
"fmt" "fmt"
"strings"
"github.com/eatmoreapple/openwechat" "github.com/eatmoreapple/openwechat"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/config" "github.com/wechatgpt/wechatbot/config"
"github.com/wechatgpt/wechatbot/openai" "github.com/wechatgpt/wechatbot/openai"
"github.com/wechatgpt/wechatbot/utils" "github.com/wechatgpt/wechatbot/utils"
"strings"
) )
var _ MessageHandlerInterface = (*GroupMessageHandler)(nil) var _ MessageHandlerInterface = (*GroupMessageHandler)(nil)
@ -19,6 +20,7 @@ func (gmh *GroupMessageHandler) handle(msg *openwechat.Message) error {
if !msg.IsText() { if !msg.IsText() {
return nil return nil
} }
return gmh.ReplyText(msg) return gmh.ReplyText(msg)
} }
@ -27,7 +29,6 @@ func NewGroupMessageHandler() MessageHandlerInterface {
} }
func (gmh *GroupMessageHandler) ReplyText(msg *openwechat.Message) error { func (gmh *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
sender, err := msg.Sender() sender, err := msg.Sender()
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)
@ -39,10 +40,12 @@ func (gmh *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
if len(key) == 0 { if len(key) == 0 {
return nil return nil
} }
splitItems := strings.Split(content, key) splitItems := strings.Split(content, key)
if len(splitItems) < 2 { if len(splitItems) < 2 {
return nil return nil
} }
requestText = strings.TrimSpace(splitItems[1]) requestText = strings.TrimSpace(splitItems[1])
} }
@ -61,18 +64,22 @@ func (gmh *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
} }
} }
} }
text, err := msg.ReplyText(fmt.Sprintf("bot error: %s", err.Error())) text, err := msg.ReplyText(fmt.Sprintf("bot error: %s", err.Error()))
log.Println(text) log.Println(text)
return err return err
} }
// 如果在提问的时候没有包含?,AI会自动在开头补充个看起来很奇怪 // 如果在提问的时候没有包含?,AI会自动在开头补充个看起来很奇怪
result := *reply result := *reply
if strings.HasPrefix(result, "?") { if strings.HasPrefix(result, "?") {
result = strings.Replace(result, "?", "", -1) result = strings.Replace(result, "?", "", -1)
} }
if strings.HasPrefix(result, "") { if strings.HasPrefix(result, "") {
result = strings.Replace(result, "", "", -1) result = strings.Replace(result, "", "", -1)
} }
// 微信不支持markdown格式所以把反引号直接去掉 // 微信不支持markdown格式所以把反引号直接去掉
if strings.Contains(result, "`") { if strings.Contains(result, "`") {
result = strings.Replace(result, "`", "", -1) result = strings.Replace(result, "`", "", -1)

View File

@ -11,6 +11,7 @@ func main() {
if err != nil { if err != nil {
log.Warn("没有找到配置文件,尝试读取环境变量") log.Warn("没有找到配置文件,尝试读取环境变量")
} }
wechatEnv := config.GetWechat() wechatEnv := config.GetWechat()
telegramEnv := config.GetTelegram() telegramEnv := config.GetTelegram()
if wechatEnv != nil && *wechatEnv == "true" { if wechatEnv != nil && *wechatEnv == "true" {

View File

@ -5,12 +5,13 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/config"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/config"
) )
// ChatGPTResponseBody 请求体 // ChatGPTResponseBody 请求体
@ -24,7 +25,7 @@ type ChatGPTResponseBody struct {
} }
type ChatGPTErrorBody struct { type ChatGPTErrorBody struct {
Error map[string]interface{} `json:"error"` Error map[string]interface{} `json:"error"`
} }
// ChatGPTRequestBody 响应体 // ChatGPTRequestBody 响应体
@ -80,6 +81,7 @@ func Completions(msg string) (*string, error) {
log.Println(err) log.Println(err)
return nil, err return nil, err
} }
log.Printf("request openai json string : %v", string(requestData)) log.Printf("request openai json string : %v", string(requestData))
req, err := http.NewRequest("POST", "https://api.openai.com/v1/completions", bytes.NewBuffer(requestData)) req, err := http.NewRequest("POST", "https://api.openai.com/v1/completions", bytes.NewBuffer(requestData))
if err != nil { if err != nil {
@ -94,6 +96,7 @@ func Completions(msg string) (*string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer func(Body io.ReadCloser) { defer func(Body io.ReadCloser) {
err := Body.Close() err := Body.Close()
if err != nil { if err != nil {
@ -113,6 +116,7 @@ func Completions(msg string) (*string, error) {
log.Println(err) log.Println(err)
return nil, err return nil, err
} }
var reply string var reply string
if len(gptResponseBody.Choices) > 0 { if len(gptResponseBody.Choices) > 0 {
for _, v := range gptResponseBody.Choices { for _, v := range gptResponseBody.Choices {
@ -128,7 +132,7 @@ func Completions(msg string) (*string, error) {
return nil, err return nil, err
} }
if (len(reply) == 0) { if len(reply) == 0 {
reply = gptErrorBody.Error["message"].(string) reply = gptErrorBody.Error["message"].(string)
} }

View File

@ -7,6 +7,7 @@ func ContainsI(a string, b string) (string, string) {
strings.ToLower(a), strings.ToLower(a),
strings.ToLower(b), strings.ToLower(b),
) )
if contain { if contain {
return strings.ToLower(a), strings.ToLower(b) return strings.ToLower(a), strings.ToLower(b)
} }