-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
main.go
107 lines (90 loc) · 2.99 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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/m1guelpf/chatgpt-telegram/src/chatgpt"
"github.com/m1guelpf/chatgpt-telegram/src/config"
"github.com/m1guelpf/chatgpt-telegram/src/session"
"github.com/m1guelpf/chatgpt-telegram/src/tgbot"
)
func main() {
persistentConfig, err := config.LoadOrCreatePersistentConfig()
if err != nil {
log.Fatalf("Couldn't load config: %v", err)
}
if persistentConfig.OpenAISession == "" {
token, err := session.GetSession()
if err != nil {
log.Fatalf("Couldn't get OpenAI session: %v", err)
}
if err = persistentConfig.SetSessionToken(token); err != nil {
log.Fatalf("Couldn't save OpenAI session: %v", err)
}
}
chatGPT := chatgpt.Init(persistentConfig)
log.Println("Started ChatGPT")
envConfig, err := config.LoadEnvConfig(".env")
if err != nil {
log.Fatalf("Couldn't load .env config: %v", err)
}
if err := envConfig.ValidateWithDefaults(); err != nil {
log.Fatalf("Invalid .env config: %v", err)
}
bot, err := tgbot.New(envConfig.TelegramToken, time.Duration(envConfig.EditWaitSeconds*int(time.Second)))
if err != nil {
log.Fatalf("Couldn't start Telegram bot: %v", err)
}
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
bot.Stop()
os.Exit(0)
}()
log.Printf("Started Telegram bot! Message @%s to start.", bot.Username)
for update := range bot.GetUpdatesChan() {
if update.Message == nil {
continue
}
var (
updateText = update.Message.Text
updateChatID = update.Message.Chat.ID
updateMessageID = update.Message.MessageID
updateUserID = update.Message.From.ID
)
if len(envConfig.TelegramID) != 0 && !envConfig.HasTelegramID(updateUserID) {
log.Printf("User %d is not allowed to use this bot", updateUserID)
bot.Send(updateChatID, updateMessageID, "You are not authorized to use this bot.")
continue
}
if !update.Message.IsCommand() {
bot.SendTyping(updateChatID)
feed, err := chatGPT.SendMessage(updateText, updateChatID)
if err != nil {
bot.Send(updateChatID, updateMessageID, fmt.Sprintf("Error: %v", err))
} else {
bot.SendAsLiveOutput(updateChatID, updateMessageID, feed)
}
continue
}
var text string
switch update.Message.Command() {
case "help":
text = "Send a message to start talking with ChatGPT. You can use /reload at any point to clear the conversation history and start from scratch (don't worry, it won't delete the Telegram messages)."
case "start":
text = "Send a message to start talking with ChatGPT. You can use /reload at any point to clear the conversation history and start from scratch (don't worry, it won't delete the Telegram messages)."
case "reload":
chatGPT.ResetConversation(updateChatID)
text = "Started a new conversation. Enjoy!"
default:
text = "Unknown command. Send /help to see a list of commands."
}
if _, err := bot.Send(updateChatID, updateMessageID, text); err != nil {
log.Printf("Error sending message: %v", err)
}
}
}