diff --git a/context/exported.go b/context/exported.go index 9f65e36..e88e801 100644 --- a/context/exported.go +++ b/context/exported.go @@ -13,12 +13,14 @@ const VERSION = "0.0.2" // Context is the main application context. type Context struct { - Config *config.Struct - Logger zerolog.Logger + Config *config.Struct + Logger zerolog.Logger + ShutdownDone chan bool } // NewContext is an initialization function for Context func NewContext() *Context { c := &Context{} + c.ShutdownDone = make(chan bool, 1) return c } diff --git a/domains/announces/v1/announces.go b/domains/announces/v1/announces.go index da92713..51645c2 100644 --- a/domains/announces/v1/announces.go +++ b/domains/announces/v1/announces.go @@ -16,6 +16,8 @@ func fwMessagesFilter(msg *tdlib.TdMessage) bool { // ZookeeperReceiver adds announcement functionality to bot func ZookeeperReceiver(client *tdlib.Client) { + // Where to send announces + announcerID := int64(665790161) log.Debug().Msg("Adding receiver for @FWorldBot messages...") // Here we can add a receiver to retreive any message type we want // We like to get UpdateNewMessage events and with a specific FilterFunc @@ -63,10 +65,13 @@ func ZookeeperReceiver(client *tdlib.Client) { replyText := battleType + " " + battleTag reply := tdlib.NewInputMessageText(tdlib.NewFormattedText(replyText, nil), true, true) - _, err := client.SendMessage(updateMsg.Message.ChatID, 0, false, true, nil, reply) + _, err := client.SendMessage(announcerID, 0, false, true, nil, reply) if err != nil { log.Error().Err(err) } } + + <-c.ShutdownDone + return } } diff --git a/local/telegram/telegram.go b/local/telegram/telegram.go index 25b13aa..d86ef95 100644 --- a/local/telegram/telegram.go +++ b/local/telegram/telegram.go @@ -78,14 +78,14 @@ func Connect() { log.Debug().Msg("Connection with Telegram established") for update := range rawUpdates { log.Debug().Msgf("Update of type %s received", update.Data["@type"]) + + <-c.ShutdownDone + return } } // Shutdown disconnects from Telegram func Shutdown() { - _, err := client.Destroy() - if err != nil { - log.Error().Err(err).Msg("Failed to destroy Telegram client instance") - } + client.DestroyInstance() log.Info().Msg("Connection with Telegram closed") } diff --git a/main.go b/main.go index 19ec6e2..c4dea95 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,6 @@ func main() { // CTRL+C handler. interrupt := make(chan os.Signal, 1) signal.Notify(interrupt) - shutdownDone := make(chan bool, 1) go func() { signalThing := <-interrupt if signalThing == syscall.SIGTERM || signalThing == syscall.SIGINT { @@ -35,14 +34,14 @@ func main() { telegram.Shutdown() - shutdownDone <- true + c.ShutdownDone <- true } }() announcesv1.New(c) telegram.New(c) - <-shutdownDone + <-c.ShutdownDone os.Exit(0) }