Fix shutdown, fix broken announces
This commit is contained in:
parent
7612ce778f
commit
dca290beb1
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
|
5
main.go
5
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)
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user