-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
79 lines (61 loc) · 1.8 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
package main
import (
"google_genai/genai"
"google_genai/telegram"
"log"
"net/http"
"os"
)
func main() {
if os.Getenv("GEMINI_API_KEY") == "" {
log.Fatal("GEMINI_API_KEY environment variable is not set")
}
if os.Getenv("BOT_TOKEN") == "" {
log.Fatal("BOT_TOKEN environment variable is not set")
}
if os.Getenv("WEBHOOK_URL") == "" {
log.Fatal("WEBHOOK_URL environment variable is not set")
}
if os.Getenv("PORT") == "" {
log.Fatal("WEBHOOK_URL environment variable is not set")
}
bot := telegram.NewBot(os.Getenv("BOT_TOKEN"))
genAIHandler := genai.NewHandler(bot)
err := bot.SetWebhook(os.Getenv("WEBHOOK_URL"))
if err != nil {
log.Fatal("Error setting webhook:", err)
}
http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
update, err := bot.ParseUpdate(r)
if err != nil {
log.Printf("Error parsing update: %v", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
if update.Message != nil {
if len(update.Message.Entities) > 0 && update.Message.Entities[0].Type == "bot_command" {
bot.HandleCommands(update.Message.Chat.ID, update.Message.Text)
} else {
chatID := update.Message.Chat.ID
text := update.Message.Text
log.Printf("ChatId: %d \nText: %s", chatID, text)
messageId, err := bot.SendLoadingMessage(chatID, "⏳")
if err != nil {
log.Println("Error sending loading message:", err)
}
log.Printf("Loading message ID: %d", messageId)
go genAIHandler.ProcessMessage(text, chatID, messageId)
}
}
w.WriteHeader(http.StatusOK)
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
cleanup := genai.NewCleanupService("synapse_files")
cleanup.Start()
defer cleanup.Stop()
log.Printf("Starting server on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}