-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
222 lines (186 loc) · 5.83 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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/option"
)
func main() {
tgToken := flag.String("TELEGRAM_BOT_TOKEN", "", "Telegram Bot Token")
apiKey := flag.String("GOOGLE_GEMINI_KEY", "", "API Key for genai")
flag.Parse()
if *tgToken == "" {
*tgToken = os.Getenv("TELEGRAM_BOT_TOKEN")
}
if *tgToken == "" {
log.Fatal("Telegram Bot Token must be set")
}
if *apiKey == "" {
*apiKey = os.Getenv("GOOGLE_GEMINI_KEY")
}
if *apiKey == "" {
log.Fatal("API Key must be set")
}
bot, err := tgbotapi.NewBotAPI(*tgToken)
if err != nil {
log.Panic(err)
}
bot.Debug = true
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(*apiKey))
if err != nil {
log.Fatal(err)
}
defer client.Close()
updateConfig := tgbotapi.NewUpdate(0)
updateConfig.Timeout = 60
updates, err := bot.GetUpdatesChan(updateConfig)
for update := range updates {
if update.Message != nil {
if update.Message.Text != "" {
go handleMessage(bot, client, update)
} else if update.Message.Photo != nil {
go handlePhoto(bot, update, client)
}
}
}
}
func handleMessage(bot *tgbotapi.BotAPI, client *genai.Client, update tgbotapi.Update) {
if update.Message == nil {
return // 确保消息存在
}
// 获取机器人的用户名
botUsername, _ := bot.GetMe()
// 构建可能的命令格式
geminiCommand := "/gemini"
geminiCommandWithBot := fmt.Sprintf("/gemini@%s", botUsername.UserName)
// 检查是否是私聊或群组中的特定命令
isPrivateChat := update.Message.Chat.IsPrivate()
isGeminiCommand := strings.HasPrefix(update.Message.Text, geminiCommand) || strings.HasPrefix(update.Message.Text, geminiCommandWithBot)
if isPrivateChat || isGeminiCommand {
// 移除命令部分,保留查询内容
userQuestion := strings.TrimSpace(strings.TrimPrefix(update.Message.Text, geminiCommand))
userQuestion = strings.TrimSpace(strings.TrimPrefix(userQuestion, geminiCommandWithBot))
if userQuestion == "" && !isPrivateChat {
replyWithResponse(bot, update, nil)
return
}
ctx := context.Background()
// 使用用户的提问作为输入生成内容
resp, err := client.GenerativeModel("gemini-pro").GenerateContent(ctx, genai.Text(userQuestion))
if err != nil {
log.Println("Error generating content:", err)
replyWithResponse(bot, update, nil)
return
}
replyWithResponse(bot, update, resp)
}
}
func handlePhoto(bot *tgbotapi.BotAPI, update tgbotapi.Update, client *genai.Client) {
photos := *update.Message.Photo
texts := update.Message.Text
fmt.Println("texts from photo", texts)
// 获取机器人的用户名
botUsername, _ := bot.GetMe()
// 构建可能的命令格式
geminiCommandWithBot := fmt.Sprintf("/gemini@%s", botUsername.UserName)
// 检查是否是私聊或群组中的特定命令
isPrivateChat := update.Message.Chat.IsPrivate()
isGeminiCommand := strings.HasPrefix(update.Message.Caption, "/gemini") || strings.HasPrefix(update.Message.Caption, geminiCommandWithBot)
if isPrivateChat || isGeminiCommand {
if len(photos) > 0 {
photo := photos[len(photos)-1]
// 获取图片的文件信息
fileID := photo.FileID
file, err := bot.GetFile(tgbotapi.FileConfig{FileID: fileID})
if err != nil {
log.Println("Error getting file:", err)
return
}
// 下载图片
imgData, err := downloadImage(bot, file.FileID)
if err != nil {
log.Println("Error downloading image:", err)
return
}
// 获取用户的文本输入
userText := ""
if update.Message.Caption != "" {
userText = strings.TrimSpace(strings.TrimPrefix(update.Message.Caption, "/gemini"))
userText = strings.TrimSpace(strings.TrimPrefix(userText, geminiCommandWithBot))
}
// 处理图片
processAndReplyImage(bot, update, client, imgData, userText)
}
}
}
func processAndReplyImage(bot *tgbotapi.BotAPI, update tgbotapi.Update, client *genai.Client, imgData []byte, userText string) {
ctx := context.Background()
// 使用 Gemini API 处理图片
model := client.GenerativeModel("gemini-pro-vision")
prompt := []genai.Part{
genai.ImageData("jpeg", imgData),
}
// 如果用户提供了文本描述,则添加到 prompt 中
if userText != "" {
prompt = append(prompt, genai.Text(userText))
} else {
// 如果没有用户输入的文本,可以使用默认文本或省略
prompt = append(prompt, genai.Text("描述这张图片"))
}
resp, err := model.GenerateContent(ctx, prompt...)
if err != nil {
log.Println("Error generating content:", err)
return
}
// 处理响应并回复用户
replyWithResponse(bot, update, resp)
}
func replyWithResponse(bot *tgbotapi.BotAPI, update tgbotapi.Update, resp *genai.GenerateContentResponse) {
// 假设我们只关心第一个响应
if len(resp.Candidates) > 0 {
messageText := printResponse(resp)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, messageText)
msg.ParseMode = tgbotapi.ModeMarkdown
msg.ReplyToMessageID = update.Message.MessageID
if _, err := bot.Send(msg); err != nil {
log.Println("Error sending message:", err)
}
}
}
func downloadImage(bot *tgbotapi.BotAPI, filePath string) ([]byte, error) {
resp, err := bot.GetFileDirectURL(filePath)
if err != nil {
return nil, err
}
// 发起 HTTP GET 请求下载文件
response, err := http.Get(resp)
if err != nil {
return nil, err
}
defer response.Body.Close()
// 读取响应内容
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return data, nil
}
func printResponse(resp *genai.GenerateContentResponse) string {
var ret string
for _, cand := range resp.Candidates {
for _, part := range cand.Content.Parts {
ret = ret + fmt.Sprintf("%v", part)
fmt.Println(part)
}
}
fmt.Println("---")
return ret + "\n---"
}