Wait sending announcements to all users before panic

This commit is contained in:
Maxim Lebedev 2018-01-25 17:25:13 +05:00
parent 27611490a5
commit 7c6aa96eff
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
2 changed files with 6 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package main
// errCheck helps debug critical errors without warnings from 'gocyclo' linter
func errCheck(err error) {
if err != nil {
waitForwards.Wait() // Wait what all users get announcement message
panic(err.Error())
}
}

View File

@ -1,12 +1,15 @@
package main
import (
"sync"
"time"
log "github.com/kirillDanshin/dlog"
tg "github.com/toby3d/telegram"
)
var waitForwards = new(sync.WaitGroup)
func updateChannelPost(post *tg.Message) {
if post.Chat.ID != channelID {
log.Ln(post.Chat.ID, "!=", channelID)
@ -17,11 +20,13 @@ func updateChannelPost(post *tg.Message) {
errCheck(err)
for i := range users {
waitForwards.Add(1)
if _, err = bot.ForwardMessage(
tg.NewForwardMessage(post.Chat.ID, int64(users[i]), post.ID),
); err != nil {
log.Ln(err.Error())
}
waitForwards.Done()
time.Sleep(time.Second / 10) // For avoid spamming
}