forked from m1guelpf/chatgpt-telegram
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
124 lines (106 loc) · 3.33 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
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"
)
const (
messageStart = "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)."
messageHelp = `/reload - clear chatGPT conversation history (Telegram messages will not be deleted)
/setToken <token> - set the openAI session token`
)
func main() {
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)
}
persistentConfig, err := config.LoadOrCreatePersistentConfig()
if err != nil {
log.Fatalf("Couldn't load config: %v", err)
}
if persistentConfig.OpenAISession == "" && !envConfig.ManualAuth {
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")
bot, err := tgbot.New(envConfig.TelegramToken, time.Duration(envConfig.EditWaitSeconds))
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 = messageHelp
case "start":
text = messageStart
case "setToken":
token := update.Message.CommandArguments()
if token == "" {
text = "Please provide a token. Example: /setToken eyJhB..."
break
}
if err := persistentConfig.SetSessionToken(token); err != nil {
text = fmt.Sprintf("Error: %v", err)
break
}
text = "Token set successfully."
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)
}
}
}