-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,455 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
chromedriver | ||
.idea | ||
*.json | ||
sdk/ | ||
event.go | ||
main.go | ||
*cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,47 @@ | ||
# chatgpt | ||
使用chrome webdriver调用chatgpt plus | ||
|
||
使用chrome webdriver调用chatgpt 支持plus | ||
|
||
## 使用方法 | ||
|
||
1. 架梯子访问 https://chat.openai.com/chat ,登录并能成功进入聊天界面 | ||
2. 安装chrome插件:EditThisCookie https://chrome.google.com/webstore/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg | ||
3. 导出当前页面的cookie,保存到 cookies.json | ||
4. 参考example/main.go,编写自己的聊天机器人 | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/cherish-chat/chatgpt" | ||
"log" | ||
) | ||
|
||
func main() { | ||
helper := chatgpt.NewHelper("cookies.json") | ||
err := helper.LaunchBrowser() | ||
if err != nil { | ||
log.Fatalf("launch browser error: %v", err) | ||
} | ||
eh := xxim() | ||
for { | ||
select { | ||
case msgData := <-eh.msgChan: | ||
go func() { | ||
if helper.IsLocked() { | ||
eh.sendTextMsg(msgData.ConvId, "机器人正在忙,请稍后再试") | ||
} else { | ||
page := helper.MustGetPage(msgData.ConvId) | ||
reply, err := helper.SendMsg(page, string(msgData.Content)) | ||
if err != nil { | ||
eh.sendTextMsg(msgData.ConvId, "机器人出错了,请稍后再试: "+err.Error()) | ||
} else { | ||
eh.sendTextMsg(msgData.ConvId, reply) | ||
} | ||
} | ||
}() | ||
} | ||
} | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package chatgpt | ||
|
||
import ( | ||
"github.com/playwright-community/playwright-go" | ||
"github.com/sirupsen/logrus" | ||
"os" | ||
) | ||
|
||
func (h *Helper) LaunchBrowser() error { | ||
runOptions := &playwright.RunOptions{ | ||
Browsers: []string{"firefox"}, | ||
Verbose: false, | ||
} | ||
err := playwright.Install(runOptions) | ||
if err != nil { | ||
logrus.Errorf("Error while installing playwright: %v", err) | ||
return err | ||
} | ||
logrus.Info("Playwright installed successfully") | ||
pw, err := playwright.Run() | ||
if err != nil { | ||
logrus.Errorf("Error while running playwright: %v", err) | ||
return err | ||
} | ||
logrus.Info("Playwright running successfully") | ||
args := []string{} | ||
os.RemoveAll("./cache") | ||
browser, err := pw.Firefox.LaunchPersistentContext("./cache", playwright.BrowserTypeLaunchPersistentContextOptions{ | ||
Args: args, | ||
Headless: playwright.Bool(false), | ||
}) | ||
if err != nil { | ||
logrus.Errorf("Error while launching browser: %v", err) | ||
return err | ||
} | ||
logrus.Info("Browser launched successfully") | ||
h.browser = browser | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package chatgpt | ||
|
||
import ( | ||
"github.com/playwright-community/playwright-go" | ||
"sync" | ||
) | ||
|
||
type Helper struct { | ||
CookiePath string | ||
browser playwright.BrowserContext | ||
pageMap sync.Map | ||
sendMsgLock sync.Mutex | ||
} | ||
|
||
func NewHelper(cookiePath string) *Helper { | ||
h := &Helper{CookiePath: cookiePath} | ||
return h | ||
} | ||
|
||
func (h *Helper) IsLocked() bool { | ||
locked := h.sendMsgLock.TryLock() | ||
if locked { | ||
h.sendMsgLock.Unlock() | ||
} | ||
return !locked | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package config | ||
|
||
type EditThisCookieItem struct { | ||
Domain string `json:"domain"` | ||
ExpirationDate float64 `json:"expirationDate"` | ||
HostOnly bool `json:"hostOnly"` | ||
HttpOnly bool `json:"httpOnly"` | ||
Name string `json:"name"` | ||
Path string `json:"path"` | ||
SameSite string `json:"sameSite"` | ||
Secure bool `json:"secure"` | ||
Session bool `json:"session"` | ||
StoreId string `json:"storeId"` | ||
Value string `json:"value"` | ||
Id int `json:"id"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/playwright-community/playwright-go" | ||
"github.com/sirupsen/logrus" | ||
"os" | ||
) | ||
|
||
func LoadCookies(filepath string) ([]playwright.BrowserContextAddCookiesOptionsCookies, error) { | ||
// 读取文件 | ||
file, err := os.OpenFile(filepath, os.O_RDONLY, 0666) | ||
if err != nil { | ||
logrus.Errorf("Error while opening file: %v", err) | ||
return nil, err | ||
} | ||
defer file.Close() | ||
// 解析文件 | ||
var cookies []EditThisCookieItem | ||
err = json.NewDecoder(file).Decode(&cookies) | ||
if err != nil { | ||
logrus.Errorf("Error while decoding file: %v", err) | ||
return nil, err | ||
} | ||
var result []playwright.BrowserContextAddCookiesOptionsCookies | ||
for _, cookie := range cookies { | ||
site := cookie.SameSite | ||
// site 首字母大写 | ||
switch site { | ||
case "lax": | ||
site = "Lax" | ||
case "strict": | ||
site = "Strict" | ||
default: | ||
site = "None" | ||
} | ||
attribute := playwright.SameSiteAttribute(site) | ||
result = append(result, playwright.BrowserContextAddCookiesOptionsCookies{ | ||
Name: playwright.String(cookie.Name), | ||
Value: playwright.String(cookie.Value), | ||
//URL: playwright.String(`https://chat.openai.com`), | ||
Domain: playwright.String(cookie.Domain), | ||
Path: playwright.String(cookie.Path), | ||
Expires: playwright.Float(cookie.ExpirationDate), | ||
HttpOnly: playwright.Bool(cookie.HttpOnly), | ||
Secure: playwright.Bool(cookie.Secure), | ||
SameSite: &attribute, | ||
}) | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```shell | ||
go get -u github.com/cherish-chat/xxim-server@showurl | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
module github.com/cherish-chat/chatgpt/example | ||
|
||
go 1.19 | ||
|
||
replace github.com/cherish-chat/chatgpt => ../../chatgpt | ||
|
||
require ( | ||
github.com/cherish-chat/chatgpt v0.0.0 | ||
github.com/cherish-chat/xxim-server v0.0.0-20230224015926-6981d04e77d3 | ||
github.com/zeromicro/go-zero v1.4.2 | ||
google.golang.org/protobuf v1.28.1 | ||
nhooyr.io/websocket v1.8.7 | ||
) | ||
|
||
require ( | ||
github.com/atotto/clipboard v0.1.4 // indirect | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/bwmarrin/snowflake v0.3.0 // indirect | ||
github.com/cenkalti/backoff/v4 v4.1.3 // indirect | ||
github.com/cespare/xxhash/v2 v2.1.2 // indirect | ||
github.com/coreos/go-semver v0.3.0 // indirect | ||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect | ||
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect | ||
github.com/fatih/color v1.13.0 // indirect | ||
github.com/go-logr/logr v1.2.3 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/go-redis/redis/v8 v8.11.5 // indirect | ||
github.com/go-stack/stack v1.8.1 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/mock v1.6.0 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/go-cmp v0.5.8 // indirect | ||
github.com/google/gofuzz v1.2.0 // indirect | ||
github.com/googleapis/gnostic v0.5.5 // indirect | ||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/klauspost/compress v1.15.9 // indirect | ||
github.com/mattn/go-colorable v0.1.9 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/openzipkin/zipkin-go v0.4.0 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.5 // indirect | ||
github.com/playwright-community/playwright-go v0.2000.1 // indirect | ||
github.com/prometheus/client_golang v1.13.0 // indirect | ||
github.com/prometheus/client_model v0.2.0 // indirect | ||
github.com/prometheus/common v0.37.0 // indirect | ||
github.com/prometheus/procfs v0.8.0 // indirect | ||
github.com/sirupsen/logrus v1.9.0 // indirect | ||
github.com/spaolacci/murmur3 v1.1.0 // indirect | ||
go.etcd.io/etcd/api/v3 v3.5.5 // indirect | ||
go.etcd.io/etcd/client/pkg/v3 v3.5.5 // indirect | ||
go.etcd.io/etcd/client/v3 v3.5.5 // indirect | ||
go.opentelemetry.io/otel v1.11.0 // indirect | ||
go.opentelemetry.io/otel/exporters/jaeger v1.11.0 // indirect | ||
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.0 // indirect | ||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.11.0 // indirect | ||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.0 // indirect | ||
go.opentelemetry.io/otel/exporters/zipkin v1.11.0 // indirect | ||
go.opentelemetry.io/otel/sdk v1.11.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.11.0 // indirect | ||
go.opentelemetry.io/proto/otlp v0.19.0 // indirect | ||
go.uber.org/atomic v1.9.0 // indirect | ||
go.uber.org/automaxprocs v1.5.1 // indirect | ||
go.uber.org/multierr v1.8.0 // indirect | ||
go.uber.org/zap v1.21.0 // indirect | ||
golang.org/x/net v0.2.0 // indirect | ||
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect | ||
golang.org/x/sys v0.2.0 // indirect | ||
golang.org/x/term v0.2.0 // indirect | ||
golang.org/x/text v0.4.0 // indirect | ||
golang.org/x/time v0.1.0 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/genproto v0.0.0-20220602131408-e326c6e8e9c8 // indirect | ||
google.golang.org/grpc v1.50.1 // indirect | ||
gopkg.in/inf.v0 v0.9.1 // indirect | ||
gopkg.in/square/go-jose.v2 v2.6.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
k8s.io/api v0.22.9 // indirect | ||
k8s.io/apimachinery v0.22.9 // indirect | ||
k8s.io/client-go v0.22.9 // indirect | ||
k8s.io/klog/v2 v2.40.1 // indirect | ||
k8s.io/utils v0.0.0-20220706174534-f6158b442e7c // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect | ||
sigs.k8s.io/yaml v1.2.0 // indirect | ||
) |
Oops, something went wrong.