diff --git a/README.md b/README.md index 90a99ec..d2ade19 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -## 欢迎使用`wechatgpt`微信机器人 +## 欢迎使用`wechatgpt`智能机器人,Let's Chat with ChatGPT 如果觉得不错,请麻烦点个`Star`,非常感谢。

-Version +Version License: MIT @@ -36,6 +36,9 @@ chatgpt: go run main.go ``` + + +## 微信 ``` ain.go #gosetup go: downloading github.com/eatmoreapple/openwechat v1.2.1 @@ -92,7 +95,19 @@ INFO[0099] 3 ## 意外之喜 drawing -这不比对象来的贴心? +这不比对象来的贴心? + + +## telegram机器人使用方式 +- 方式1: 直接添加小莫的bot进行使用 +![chatbot](screenshots/telegram.png) + +- 方式2:自己部署 +修改 config下的 chatgpt.telegram的token后运行`go run main.go`进行启动 +`token`获取方式,请在telegram中添加好友`@botFather`并按提示操作 + +![telegram](screenshots/IMG_3991.png) + ## 变爸爸事件 放在B站 diff --git a/bootstrap/telegram.go b/bootstrap/telegram.go new file mode 100644 index 0000000..97bbc8f --- /dev/null +++ b/bootstrap/telegram.go @@ -0,0 +1,52 @@ +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" + "time" +) + +func StartTelegramBot() { + getConfig := config.GetConfig() + if getConfig == nil { + return + } + botConfig := getConfig.ChatGpt + bot, err := tgbotapi.NewBotAPI(botConfig.Telegram) + if err != nil { + return + } + + bot.Debug = true + + log.Printf("Authorized on account %s", bot.Self.UserName) + u := tgbotapi.NewUpdate(0) + + updates := bot.GetUpdatesChan(u) + time.Sleep(time.Millisecond * 500) + for len(updates) != 0 { + <-updates + } + + for update := range updates { + if update.Message == nil { + continue + } + text := update.Message.Text + chatID := update.Message.Chat.ID + responseMsg := telegram.Handle(text) + if responseMsg == nil { + continue + } + msg := tgbotapi.NewMessage(chatID, *responseMsg) + send, err := bot.Send(msg) + if err != nil { + log.Errorf("发送消息出错:%s", err.Error()) + return + } + fmt.Println(send.Text) + } +} diff --git a/bootstrap/wechat.go b/bootstrap/wechat.go index 83e14ae..7229403 100644 --- a/bootstrap/wechat.go +++ b/bootstrap/wechat.go @@ -3,7 +3,7 @@ package bootstrap import ( "github.com/eatmoreapple/openwechat" log "github.com/sirupsen/logrus" - "github.com/wechatgpt/wechatbot/wechat" + "github.com/wechatgpt/wechatbot/handler/wechat" ) func StartWebChat() { diff --git a/config/config.go b/config/config.go index bf6303f..19acffa 100644 --- a/config/config.go +++ b/config/config.go @@ -8,16 +8,12 @@ var config *Config type Config struct { ChatGpt ChatGptConfig `json:"chatgpt"` - Slack SlackConfig `json:"slack"` } type ChatGptConfig struct { - Keyword string `json:"keyword,omitempty"` - Token string `json:"token,omitempty" json:"token,omitempty"` -} - -type SlackConfig struct { - Token string `json:"token"` + Keyword string `json:"keyword,omitempty"` + Token string `json:"token,omitempty" json:"token,omitempty"` + Telegram string `json:"telegram"` } func LoadConfig() error { diff --git a/config/config.yaml.example b/config/config.yaml.example index dd6e523..f18ae5e 100644 --- a/config/config.yaml.example +++ b/config/config.yaml.example @@ -1,3 +1,4 @@ chatgpt: keyword: chatgpt - token: your token \ No newline at end of file + token: your token + telegram: your telegram token \ No newline at end of file diff --git a/go.mod b/go.mod index cb9ce87..aebb163 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.16 require ( github.com/eatmoreapple/openwechat v1.2.1 + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 github.com/sirupsen/logrus v1.6.0 - github.com/slack-go/slack v0.11.4 github.com/spf13/viper v1.14.0 ) diff --git a/go.sum b/go.sum index e078c36..79a2bb6 100644 --- a/go.sum +++ b/go.sum @@ -257,8 +257,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9 github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho= -github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= @@ -349,8 +349,6 @@ github.com/googleapis/gax-go/v2 v2.5.1/go.mod h1:h6B0KMMFNtI2ddbGJn3T3ZbwkeT6yqE github.com/googleapis/gax-go/v2 v2.6.0/go.mod h1:1mjbznJAPHFpesgE5ucqfYEscaz5kMdcIDwU/6+DDoY= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/consul/api v1.15.3/go.mod h1:/g/qgcoBcEXALCNZgRRisyTW0nY86++L0KbeAMXYCeY= @@ -492,8 +490,6 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/slack-go/slack v0.11.4 h1:ojSa7KlPm3PqY2AomX4VTxEsK5eci5JaxCjlzGV5zoM= -github.com/slack-go/slack v0.11.4/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw= github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= diff --git a/handler/telegram/telegram.go b/handler/telegram/telegram.go new file mode 100644 index 0000000..8a823c1 --- /dev/null +++ b/handler/telegram/telegram.go @@ -0,0 +1,21 @@ +package telegram + +import ( + log "github.com/sirupsen/logrus" + "github.com/wechatgpt/wechatbot/config" + "github.com/wechatgpt/wechatbot/openai" + "strings" +) + +func Handle(msg string) *string { + appConfig := config.GetConfig() + if appConfig == nil { + return nil + } + requestText := strings.TrimSpace(msg) + reply, err := openai.Completions(requestText, appConfig.ChatGpt.Token) + if err != nil { + log.Println(err) + } + return reply +} diff --git a/wechat/handler.go b/handler/wechat/handler.go similarity index 100% rename from wechat/handler.go rename to handler/wechat/handler.go diff --git a/wechat/wechat_handler.go b/handler/wechat/wechat_handler.go similarity index 100% rename from wechat/wechat_handler.go rename to handler/wechat/wechat_handler.go diff --git a/main.go b/main.go index 7c22ad4..c5ff2c8 100644 --- a/main.go +++ b/main.go @@ -10,5 +10,6 @@ func main() { if err != nil { panic(err) } + bootstrap.StartTelegramBot() bootstrap.StartWebChat() } diff --git a/screenshots/IMG_3991.png b/screenshots/IMG_3991.png new file mode 100644 index 0000000..fb73c57 Binary files /dev/null and b/screenshots/IMG_3991.png differ diff --git a/screenshots/telegram.png b/screenshots/telegram.png new file mode 100644 index 0000000..f560397 Binary files /dev/null and b/screenshots/telegram.png differ