Skip to content

Commit

Permalink
Merge pull request #73 from yongxin-ms/main
Browse files Browse the repository at this point in the history
提供了对gpt-3.5-turbo的支持
  • Loading branch information
houko authored Mar 6, 2023
2 parents eafbe2c + 556c345 commit d840fc9
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 728 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
with:
push: true
platforms: linux/amd64,linux/arm64
tags: xiaomoinfo/wechatgpt:2.9.7,xiaomoinfo/wechatgpt:latest
tags: xiaomoinfo/wechatgpt:3.0.0,xiaomoinfo/wechatgpt:latest
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea
local/config.yaml
token.json
wechatbot
wechatbot.exe
run.log
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
31 changes: 21 additions & 10 deletions bootstrap/telegram.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package bootstrap

import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/config"
"github.com/wechatgpt/wechatbot/handler/telegram"
"github.com/wechatgpt/wechatbot/utils"
"strings"
"time"

"wechatbot/config"
"wechatbot/handler/telegram"
"wechatbot/utils"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
log "github.com/sirupsen/logrus"
)

func StartTelegramBot() {
log.Info("Start Telegram Bot")
telegramKey := config.GetTelegram()
if telegramKey == nil {
log.Info("未找到tg token,不启动tg tot")
log.Info("未找到tg token,不启动tg bot")
return
}

bot, err := tgbotapi.NewBotAPI(*telegramKey)
if err != nil {
log.Error("tg bot 启动失败:", err.Error())
Expand All @@ -37,6 +40,7 @@ func StartTelegramBot() {
if update.Message == nil {
continue
}

text := update.Message.Text
chatID := update.Message.Chat.ID
chatUserName := update.Message.Chat.UserName
Expand Down Expand Up @@ -68,26 +72,33 @@ func StartTelegramBot() {
if len(key) == 0 {
continue
}

splitItems := strings.Split(content, key)
if len(splitItems) < 2 {
continue
}

requestText := strings.TrimSpace(splitItems[1])
log.Println("问题:", requestText)
log.Info("问题:", requestText)
reply = telegram.Handle(requestText)
} else {
log.Info("问题:", text)
reply = telegram.Handle(text)
}

if reply == nil {
continue
}

msg := tgbotapi.NewMessage(chatID, *reply)
send, err := bot.Send(msg)
_, err := bot.Send(msg)
if err != nil {
log.Errorf("发送消息出错:%s", err.Error())
continue
}
fmt.Println(send.Text)

log.Info("回答:", *reply)
}

select {}
}
11 changes: 8 additions & 3 deletions bootstrap/wechat.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package bootstrap

import (
"os"

"wechatbot/handler/wechat"

"github.com/eatmoreapple/openwechat"
log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/handler/wechat"
"os"
)

func StartWebChat() {
log.Info("Start WebChat Bot")
bot := openwechat.DefaultBot(openwechat.Desktop)
bot.MessageHandler = wechat.Handler
bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
Expand All @@ -19,6 +22,7 @@ func StartWebChat() {
if err != nil {
return
}

reloadStorage = openwechat.NewJsonFileHotReloadStorage("token.json")
err = bot.HotLogin(reloadStorage)
if err != nil {
Expand All @@ -34,14 +38,15 @@ func StartWebChat() {
}

friends, err := self.Friends()

for i, friend := range friends {
log.Println(i, friend)
}

groups, err := self.Groups()
for i, group := range groups {
log.Println(i, group)
}

err = bot.Block()
if err != nil {
log.Fatal(err)
Expand Down
19 changes: 16 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ func LoadConfig() error {
if err := viper.ReadInConfig(); err != nil {
return err
}

if err := viper.Unmarshal(&config); err != nil {
return err
}

return nil
}

func GetWechat() *string {
wechat := getEnv("wechat")

if wechat != nil {
return wechat
}

if config == nil {
return nil
}

if wechat == nil {
wechat = config.ChatGpt.Wechat
}
Expand All @@ -58,9 +61,11 @@ func GetWechatKeyword() *string {
if keyword != nil {
return keyword
}

if config == nil {
return nil
}

if keyword == nil {
keyword = config.ChatGpt.WechatKeyword
}
Expand All @@ -72,9 +77,11 @@ func GetTelegram() *string {
if tg != nil {
return tg
}

if config == nil {
return nil
}

if tg == nil {
tg = config.ChatGpt.Telegram
}
Expand All @@ -87,9 +94,11 @@ func GetTelegramKeyword() *string {
if tgKeyword != nil {
return tgKeyword
}

if config == nil {
return nil
}

if tgKeyword == nil {
tgKeyword = config.ChatGpt.TgKeyword
}
Expand All @@ -102,9 +111,11 @@ func GetTelegramWhitelist() *string {
if tgWhitelist != nil {
return tgWhitelist
}

if config == nil {
return nil
}

if tgWhitelist == nil {
tgWhitelist = config.ChatGpt.TgWhitelist
}
Expand All @@ -113,14 +124,14 @@ func GetTelegramWhitelist() *string {

func GetOpenAiApiKey() *string {
apiKey := getEnv("api_key")

if apiKey != nil {
return apiKey
}

if config == nil {
return nil
}

if apiKey == nil {
apiKey = &config.ChatGpt.Token
}
Expand All @@ -143,7 +154,9 @@ func getEnv(key string) *string {

if len(value) > 0 {
return &value
} else if config.ChatGpt.WechatKeyword != nil {
}

if config.ChatGpt.WechatKeyword != nil {
value = *config.ChatGpt.WechatKeyword
}
return nil
Expand Down
27 changes: 22 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
module github.com/wechatgpt/wechatbot
module wechatbot

go 1.16
go 1.20

require (
github.com/eatmoreapple/openwechat v1.2.1
github.com/eatmoreapple/openwechat v1.4.1
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/sirupsen/logrus v1.6.0
github.com/spf13/viper v1.14.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.15.0
)

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/pelletier/go-toml/v2 v2.0.6 // 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/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit d840fc9

Please sign in to comment.