add telegram 支持 🚀

This commit is contained in:
Evan 2022-12-12 20:35:08 +09:00
parent b6335ad8cd
commit 2837771a20
13 changed files with 101 additions and 19 deletions

View File

@ -1,8 +1,8 @@
## 欢迎使用`wechatgpt`微信机器人 ## 欢迎使用`wechatgpt`智能机器人Let's Chat with ChatGPT
如果觉得不错,请麻烦点个`Star`,非常感谢。 如果觉得不错,请麻烦点个`Star`,非常感谢。
<p> <p>
<img alt="Version" src="https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=86400" /> <img alt="Version" src="https://img.shields.io/badge/version-2.0.0-blue.svg?cacheSeconds=86400" />
<a href="#" target="_blank"> <a href="#" target="_blank">
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-green.svg" /> <img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-green.svg" />
</a> </a>
@ -36,6 +36,9 @@ chatgpt:
go run main.go go run main.go
``` ```
## 微信
``` ```
ain.go #gosetup ain.go #gosetup
go: downloading github.com/eatmoreapple/openwechat v1.2.1 go: downloading github.com/eatmoreapple/openwechat v1.2.1
@ -94,6 +97,18 @@ INFO[0099] 3 <Friend:wloscar>
这不比对象来的贴心? 这不比对象来的贴心?
## 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站 放在B站
[用chatgpt写了个微信机器人结果变爸爸了](https://www.bilibili.com/video/BV1B24y1Q7us/) [用chatgpt写了个微信机器人结果变爸爸了](https://www.bilibili.com/video/BV1B24y1Q7us/)

52
bootstrap/telegram.go Normal file
View File

@ -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)
}
}

View File

@ -3,7 +3,7 @@ package bootstrap
import ( import (
"github.com/eatmoreapple/openwechat" "github.com/eatmoreapple/openwechat"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/wechatgpt/wechatbot/wechat" "github.com/wechatgpt/wechatbot/handler/wechat"
) )
func StartWebChat() { func StartWebChat() {

View File

@ -8,16 +8,12 @@ var config *Config
type Config struct { type Config struct {
ChatGpt ChatGptConfig `json:"chatgpt"` ChatGpt ChatGptConfig `json:"chatgpt"`
Slack SlackConfig `json:"slack"`
} }
type ChatGptConfig struct { type ChatGptConfig struct {
Keyword string `json:"keyword,omitempty"` Keyword string `json:"keyword,omitempty"`
Token string `json:"token,omitempty" json:"token,omitempty"` Token string `json:"token,omitempty" json:"token,omitempty"`
} Telegram string `json:"telegram"`
type SlackConfig struct {
Token string `json:"token"`
} }
func LoadConfig() error { func LoadConfig() error {

View File

@ -1,3 +1,4 @@
chatgpt: chatgpt:
keyword: chatgpt keyword: chatgpt
token: your token token: your token
telegram: your telegram token

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.16
require ( require (
github.com/eatmoreapple/openwechat v1.2.1 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/sirupsen/logrus v1.6.0
github.com/slack-go/slack v0.11.4
github.com/spf13/viper v1.14.0 github.com/spf13/viper v1.14.0
) )

8
go.sum
View File

@ -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.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= 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-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
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/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= 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.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= 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/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/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/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/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/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= 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.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= 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/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 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=

View File

@ -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
}

View File

@ -10,5 +10,6 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
bootstrap.StartTelegramBot()
bootstrap.StartWebChat() bootstrap.StartWebChat()
} }

BIN
screenshots/IMG_3991.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

BIN
screenshots/telegram.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 419 KiB