-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (54 loc) · 1.49 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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/gouae/hummus/internal/config"
"github.com/gouae/hummus/internal/discord"
"github.com/gouae/hummus/internal/utils"
"github.com/gouae/hummus/internal/whatsapp"
// autoload loads all the environment variables from the .env file
_ "github.com/joho/godotenv/autoload"
)
func main() {
cfg, err := config.LoadFromEnv()
if err != nil {
utils.LogError(err, "Failed to load config from environment")
return
}
discordBot, err := discord.New(cfg)
if err != nil {
utils.LogError(err, "Failed to start the discord bot")
return
}
// Signal handling to gracefully shutdown the bot
stop := make(chan os.Signal, 1)
// channel to listen for SIGINT and SIGTERM events (CTRL-C etc.)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
err = discordBot.Run()
if err != nil {
return
}
waBot, err := whatsapp.New(cfg, discordBot)
if err != nil {
utils.LogError(err, "Failed to initialize the whatsapp bot")
utils.LogError(discordBot.Stop(), "Error stopping discord bot")
return
}
err = waBot.Run()
if err != nil {
utils.LogError(err, "Failed to start the whatsapp bridge")
utils.LogError(discordBot.Stop(), "Error stopping discord bot")
return
}
<-stop // Wait for a termination signal
log.Println("Shutting down the bot...")
waBot.Stop()
err = discordBot.Stop()
if err != nil {
utils.LogError(discordBot.Stop(), "Error stopping discord bot")
return
}
log.Println("Bot stopped gracefully.")
}