-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
69 lines (58 loc) · 1.51 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
/*
* File: main.go
* Project: slackbot
* File Created: Tuesday, 24th January 2023 5:26:47 pm
* Author: Mark Mester (mmester6016@gmail.com)
* -----
* Last Modified: Saturday, 28th January 2023 4:53:25 pm
* Modified By: Mark Mester (mmester6016@gmail.com>)
*/
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
_ "github.com/joho/godotenv/autoload"
slackbot "github.com/markmester/fanyi-slackbot/pkg/bot"
clients "github.com/markmester/fanyi-slackbot/pkg/clients"
)
var (
slackBotToken = getEnvOrPanic("SLACK_BOT_TOKEN")
slackAppToken = getEnvOrPanic("SLACK_APP_TOKEN")
chatGptApiKey = getEnvOrPanic("CHATGPT_API_KEY")
chatGptEngine = getEnvOrPanic("CHATGPT_COMPLETION_ENGINE")
datastorePath = os.Getenv("DATASTORE_PATH")
)
func getEnvOrPanic(env string) string {
e := os.Getenv(env)
if e == "" {
panic(fmt.Sprintf("Required environmental variable unset: %s", env))
}
return e
}
func main() {
// Initialize clients
slackClient := clients.NewSlackClient(slackBotToken, slackAppToken)
gpt3Client := clients.NewGpt3Client(chatGptApiKey, chatGptEngine)
detector := clients.NewDetector()
datastore, err := clients.NewDatastore(datastorePath)
if err != nil {
panic(err)
}
// Initialize bot
bot, err := slackbot.New(slackClient, gpt3Client, detector, datastore)
if err != nil {
panic(err)
}
defer bot.Shutdown()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// Wake up!
go func() {
if err := bot.Process(); err != nil {
panic(err)
}
}()
<-sigs
}