-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
231 lines (210 loc) · 5.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"
"tg-gemini-bot/geminis"
"context"
"github.com/google/generative-ai-go/genai"
"go.uber.org/zap"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"golang.org/x/net/proxy"
tele "gopkg.in/telebot.v3"
)
func buildClientWithProxy(addr string) (*http.Client, error) {
auth := &proxy.Auth{
User: "",
Password: "",
}
dialer, err := proxy.SOCKS5("tcp", string(addr), auth, proxy.Direct)
if err != nil {
return nil, err
}
httpTransport := &http.Transport{Dial: dialer.Dial}
hc := &http.Client{Transport: httpTransport}
return hc, nil
}
type APIKeyProxyTransport struct {
APIKey string
Transport http.RoundTripper
ProxyURL string
}
func detectImageType(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
buffer := make([]byte, 512)
_, err = file.Read(buffer)
if err != nil {
return "", err
}
if buffer[0] == 0xFF && buffer[1] == 0xD8 && buffer[2] == 0xFF {
return "jpeg", nil
} else if buffer[0] == 0x89 && buffer[1] == 0x50 && buffer[2] == 0x4E && buffer[3] == 0x47 {
return "png", nil
} else if buffer[0] == 0x47 && buffer[1] == 0x49 && buffer[2] == 0x46 && buffer[3] == 0x38 {
return "gif", nil
} else if buffer[1] == 0x50 && buffer[2] == 0x4E && buffer[3] == 0x47 {
return "png", nil
} else {
return "Unknown", nil
}
}
func (t *APIKeyProxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
rt := t.Transport
if rt == nil {
rt = http.DefaultTransport
}
// 如果提供了 ProxyURL,则对 Transport 设置代理
if t.ProxyURL != "" {
proxyURL, err := url.Parse(t.ProxyURL)
if err != nil {
return nil, err
}
dialer, err := proxy.FromURL(proxyURL, proxy.Direct)
if err != nil {
return nil, err
}
rt = &http.Transport{
Dial: dialer.Dial,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
// 克隆请求以避免修改原始请求
newReq := *req
args := newReq.URL.Query()
args.Set("key", t.APIKey)
newReq.URL.RawQuery = args.Encode()
resp, err := rt.RoundTrip(&newReq)
if err != nil {
return nil, fmt.Errorf("error during round trip: %v", err)
}
return resp, nil
}
func main() {
var (
proxyaddr string
proxyurl string
verison bool
)
flag.StringVar(&proxyaddr, "proxy", "", "SOCKS5 proxy address")
flag.BoolVar(&version, "version", false, "show version")
flag.Parse()
if version {
fmt.Print("2024.04.10")
}
var pref tele.Settings
if proxyaddr != "" {
client, err := buildClientWithProxy(proxyaddr)
if err != nil {
log.Fatal(err)
return
}
pref = tele.Settings{
Token: os.Getenv("TELEBOT_TOKEN"),
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
Client: client,
}
} else {
pref = tele.Settings{
Token: os.Getenv("TELEBOT_TOKEN"),
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
}
}
if proxyaddr != "" {
proxyurl = "socks5://" + proxyaddr
} else {
proxyurl = ""
}
logger, err := zap.NewProduction()
if err != nil {
panic("Failed to create logger: " + err.Error())
}
defer logger.Sync()
b, err := tele.NewBot(pref)
if err != nil {
zap.Error(err)
return
}
b.Handle(tele.OnPhoto, func(c tele.Context) error {
var (
text = c.Text()
photo = c.Message().Photo
)
client1 := &http.Client{Transport: &APIKeyProxyTransport{
APIKey: os.Getenv("GEMINI_API_KEY"),
Transport: nil,
ProxyURL: proxyurl,
}}
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithHTTPClient(client1))
if err != nil {
logger.Fatal("Error while creating client", zap.Error(err))
}
defer client.Close()
if err != nil {
fmt.Println("解析JSON失败:", err)
}
//for _, photo := range {
b.Download(&photo.File, photo.FileID)
imageType, err := detectImageType(photo.FileID)
if err != nil {
fmt.Println("Error:", err)
return err
}
data, err := os.ReadFile(photo.FileID)
if err != nil {
logger.Fatal("", zap.Error(err))
}
geminis.GetPhotoResponse(c, client.GenerativeModel("gemini-1.0-pro-vision"), imageType, text, data)
os.Remove(photo.FileID)
//}
//c.Send(resp, &tele.SendOptions{ParseMode: tele.ModeMarkdown})
return err
})
b.Handle(tele.OnText, func(c tele.Context) error {
client1 := &http.Client{Transport: &APIKeyProxyTransport{
APIKey: os.Getenv("GEMINI_API_KEY"),
Transport: nil,
ProxyURL: proxyurl,
}}
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithHTTPClient(client1))
//client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
logger.Fatal("Error while creating client", zap.Error(err))
}
defer client.Close()
var (
text = c.Text()
)
if c.Message().Photo == nil {
geminis.GetTextResponse(c, client.GenerativeModel("models/gemini-1.5-pro-latest"), text)
client.ListModels(ctx)
iter := client.ListModels(ctx)
for {
m, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
panic(err)
}
fmt.Println(m.Name, m.Description)
}
//c.Send(resp, &tele.SendOptions{ParseMode: tele.ModeMarkdown})
}
return err
})
b.Start()
}