1
Fork 0

Fix shutdown, fix broken announces

master
Vladimir Hodakov 2019-01-09 03:24:12 +04:00
parent 7612ce778f
commit dca290beb1
Signed by: Vladimir Hodakov
GPG Key ID: 673980B6882F82C6
4 changed files with 16 additions and 10 deletions

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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")
}

View File

@ -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)
}